<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/
include('includes/functions.php'); # general functions
$advanceAmount = 0;

$staffRs = mysqli_query($con, "SELECT id, name FROM tbl_staff WHERE status = 'Active' ORDER BY name");

if(isset($_REQUEST['staff_id']) && $_REQUEST['staff_id']!=''){	
	$staffId = mysqli_real_escape_string($con, trim($_REQUEST['staff_id']));	
} else {
	header('Location:index.php?page=manage_staff');
	exit();
}


if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){
	$editId = trim($_REQUEST['id']);
	$editRs = mysqli_query($con, "SELECT * FROM tbl_staff_leaves WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$editRec = mysqli_fetch_array($editRs);
		$staffId = $editRec['staff_id'];
	}
}

if(isset($_POST['add'])) {
		
	if(isset($_POST['leave_type']) && strlen($_POST['leave_type'])>0) {
		$LeaveType = trim($_POST['leave_type']);
	} else {		
		$error = 'Please select leave type';
	}
		
	if(isset($_POST['status']) && strlen($_POST['status'])>0) {
		$status = trim($_POST['status']);
	} else {
		$error = 'Please select status';
	}
	
	if(isset($_POST['date']) && strlen($_POST['date'])>0) {
		$date = trim($_POST['date']);
	} else {
		$error = 'Please select date';
	}
	
	if(isset($_POST['staff']) && strlen($_POST['staff'])>0) {
		$staffId = trim($_POST['staff']);
	} else {
		$error = 'Please select a staff';
	}	
	
	if(empty($error)) {
		
		if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)) {
		    //ok - yyy-mm-dd
		} else {
		    $dateArr = explode('-', $date);
			$date = $dateArr[2].'-'.$dateArr[1].'-'.$dateArr[0];
		}
						
		//echo "SELECT id FROM tbl_staff_leaves WHERE staff_id = '".$staffId."' AND date = '".$date."'"; exit;		
		//check if already added for this staff
		$chkRs = mysqli_query($con, "SELECT id FROM tbl_staff_leaves WHERE staff_id = '".$staffId."' AND date = '".$date."'");
		if(mysqli_num_rows($chkRs)>0){
			$error = 'Leave already added for the date!';
		}
		
		if(empty($error)) {
			$insertQry = "INSERT INTO tbl_staff_leaves(staff_id, date, status, leave_type, added_on, added_by) VALUES (
			'".mysqli_real_escape_string($con, $staffId)."',
			 '".$date."',
			 '".mysqli_real_escape_string($con, $status)."',
			 '".mysqli_real_escape_string($con, $LeaveType)."',	NOW(), '".$_SESSION['adminId']."')";
			
			$execInsert = mysqli_query($con,$insertQry);
			$insert_id = mysqli_insert_id($con);		
			
			if($execInsert) {					
				$success = 'Leave Added Successfully';
				if(isset($_GET['staff_id']) && $_GET['staff_id']!=''){
					//went from manage staff page. so return back to there
					header('Location:index.php?page=manage_staff&success='.$success);
					exit();
				} else {
					if(isset($_GET['p']) && $_GET['p']!=''){
						$p = $_GET['p'];
						//went from leave report page. so return back to there
						header('Location:index.php?page=leave_report&p='.$p.'&success='.$success);
						exit();
					} else {						
						header('Location:index.php?page=add_leave&staff_id='.$staffId.'&success='.$success);
						exit();
					}						
				}								
			}			
		}	
						
	} 
}



if(isset($_POST['update'])) {
	
	//print_r($_POST);
		
	if(isset($_POST['leave_type']) && strlen($_POST['leave_type'])>0) {
		$LeaveType = trim($_POST['leave_type']);
	} else {		
		$error = 'Please select leave type';
	}
		
	if(isset($_POST['status']) && strlen($_POST['status'])>0) {
		$status = trim($_POST['status']);
	} else {
		$error = 'Please select status';
	}
	
	if(isset($_POST['date']) && strlen($_POST['date'])>0) {
		$date = trim($_POST['date']);
	} else {
		$error = 'Please select date';
	}
	
	if(isset($_POST['staff']) && strlen($_POST['staff'])>0) {
		$staffId = trim($_POST['staff']);
	} else {
		$error = 'Please select a staff';
	}	
	
	if(empty($error)) {
		if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)) {
		    //ok - yyy-mm-dd
		} else {
		    $dateArr = explode('-', $date);
			$date = $dateArr[2].'-'.$dateArr[1].'-'.$dateArr[0];
		}	
				
		$lastUpdatedBy = $_SESSION['adminId'];
		
		if(empty($error)) {
			/*echo "UPDATE tbl_staff_leaves SET staff_id = '".$staffId."', 
			date = '".$date."',
			status = '".mysqli_real_escape_string($con, $status)."',
			leave_type = '".mysqli_real_escape_string($con, $LeaveType)."',
			last_updated_by = '".$lastUpdatedBy."',
			last_updated_date = NOW() WHERE id = '".$editId."'";*/
			
			$update = mysqli_query($con, "UPDATE tbl_staff_leaves SET staff_id = '".$staffId."', 
			date = '".$date."',
			status = '".mysqli_real_escape_string($con, $status)."',
			leave_type = '".mysqli_real_escape_string($con, $LeaveType)."',
			last_updated_by = '".$lastUpdatedBy."',
			last_updated_date = NOW() WHERE id = '".$editId."'");
							
			if($update) {					
				$success = 'Leave Updated Successfully';
				if(isset($_GET['staff_id']) && $_GET['staff_id']!=''){
					header('Location:index.php?page=manage_staff&success='.$success);
					exit();
				} else {
					if(isset($_GET['p']) && $_GET['p']!=''){
						$p = $_GET['p'];
						//went from leave report page. so return back to there
						header('Location:index.php?page=leave_report&p='.$p.'&success='.$success);
						exit();
					} else {						
						header('Location:index.php?page=add_leave&staff_id='.$staffId.'&success='.$success);
						exit();
					}					
				}						
			}
			
		}	
						
	} 
}



?>