<?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 = true;

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

//get all categories
$categoryRs = mysqli_query($con, "SELECT id, name FROM tbl_service_categories WHERE parent_id = '0' AND status = 'Active'");
 
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."' ";	
		} 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;
}

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

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

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 `user_id` FROM `tbl_job` WHERE DATE(date) BETWEEN '".$workFrom."' AND '".$workTo."') ";
	}*/
	
	if(isset($_REQUEST['category']) && strlen($_REQUEST['category'])>0) {
		$cat_id = trim($_REQUEST['category']);
		$where .= " AND id IN(SELECT partner_id FROM `tbl_purchase_partner_categories` WHERE categories IN(".$cat_id.")) ";	
		//$pageVars.= '&category='.$techCatId.'&';
	}
	
}



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

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

$select = "SELECT * FROM tbl_purchase_partner WHERE ".$where." AND latitude != '' AND longitude != '' ORDER BY id";
$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['building_name'];
		$userArray[$i]['latitude'] = $userRec['latitude'];
		$userArray[$i]['longitude'] = $userRec['longitude'];
		$userArray[$i]['phone'] = $userRec['phone'];
		$i++;
	}
}

//print_r($userArray);
?>