<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Submit";
$table_name 	= "leave_holiday_management";
$id	 = "";
$vat = "";
$query_string = "";
$page_name      = "Holiday Request";
$from = $_REQUEST['from'];
$employee_name_id = $_SESSION['admin_user_id'];
$clock_no_id = $_SESSION['admin_user_id'];
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	
	
    $employee_name_id			= $_REQUEST['employee_name_id'];
	$clock_no_id				= $_REQUEST['clock_no_id'];
	$leave_holiday_start_date	= utility::setFormatedDate($_REQUEST['leave_holiday_start_date']);
	$leave_holiday_end_date		= utility::setFormatedDate($_REQUEST['leave_holiday_end_date']);
	$no_of_day				    = $_REQUEST['no_of_day'];
	$is_authorised				= $_REQUEST['is_authorised'];
	$holiday_authorised_by		= $_REQUEST['holiday_authorised_by'];
	$paid_week_num				= $_REQUEST['paid_week_num'];
	$rejection_reason			= $_REQUEST['rejection_reason'];
	$holiday_auth_date_time		= utility::setFormatedDate($_REQUEST['holiday_auth_date_time']);
	$date_submitted_finance		= utility::setFormatedDate($_REQUEST['date_submitted_finance']);
	
	$created_by					= $_SESSION['user_id'];
	$created_on					= date("Y-m-d H:i:s");	
	$updated_by					= $_SESSION['user_id'];
	$leave_holiday_type			= 1;

	$sql = "employee_name_id = '".$employee_name_id."', clock_no_id = '".$clock_no_id."', leave_holiday_start_date = '".$leave_holiday_start_date."', leave_holiday_end_date = '".$leave_holiday_end_date."', no_of_day = '".$no_of_day."', is_authorised = '".$is_authorised."', authorised_by = '".$holiday_authorised_by."', date_submitted_finance = '".$date_submitted_finance."', paid_week_num = '".$paid_week_num."', rejection_reason = '".$rejection_reason."', auth_date_time = '".$holiday_auth_date_time."',leave_holiday_type = '".$leave_holiday_type."',created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";

	$sql= "INSERT INTO $table_name SET ".$sql;
	
    mysqli_query($conn,$sql);
	
	
	$_SESSION['info_message'] = "Holiday added successfully";
	header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];
	$leave_id = utility::app_crypt($_REQUEST['leave_id'],'d');
		if($_REQUEST['id'] != $leave_id){
			die("Invaild URL");
		}
	$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);
	$row_edit = utility::validate_crypt_data($row_edit);
	
   
    $employee_name_id			= $row_edit['employee_name_id'];
	$clock_no_id				= $row_edit['clock_no_id'];
	$leave_holiday_start_date	= ($row_edit['leave_holiday_start_date']);
	$leave_holiday_end_date		= $row_edit['leave_holiday_end_date'];
	$no_of_day				    = $row_edit['no_of_day'];
	$is_authorised				= $row_edit['is_authorised'];
	$holiday_authorised_by		= $row_edit['authorised_by'];
	$date_submitted_finance		= $row_edit['date_submitted_finance'];
	$paid_week_num				= $row_edit['paid_week_num'];
	$rejection_reason			= $row_edit['rejection_reason'];
	$holiday_auth_date_time		= $row_edit['auth_date_time'];
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	
	$id  						= $_REQUEST['id'];
    $employee_name_id			= $_REQUEST['employee_name_id'];
	$clock_no_id				= $_REQUEST['clock_no_id'];
	$leave_holiday_start_date	= utility::setFormatedDate($_REQUEST['leave_holiday_start_date']);
	$leave_holiday_end_date		= utility::setFormatedDate($_REQUEST['leave_holiday_end_date']);
	$no_of_day				    = $_REQUEST['no_of_day'];
	$is_authorised				= $_REQUEST['is_authorised'];
	$holiday_authorised_by		= $_REQUEST['holiday_authorised_by'];
	$paid_week_num				= $_REQUEST['paid_week_num'];
	$rejection_reason			= $_REQUEST['rejection_reason'];
	$holiday_auth_date_time		= ($_REQUEST['holiday_auth_date_time']);
	$date_submitted_finance		= utility::setFormatedDate($_REQUEST['date_submitted_finance']);
		
	$updated_by		 		    = $_SESSION['user_id'];
	
	$sql = "employee_name_id = '".$employee_name_id."', clock_no_id = '".$clock_no_id."', leave_holiday_start_date = '".$leave_holiday_start_date."', leave_holiday_end_date = '".$leave_holiday_end_date."', no_of_day = '".$no_of_day."', date_submitted_finance = '".$date_submitted_finance."', paid_week_num = '".$paid_week_num."', rejection_reason = '".$rejection_reason."',  created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";

	$sql= "UPDATE $table_name SET ".$sql." 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;
	
	$leave_id = urlencode(utility::app_crypt($id,'e'));
	
	$_SESSION['info_message'] = "Holiday updated successfully";
	if(isset($from) && $from == "view_leave_progress"){
		header("Location: index?page=view_leave_progress&from=view&leave_id=$leave_id&id=$id");
		exit();
	}
	
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<style>
em{color:red;font-size: 20px;position: absolute;}

fieldset {
    border-top: 4px solid #dedede; 
    border-right: 1px solid #dedede;
    border-bottom: 1px solid #dedede; 
    border-left: 1px solid #dedede; 
    padding: 15px;
    margin-bottom: 10px;
}

legend {
    width: auto;
    color: #a423a4;
}
</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 class="col-sm-6">
			<?
			$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
			if(isset($from) && $from == "view_leave_progress"){
				$query_string = str_ireplace("page=add_holiday", "page=view_leave_progress", $query_string);
			}else {
				$query_string = str_ireplace("page=add_holiday", "page=view", $query_string);
			}
				
			?>
			<button type="button" id="back" name="back" value="back"  style="float:right;margin-right:5px;margin-bottom:8px;" class="btn btn-primary" onclick="window.location.href = 'index?<?=$query_string;?>'">Back</button>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="row">
		<div class="col-12">
			<!-- Message -->
			<?php if(isset($_SESSION['info_message'])) { ?>
				<div class="alert alert-info alert-dismissable">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					<?php echo $_SESSION['info_message']; unset($_SESSION['info_message']); ?>
				</div>
			<?php } ?>
			
			<!-- General form elements -->
			<div class="card">
				<form method="post" name="form1" id="form1" action="index?page=add_holiday&action=<?php echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
					<div class="card-body">                    
						<div class="row mb-3">
							<div class="col-md-4">
								<label for="employeeName" class="form-label">Employee Name<em>*</em></label>
								<? $gsi->getEmployeeList("employee_name_id", $employee_name_id, "100%", false, "form-control"); ?>
							</div>
							<div class="col-md-4">
								<label for="clockNo" class="form-label">Clock No</label>
									 <? $gsi->getEmployeeClockNoList("clock_no_id", $clock_no_id, "100%", false, "form-control"); ?>
							</div>
						</div>

						<div class="row mb-3">
							<div class="col-md-4">
								<label for="holidayStartDate" class="form-label">Holiday Start Date</label>
								<input type="text" class="form-control datepicker" id="leave_holiday_start_date" name="leave_holiday_start_date" value="<?php echo (isset($leave_holiday_start_date) && $leave_holiday_start_date != "") ? utility::getFormatedDate($leave_holiday_start_date) : ""; ?>" placeholder="DD-MM-YYYY" autocomplete="off">
							</div>
							<div class="col-md-4">
								<label for="holidayEndDate" class="form-label">Holiday End Date</label>
								<input type="text" class="form-control datepicker" id="leave_holiday_end_date" name="leave_holiday_end_date" value="<?php echo (isset($leave_holiday_end_date) && $leave_holiday_end_date != "") ? utility::getFormatedDate($leave_holiday_end_date) : ""; ?>" placeholder="DD-MM-YYYY" autocomplete="off">
							</div>
							<div class="col-md-4">
								<label for="noOfDays" class="form-label">No of Days</label>
								<input type="number" class="form-control" name="no_of_day" id="no_of_day" value="<?php echo $no_of_day;?>" readonly placeholder="No of Days">
							</div>
						</div>
                           
						<div class="row mb-3">
							<div class="col-md-4">
								<label for="authorised" class="form-label">Authorised?</label>
								<input type="checkbox" class="flat-red" id="is_authorised" name="is_authorised" value="1" <?php echo ($is_authorised == 1) ? 'checked' : ''; ?>> 
							</div>
							<div class="col-md-4">
								<label for="authorisedBy" class="form-label">Authorised By</label>
								<? $gsi->getEmployeeList("holiday_authorised_by", $holiday_authorised_by, "100%", false, "form-control"); ?>
							</div>
							<div class="col-md-4">
								<label for="authorisedDate" class="form-label">Authorised Date & Time</label>
								<div class="d-flex">
									<input type="text" class="form-control datepicker" id="holiday_auth_date_time" name="holiday_auth_date_time"  value="<?php echo (isset($holiday_auth_date_time) && $holiday_auth_date_time !="") ? date("d-m-Y H:i:s", strtotime($holiday_auth_date_time)) : "";?>" placeholder="DD-MM-YYYY" autocomplete="off">
									<?php //utility::getTimeList("holiday_auth_time", $holiday_auth_time,"00:00","Time",false,"form-control select2",""); ?>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="form-group col-lg-12">
							  <label class="control-label" for="rejection_reason">Rejection Reason<em></em></label>
							  <div class="">
								<textarea id="rejection_reason" name="rejection_reason" rows="3" cols="50" maxlength="500" class="form-control" placeholder="Rejection Reason"  style="resize: vertical;" readonly><?php echo $rejection_reason;?></textarea>
							  </div>
							</div>
						</div>

						<h4>Finance</h4>
						
						<div class="row mb-3">
							<div class="col-md-4">
								<label for="dateSubmitted" class="form-label">Date Submitted to Finance</label>
								<input type="text" class="form-control datepicker" id="date_submitted_finance" name="date_submitted_finance" value="<?php echo (isset($date_submitted_finance) && $date_submitted_finance != "") ? utility::getFormatedDate($date_submitted_finance) : ""; ?>" placeholder="DD-MM-YYYY" autocomplete="off">
							</div>
							<div class="col-md-4">
								<label for="paid Week Number" class="form-label">Paid Week Number</label>
								<input type="number" class="form-control" name="paid_week_num" id="paid_week_num" value="<?php echo $paid_week_num;?>" placeholder="Paid Week Number">
							</div>
						</div>
					</div>

					<div class="card-footer">
						<input type="hidden" id="id" name="id" value="<?php echo $id; ?>">
						<input type="hidden" id="from" name="from" value="<?php echo $from; ?>">
						<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>
$(document).ready(function(){
	$("#employee_name_id").prop("disabled", true);
	$("#clock_no_id").prop("disabled", true);
	$("#is_authorised").prop("disabled", true);
	$("#holiday_authorised_by").prop("disabled", true);
	$("#holiday_auth_date_time").prop("disabled", true);
	$('#form1').submit(function(){
		$("#submit").attr("disabled", true);
		$("#employee_name_id").prop("disabled", false);
		$("#clock_no_id").prop("disabled", false);
		$("#holiday_auth_date_time").prop("disabled", false);
		$("#is_authorised").prop("disabled", false);
		$("#holiday_authorised_by").prop("disabled", false);
		$('#page_load_div').show();
	});
	
});
	function calculateDays() {
		var startDate = document.getElementById("leave_holiday_start_date").value;
		var endDate = document.getElementById("leave_holiday_end_date").value;
		var startDate = moment(startDate, 'DD-MM-YYYY');
        var endDate = moment(endDate, 'DD-MM-YYYY');
		const noOfDaysField = document.getElementById("no_of_day");
		if (startDate && endDate) {
			const start = new Date(startDate);
			const end = new Date(endDate);
			const timeDiff = end - start;

			if (timeDiff >= 0) {
				const days = Math.ceil(timeDiff / (1000 * 60 * 60 * 24)) + 1; // Include start and end dates
				noOfDaysField.value = days;
			} else {
				noOfDaysField.value = 0;
			}
		} else {
			noOfDaysField.value = '';
		}
	}
    $('#leave_holiday_start_date, #leave_holiday_end_date').change(function() {
	calculateDays();
	});
</script>