<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "admin_holidays";
$page_name      = "Add Holiday";
$id	 = "";
$vat = "";
$query_string = "";
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	
    $holiday_date 	= Utility::setFormatedDate($_REQUEST['holiday_date']); 
    $holiday_name 	= $_REQUEST['holiday_name']; 
    $holiday_year 	=  $_REQUEST['holiday_year']; 
	
    $sql= "INSERT INTO $table_name (holiday_date,holiday_name,holiday_year) VALUES('$holiday_date','$holiday_name','$holiday_year')";
    mysqli_query($conn,$sql);
	
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "$tmp added successfully";
    header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];

	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit Holiday";
	$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
	
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

	 $holiday_date 	= $row_edit['holiday_date']; 
    $holiday_name 	= $row_edit['holiday_name']; 
    $holiday_year 	=  $row_edit['holiday_year']; 
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	 
	$id					= $_REQUEST['id'];
	 $holiday_date 	=  Utility::setFormatedDate($_REQUEST['holiday_date']); 
    $holiday_name 	= $_REQUEST['holiday_name']; 
    $holiday_year 	= $_REQUEST['holiday_year']; 
	

	$sql= "UPDATE $table_name SET holiday_date = '$holiday_date' ,holiday_name = '$holiday_name' ,holiday_year = '$holiday_year' WHERE id = $id";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add", "page=view", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "$tmp updated successfully";
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<style>
em{color:red;font-size: 20px;position: absolute;}
</style>
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1><?=$page_name;?></h1>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="row">
		<div class="col-lg-12">
			<!-- general form elements -->
              <div class="card box-primary">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body row">
                    <div class="col-lg-4">
                      <label for="part_num_category" class="control-label">Holiday Name<em>*</em></label>
                      <div class="">
                        <input type="text" id="holiday_name" name="holiday_name" value="<?php echo $holiday_name;?>" class="form-control" placeholder="Enter Holiday Name" required>
                      </div>
                    </div>
					<div class="col-lg-4">
                      <label for="part_num_internal" class=" control-label">Holiday Date<em></em></label>
                      <div class="">
                      <input type="text" class="form-control datepicker" id="holiday_date" name="holiday_date" value="<?php (isset($holiday_date) && $holiday_date!="") ? utility::getFormatedDate($holiday_date) : "";?>" placeholder="DD-MM-YYYY">
                      </div>
                    </div>
					<div class="col-lg-4 hide">
                      <label for="part_num_internal" class=" control-label">Holiday Year<em></em></label>
                      <div class="">
                       <select id="year" name="holiday_year" class="form-control select2">
							<option value="">Select Year</option>
							<?php
							$startYear = date("Y") - 4; // Four years prior to the current year
								$endYear = date("Y") + 4;   // Four years subsequent to the current year
								$holiday_year = isset($holiday_year) && $holiday_year != '' ? $holiday_year : date("Y"); // Default to current year if not set

								for ($year = $startYear; $year <= $endYear; $year++) {
									$selected = ($year == $holiday_year) ? 'selected' : '';
									echo "<option value=\"$year\" $selected>$year</option>";
								}
							?>
						</select>
                      </div>
                    </div>
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary">Submit
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">
$(document).ready(function(){
	$("#form1").submit(function() {
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
		//return true;
	});
	$("#form1").on("blur","#part_num_category",function(){	
		var part_num_category = $(this).val();
		var part_num_category = $("#part_num_category").val();
		var id = $("#id").val();
		if(part_num_category != ""){
		var form_data   = {'part_num_category':part_num_category,'id':id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/part_num_category/index.php?page=ajax_actions&action=check_part_num_category_exist",
				data: form_data,
				success: function(result){          
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "failure"){
						//alert("This part_num_category already in system");
						$("#part_num_category_check").show();
						$("#part_num_category_check").html("This part_num_category already in system.");
						//$("#email").val("");
						$("#part_num_category").focus();
					}else{
						$("#part_num_category_check").html("");
						$("#part_num_category_check").hide();
					}
				}
			});
		}
	});
});
</script>