<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_technician
*/
include('includes/imgFunctions.php');
include('includes/functions.php'); # general functions



$technicianId = trim($_REQUEST['tech_id']);

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

//get tech location
$techLocRs = mysqli_query($con, "SELECT * FROM tbl_technician_location WHERE technician_id = '".$technicianId."'");
if(mysqli_num_rows($techLocRs)>0){
	$techLocRec = mysqli_fetch_array($techLocRs);
}

if(isset($_POST['add'])){	
	
	if(isset($_POST['radius']) && $_POST['radius']!=''){
		$radius = trim(mysqli_real_escape_string($con, $_POST['radius']));
	} else {
		$error = 'Please enter the available radius in Km';
	}
	
	if(isset($_POST['place']) && $_POST['place']!=''){
		$map_place = trim(mysqli_real_escape_string($con, $_POST['place']));
	} else {
		$error = 'Did you select your place from the google suggest? If not please do';
	}
	
	if(isset($_POST['place']) && $_POST['place']!=''){
		$place = trim(mysqli_real_escape_string($con, $_POST['place']));
	} else {
		$error = 'Please enter your place';
	}
		
	if(strlen(trim($_POST['radius'])) > 0) {
		$radius = trim($_POST['radius']);
		if(is_numeric($radius) && $radius>0 && $radius<=50){
			//ok
		} else {
			$error = 'Please enter a numeric value between 0 to 50 for radius';
		}
	} else {
		$error = 'Please enter available radius in Kilometeres';
	}
	
	
	if(strlen(trim($_POST['place'])) > 0) {
		$place = trim($_POST['place']);		
		//$placeArr = explode(',', $place);
		//$place = $placeArr[0];
	} else {
		$error = 'Please enter location';
	}
	
	if(empty($error)){
		
		//get lat, long from the map place
		$latLong = getLatLong($map_place);			
		if(is_array($latLong) && $latLong['latitude']!='' && $latLong['longitude']!=''){			
			$latitude = $latLong['latitude'];
			$longitude = $latLong['longitude'];
		} else {
			$error = 'Unable to find lat-long from the location - '.$map_place.'. Try again';
		}
		
		$placeName = '';
		$placeArr = explode(',', $place);
		$placeName = $placeArr[0];
		//echo "SELECT id FROM tbl_technician_location WHERE technician_id = '".$technicianId."'";
		//insert the location details if no recs added else update the existing
		$techLocationRs = mysqli_query($con, "SELECT id FROM tbl_technician_location WHERE technician_id = '".$technicianId."'");
		if(mysqli_fetch_array($techLocationRs)>0){
			$updateLoc = mysqli_query($con, "UPDATE tbl_technician_location SET technician_id = '".$technicianId."', location = '".$place."', place = '".$placeName."', latitude = '".$latitude."', longitude = '".$longitude."', radius = '".$radius."' WHERE technician_id = '".$technicianId."'");
			if($updateLoc){
				$success = 'Your availability settings has been updated';
			}
		} else {
			//echo "INSERT INTO tbl_technician_location (technician_id, location, place, latitude, longitude, radius) VALUES ('".$technicianId."', '".$place."', '".$placeName."', '".$latitude."', '".$longitude."', '".$radius."')";
			//$insertLoc = mysqli_query($con, "INSERT INTO tbl_technician_location (technician_id, location, latitude, longitude, radius) VALUES ('".$technicianId."', '".$place."', '".$latitude."', '".$longitude."', '".$radius."')");
			if($placeName!=''){
				$insertLoc = mysqli_query($con, "INSERT INTO tbl_technician_location (technician_id, location, place, latitude, longitude, radius) VALUES ('".$technicianId."', '".$place."', '".$placeName."', '".$latitude."', '".$longitude."', '".$radius."')");
			} else {
				$insertLoc = mysqli_query($con, "INSERT INTO tbl_technician_location (technician_id, location, latitude, longitude, radius) VALUES ('".$technicianId."', '".$place."', '".$latitude."', '".$longitude."', '".$radius."')");
			}
			if($insertLoc){
				$success = 'Your availability settings has been updated';
			}
		}
		
	}
	
	
}

$techLocationRs = mysqli_query($con, "SELECT * FROM tbl_technician_location WHERE technician_id = '".$technicianId."'");
if(mysqli_num_rows($techLocationRs)>0){
	$techLocation = mysqli_fetch_array($techLocationRs);	
}


?>