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


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

if(isset($_REQUEST['aa']) && $_REQUEST['aa']=='test'){
	/*$phone = '9895518292';
	$smsMessage = 'Test msg trans';
	$smsMessage = urlencode($smsMessage);
	$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$phone.'&message='.$smsMessage.'&sender=LiiMRT&type=1&route=2');
	//$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$phone.'&message='.$smsMessage.'&sender=LiiMRT&type=1&route=2');
	//$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$phone.'&message='.$smsMessage.'&sender=LiiMRP&type=1&route=3');
	$smsRes = json_decode($smsRes); 
	if($_SERVER['REMOTE_ADDR']=='202.88.237.77'){
		print_r($smsRes);
	}*/
	
	
	/*$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE tech_id=231');
				if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {
					while($res_token = mysqli_fetch_array($exec_token)) {
						if($res_token['fcm_token']!='') {
							$title = 'Thanks for registering with us.';
							$body = 'Your profile has been activated. For any help and information please contact at 933 933 4 933.';
							echo $res_token['device'];
							if($res_token['device']=='android') {																
								sendGCM_TechActivate($title, $body, $res_token['fcm_token'], "tech");
							} elseif($res_token['device']=='ios') {								
								//sendIosTechAssignPush($title, $body, $res_token['fcm_token'], "tech", $taskId, "task", $taskRec['user_id'], $userName, $techId, $techName);
							}
						}
					}
				}*/
	
	
}

$adminDispatchCityString = '';
$dispatchArray = array();
//get admin dispatch cities 
$adminDispatchRs = mysqli_query($con, "SELECT id FROM tbl_city WHERE dispatch_type = 'admin'");
if(mysqli_num_rows($adminDispatchRs)>0){
	while($adminDispatchRec = mysqli_fetch_array($adminDispatchRs)){
		$dispatchArray[] = $adminDispatchRec['id'];
	}	
}
if(count($dispatchArray)>0){
	$adminDispatchCityString = implode(',', $dispatchArray);
}

//get all categories
$categoryRs = mysqli_query($con, "SELECT id, name FROM tbl_service_categories WHERE parent_id = '0' AND status = 'Active'");

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

if(isset($_GET['action']) && $_GET['action'] == 'make_normal') {
	/* Depromote  */
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_technician SET operator = '' WHERE id='".$_GET['id']."'");
		//delete entry in tbl_technician_operator
		mysqli_query($con, "DELETE FROM tbl_technician_operator WHERE technician_id = '".$_GET['id']."'");
		mysqli_query($con, "DELETE FROM tbl_operator_locations WHERE operator_id = '".$_GET['id']."'");
		if($update) {
			$success = 'Changed the technician to normal'; ?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&p=<?php echo $p;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'make_operator') {
	/* Promote  */
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_technician SET operator = 'yes' WHERE id='".$_GET['id']."'");
		
		if($update) {
			$success = 'Promoted the technician to operator'; ?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&p=<?php echo $p;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'activate') {
	/* Activate User */
	if(isset($_GET['id']) && $_GET['id']!=''){
		$techId = mysqli_real_escape_string($con, trim($_GET['id']));	
		$techPhoneRec = mysqli_fetch_array(mysqli_query($con, "SELECT phone, status FROM tbl_technician WHERE id = '".$techId."'"));
		$techPhone = $techPhoneRec['phone'];
		$techStatus = $techPhoneRec['status'];	
		$update = mysqli_query($con,"UPDATE tbl_technician SET status = 'Active' WHERE id='".$techId."'");
		if($update) {
			//send sms to tech if pending
			//if($techStatus == 'Pending'){				
				$smsMessage = urlencode('Thanks for registering with us. Your profile has been activated. Please log-out and log-in again. For any help and information please contact at 933 933 4 933.');						
				//$smsRes = file_get_contents('http://roundsms.com/api/sendhttp.php?authkey=Nzk3YWJkY2VmMWU&mobiles='.$techPhone.'&message='.$smsMessage.'&sender=LiiMRP&type=1&route=3'); 
				//$smsRes = json_decode($smsRes);
				sendSMSP($techPhone, $smsMessage);
				
				//send push
				$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE tech_id='.$techId);
				if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {
					while($res_token = mysqli_fetch_array($exec_token)) {
						if($res_token['fcm_token']!='') {
							$title = 'Thanks for registering with us.';
							$body = 'Your profile has been activated. For any help and information please contact at 933 933 4 933.';
							if($res_token['device']=='android') {																
								sendGCM_TechActivate($title, $body, $res_token['fcm_token'], "tech");
							} elseif($res_token['device']=='ios') {
								echo $res_token['device']; 								
								iosTechActivate($title, $body, $res_token['fcm_token'], "tech");
							}
						}
					}
				}
				
				
			//}
			
			$success = 'Activated the technician'; ?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&p=<?php echo $p;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'disable') {
	/* Disable User */
	if(isset($_GET['id']) && $_GET['id']!=''){
		$update = mysqli_query($con,"UPDATE tbl_technician SET status = 'Disabled' WHERE id='".$_GET['id']."'");
		if($update) {
			$success = 'Disabled the technician'; ?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&p=<?php echo $p;?>&success=<?php echo $success; ?>';
			</script>
	        <?php
		}
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'delete') {
	/* Delete User */
	if(isset($_GET['del_id']) && $_GET['del_id']!=''){
		$delId = trim($_GET['del_id']);
		//delete all foreign key related recs
		//mysqli_query($con, "DELETE FROM tbl_technician_category WHERE technician_id = '".$delId."'");		
		
		//$delete = mysqli_query($con,'DELETE FROM tbl_technician WHERE id='.$delId);		
		$update = mysqli_query($con,"UPDATE tbl_technician SET status = 'Deleted' WHERE id='".$_GET['del_id']."'");
		if($update) {
			$success = 'Deleted the technician';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&p=<?php echo $p;?>&success=<?php echo $success; ?>';
			</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($_POST['category']) && strlen($_POST['category'])>0) {
	//$where .= ' AND role='.$_POST['category'];
	$category = $_POST['category'];
}*/
		
if(isset($_REQUEST['searchvalue']) && strlen($_REQUEST['searchvalue'])>0) {
	$keyword = trim($_REQUEST['searchvalue']);
	if (strpos($keyword, TECH_ID) !== false) {
	    $techId = str_replace(TECH_ID,'',$keyword);
		$where.= " AND id = '".$techId."' ";
	} else {
		$where.= " AND (name LIKE '%".$keyword."%' OR email LIKE '%".$keyword."%' OR phone LIKE '%".$keyword."%' OR place LIKE '%".$keyword."%' OR mobile2 LIKE '%".$keyword."%' ) ";
	}	
	$pageVars.= "&searchvalue=".$keyword.'&';
}	

if(isset($_REQUEST['dispatch_type']) && strlen($_REQUEST['dispatch_type'])>0) {
	$dispatchType = trim($_REQUEST['dispatch_type']);	
	if($dispatchType == 'admin'){
		$where.= " AND city IN (SELECT id FROM tbl_city WHERE dispatch_type = 'admin') ";
	} else {
		$where.= " AND city IN (SELECT id FROM tbl_city WHERE dispatch_type = 'direct') ";
	}				
	$pageVars.= "&dispatch_type=".$dispatchType.'&';
}

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

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

if(isset($_REQUEST['optype']) && strlen($_REQUEST['optype'])>0) {
	$opType = trim($_REQUEST['optype']);
	if($opType == 'yes'){
		$where.= " AND operator = 'yes' ";
	} else {
		$where.= " AND operator != 'yes' ";
	}	
	$pageVars.= "&optype=".$opType.'&';
}

if(isset($_REQUEST['cattype']) && strlen($_REQUEST['cattype'])>0) {
	$catType = trim($_REQUEST['cattype']);
	if($catType == 'yes'){
		$where.= " AND id IN (SELECT DISTINCT technician_id FROM tbl_technician_category) ";
	} else {
		$where.= " AND id NOT IN (SELECT DISTINCT technician_id FROM tbl_technician_category) ";
	}	
	$pageVars.= "&cattype=".$catType.'&';
}

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

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

if(isset($_REQUEST['category']) && strlen($_REQUEST['category'])>0) {
	$techCatId = trim($_REQUEST['category']);
	$where .= " AND id IN (SELECT `technician_id` FROM `tbl_technician_category` WHERE `category_id` = '".$techCatId."' AND `verified` = 'yes') ";	
	$pageVars.= '&category='.$techCatId.'&';
}

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

if(isset($_REQUEST['license']) && strlen($_REQUEST['license'])>0) {
	$license = trim($_REQUEST['license']);	
	if($license == 'yes'){
		if(isset($_REQUEST['license_cat']) && strlen($_REQUEST['license_cat'])>0) {
			$licenseCat = trim($_REQUEST['license_cat']);
			$where.= " AND licensed = 'yes' AND id IN (SELECT tech_id FROM tbl_tech_licensed WHERE cat_id = '".$licenseCat."') ";
			$pageVars.= "&license=".$license.'&license_cat='.$licenseCat.'&';
		} else {
			$where.= " AND licensed = 'yes' ";
			$pageVars.= "&license=".$license.'&';
		}		
	} else {		
		$where.= " AND licensed != 'yes' ";		
		$pageVars.= "&license=".$license.'&';
	}					
}


if(isset($_REQUEST['operator_id']) && $_REQUEST['operator_id']!=''){
	$operatorId = trim($_REQUEST['operator_id']);
	//$where.= " AND operator_id = '".$operatorId."' ";	
	//get sub techs from location
	//$where.= " AND place = '".$operatorId."' ";	
	$where.= " AND `place` IN (SELECT `place` FROM tbl_operator_locations WHERE operator_id = '".$operatorId."') AND id != '".$operatorId."' ";
}

$select = "SELECT * FROM tbl_technician WHERE ".$where." ORDER BY id DESC";

/*if(isset($_REQUEST['aa']) && $_REQUEST['aa']=='test'){
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);
?>