<?php
/*
* date last modified 03-10-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - manage_staff.html.php
* Mysql Tables - tbl_admin, tbl_user, tbl_admin_type
*/
require_once('./classes/class.noseo_pagination.php'); # pagination class
require_once('./includes/functions.php'); # general functions

if(isset($_REQUEST['p']) && $_REQUEST['p']!=''){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

//get cities
$cityRs = mysqli_query($con, "SELECT id, name FROM tbl_city ORDER BY name");

if(isset($_GET['action']) && $_GET['action'] == 'delete') {
	/* Delete User */
	if(isset($_GET['del_id']) && $_GET['del_id']!=''){
		//$delete = mysqli_query($con,'DELETE FROM tbl_user WHERE id='.$_GET['del_id']);
		$delUserId = $_GET['del_id'];
		$delete = mysqli_query($con, "UPDATE tbl_user SET status = 'Deleted' WHERE id = '".$delUserId."'");
		if($delete) {
			//remove all un assigned extra tech requests from tasks of this user
			$userTaskRs = mysqli_query($con, "SELECT id FROM tbl_job WHERE user_id = '".$delUserId."'");
			while($taskRec = mysqli_fetch_array($userTaskRs)){
				$userTaskId = $taskRec['id'];
				mysqli_query($con, "DELETE FROM tbl_job_extra_tech WHERE job_id = '".$userTaskId."' AND technician_id = '0'");				
			}
			
			$success = 'Deleted the user';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'disable') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_user SET status = 'Disabled' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'Disabled the user';	?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&p=<?php echo $p; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'activate') {	
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_user SET status = 'Active' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'Activated the user';	?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&p=<?php echo $p; ?>';
			</script>
	        <?php
		}
	}
}



$pageVars = '';

// Get All User details to list 
$where = ' status != "Deleted" ';

if(isset($_POST['clear'])) { ?>
<script language="javascript" type="text/javascript">			
	window.location='index.php?page=<?php echo $page;?>';
</script>
<?php }
		
if(isset($_REQUEST['searchvalue']) && strlen($_REQUEST['searchvalue'])>0) {
	$keyword = trim($_POST['searchvalue']);
	$where.= " AND (name LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR phone LIKE '%".$keyword."%' OR place LIKE '%".$keyword."%' OR FIND_IN_SET('".$keyword."',`additional_numbers`)) ";
	$pageVars .=  "&searchvalue=".$keyword."&"; 
}

if(isset($_REQUEST['device']) && strlen($_REQUEST['device'])>0) {
	$device = trim($_REQUEST['device']);
	$where.= " AND id IN (SELECT DISTINCT user_id FROM tbl_fcm_tokens WHERE user_id > 0 AND device = '".$device."') ";
	$pageVars .=  "&device=".$device."&"; 
}	

if(isset($_REQUEST['city']) && strlen($_REQUEST['city'])>0) {
	$city = trim($_REQUEST['city']);
	$where.= " AND city = '".$city."' ";
	$pageVars .=  "&city=".$city."&"; 
}

if(isset($_REQUEST['booked']) && strlen($_REQUEST['booked'])>0) {
	$booked = trim($_REQUEST['booked']);
	if($booked=='yes'){
		$where.= " AND id IN (SELECT DISTINCT user_id FROM tbl_job) ";
	} else if($booked=='no'){
		$where.= " AND id NOT IN (SELECT DISTINCT user_id FROM tbl_job) ";
	}	
	$pageVars .=  "&booked=".$booked."&"; 
}

if(isset($_REQUEST['ref_id']) && strlen($_REQUEST['ref_id'])>0) {
	$refId = trim($_REQUEST['ref_id']);
	$where.= " AND ref_id = '".$refId."' ";
	$pageVars .=  "&ref_id=".$refId."&"; 
}

$select = "SELECT * FROM tbl_user WHERE ".$where." ORDER BY id DESC";
//echo $select;
// Using pagination class
$pagination = new pagination_noseo($select);
$execDetails = $pagination->createPages(30);

$pageLnk = 'index.php?page='.$page.'&'.$pageVars;
$pageNums = $pagination->pageNumber(5, $pageLnk);
?>