<?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('./includes/functions.php'); # general functions
$debug = false;

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

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

// Get All User details to list 
//$where = " latitude != '' AND longitude != '' ";
$where = " id > 0 ";
$regFrom = $regTo = $workFrom = $workTo = $error = '';

if(isset($_POST['place']) && $_POST['place']!=''){			
		$location = trim($_POST['place']);
		$latLong = getLatLong($location);
		if(is_array($latLong) && $latLong['latitude']!='' && $latLong['longitude']!=''){
			//ok
			$latitude = $latLong['latitude'];
			$longitude = $latLong['longitude'];
			$locArray = explode(',', $location);
			$place = trim($locArray[0]);
			//$where.= " AND place = '".$place."' ";
			$where.= " AND id IN (SELECT technician_id FROM tbl_technician_location WHERE location LiKE '".$place."%') ";			
		} else {
			$error = 'Unable to find lat-long from the location - '.$formattedAddress.'. Try again';
		}		
} else if(isset($_POST['city']) && $_POST['city']!=''){
	$city = trim($_POST['city']);
	$cityRec = mysqli_fetch_array(mysqli_query($con, "SELECT latitude, longitude FROM tbl_city WHERE id = '".$city."'"));
	$latitude = $cityRec['latitude'];
	$longitude = $cityRec['longitude'];
} else {
	$latitude = 8.5241391;
	$longitude = 76.9366376;
}


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


if(isset($_POST['search'])){
	//print_r($_POST); exit;
	if(isset($_REQUEST['reg_from']) && $_REQUEST['reg_from']!=''){
		$regFrom = trim($_REQUEST['reg_from']);		
	} 
	if(isset($_REQUEST['reg_to']) && $_REQUEST['reg_to']!=''){
		$regTo = trim($_REQUEST['reg_to']);		
	} else {
		$regTo = date('Y-m-d');
	}
	
	if(isset($_REQUEST['work_from']) && $_REQUEST['work_from']!=''){
		$workFrom = trim($_REQUEST['work_from']);		
	} 
	if(isset($_REQUEST['work_to']) && $_REQUEST['work_to']!=''){
		$workTo = trim($_REQUEST['work_to']);		
	} else {
		$workTo = date('Y-m-d');
	}
	
	if($regFrom!='' && $regTo!=''){
		$where.= " AND DATE(date) BETWEEN '".$regFrom."' AND '".$regTo."' ";
	}
	if($workFrom!='' && $workTo!=''){
		$where.= " AND id IN(SELECT `technician_id` FROM `tbl_job` WHERE DATE(date) BETWEEN '".$workFrom."' AND '".$workTo."') ";
	}
}

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['keyword']) && strlen($_REQUEST['keyword'])>0) {
	$keyword = trim($_REQUEST['keyword']);
	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 `mobile2` LIKE '%".$keyword."%') ";
	}
			
}

$userArray = array();
$i = 0;

if(empty($error)){
	//ok
} else {
	$latitude = 8.5241391;
	$longitude = 76.9366376;
}

if(isset($_REQUEST['map_type']) && $_REQUEST['map_type']!=''){
	$mapType = trim($_REQUEST['map_type']);		
} else {
	$mapType = 'service_area';
}

if($mapType == 'service_area'){
	
} else {
	$where.= " AND id IN (SELECT technician_id FROM `tbl_update_location` WHERE time >= DATE_SUB(NOW(),INTERVAL 1 HOUR)) ";
}

$where .= " AND `status` != 'Deleted' ";

//echo $where;

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

/*if($_SERVER['REMOTE_ADDR']=='202.88.237.77'){
echo $select;
}*/
$userRs = mysqli_query($con, $select);
if(mysqli_num_rows($userRs)>0){
	while($userRec = mysqli_fetch_array($userRs)){
		$userArray[$i]['id'] = $userRec['id'];
		$userArray[$i]['name'] = $userRec['name'];
		
		$userArray[$i]['phone'] = $userRec['phone'];
		if($mapType == 'service_area'){
			//check if available location added
			$availLocRs = mysqli_query($con, "SELECT latitude, longitude FROM tbl_technician_location WHERE technician_id = '".$userRec['id']."'");
			if(mysqli_num_rows($availLocRs)>0){
				$availLocRec = mysqli_fetch_array($availLocRs);
				//if($availLocRec['latitude']!='' && $availLocRec['longitude']!=''){
					$userArray[$i]['latitude'] = $availLocRec['latitude'];
					$userArray[$i]['longitude'] = $availLocRec['longitude'];
				//}				
			} else {
				$userArray[$i]['latitude'] = $userRec['latitude'];
				$userArray[$i]['longitude'] = $userRec['longitude'];
			}
			if(isOperator($userRec['id'])){
				$mapIcon = 'map_icon_tech.png';
			} else if(isPaidTech($userRec['id'])) {
				$mapIcon = 'map_icon_tech_paid.png';
			} else {
				$mapIcon = 'map_icon_tech_operator.png';
			}
			$userArray[$i]['icon'] = $mapIcon;
		} else {
			$locRs = mysqli_query($con, "SELECT latitude, longitude FROM tbl_update_location WHERE technician_id = '".$userRec['id']."' ORDER BY id DESC LIMIT 1");
			if(mysqli_num_rows($locRs)>0){
				$locRec = mysqli_fetch_array($locRs);
				$userArray[$i]['latitude'] = $locRec['latitude'];
				$userArray[$i]['longitude'] = $locRec['longitude'];
			} else {
				$userArray[$i]['latitude'] = $userRec['latitude'];
				$userArray[$i]['longitude'] = $userRec['longitude'];
			}
			if(isOperator($userRec['id'])){
				$mapIcon = 'map_icon_tech.png';
			} else {
				$mapIcon = 'map_icon_tech_operator.png';
			}
			$userArray[$i]['icon'] = $mapIcon;
		}		
		$i++;
	}
}

//print_r($userArray);
?>