<?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/imgFunctions.php');
include('includes/functions.php'); # general functions


if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){		
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));	
	
	//disable
	if(isset($_REQUEST['action']) && $_REQUEST['action']=='disable'){		
		$update = mysqli_query($con, "UPDATE tbl_staff SET status = 'Disabled' WHERE id = '".$editId."'");
		if($update){
			$success = 'Staff has been disabled';
		}		
	}
	
	//enable
	if(isset($_REQUEST['action']) && $_REQUEST['action']=='enable'){		
		$update = mysqli_query($con, "UPDATE tbl_staff SET status = 'Active' WHERE id = '".$editId."'");
		if($update){
			$success = 'Status changed to active';
		}		
	}
	
	//delete image
	if(isset($_REQUEST['action']) && $_REQUEST['action']=='del_image'){
		$recImg = mysqli_fetch_array(mysqli_query($con, "SELECT image FROM tbl_staff WHERE id = '".$editId."'"));
		if(($recImg['image']!='') && file_exists(STAFF_IMG_FOLDER.$recImg['image'])){
			unlink(STAFF_IMG_FOLDER.$recImg['image']);
			unlink(STAFF_IMG_FOLDER.'thumb/'.$recImg['image']);
		}
		$update = mysqli_query($con, "UPDATE tbl_staff SET image = '' WHERE id = '".$editId."'");
		$success = 'Staff image has been deleted';
	}	
	
	$editRs = mysqli_query($con, "SELECT * FROM tbl_staff WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
		$curYear = date('Y');
		//get casual leaves taken on this year
		$casualCount = 0;		
		$casualRs = mysqli_query($con, "SELECT `status` FROM `tbl_staff_leaves` WHERE `staff_id` = '".$editId."' AND YEAR(date) = '".$curYear."' AND leave_type = 'casual'");
		if(mysqli_num_rows($casualRs)>0){
			while($casualRec = mysqli_fetch_array($casualRs)){
				if($casualRec['status'] == 'half_day'){
					$casualCount+=1;
				} else if($casualRec['status'] == 'full_day'){
					$casualCount+=1;
				}
			}
		}		
		//get sick leaves taken on this year
		$sickCount = 0;
		$sickRs = mysqli_query($con, "SELECT `status` FROM `tbl_staff_leaves` WHERE `staff_id` = '".$editId."' AND YEAR(date) = '".$curYear."' AND leave_type = 'sick'");
		if(mysqli_num_rows($sickRs)>0){
			while($sickRec = mysqli_fetch_array($sickRs)){
				if($sickRec['status'] == 'half_day'){
					$sickCount+=0.5;
				} else if($sickRec['status'] == 'full_day'){
					$sickCount+=1;
				}
			}
		}
		//get advance salary report
		$advanceRs = mysqli_query($con, "SELECT * FROM tbl_staff_advance WHERE staff_id = '".$editId."'ORDER BY id DESC");
		//get salary report
		$salaryRs = mysqli_query($con, "SELECT * FROM tbl_staff_salary WHERE staff_id = '".$editId."'ORDER BY id DESC");
				
	} else {
		$error = 'Staff does not exist!';
	}

} 


?>