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


$debug = false;
$isEdit = FALSE;

if(isset($_REQUEST['tech_id']) && $_REQUEST['tech_id']!=''){
	//ok
	$techId = trim($_REQUEST['tech_id']);
} else {
	$error = 'Something went wrong';	
	header('Location:index.php?page=manage_technician&error='.$success);
	exit();
}


if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){
	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));
	
	//delete image
	/*if(isset($_REQUEST['action']) && $_REQUEST['action']='del_image'){
		$recImg = mysqli_fetch_array(mysqli_query($con, "SELECT image FROM tbl_user WHERE id = '".$editId."'"));
		if(($recImg['image']!='') && file_exists(STAFF_IMG_FOLDER.$recImg['image'])){
			unlink(STAFF_IMG_FOLDER.$recImg['image']);
			unlink(STAFF_IMG_FOLDER.'thumb/'.$recImg['image']);
		}
		$update = mysqli_query($con, "UPDATE tbl_user SET image = '' WHERE id = '".$editId."'");
		$success = 'User image has been deleted';
	}*/	
	
	$editRs = mysqli_query($con, "SELECT * FROM tbl_user WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
	} else {
		$error = 'User does not exist!';
	}

} 

if(isset($_POST['submit'])) {
	
	if(strlen(trim($_POST['radius'])) > 0) {
		$radius = trim($_POST['radius']);
	} else {
		$error = 'Please enter radius';
	}
		
	if(strlen(trim($_POST['location'])) > 0) {
		$location = trim($_POST['location']);
	} else {
		$error = 'Please enter location';
	}	
			
	if(empty($error)) {
		
		$googleAddr = array();
		$latitude = $longitude = '';				
		if($location!=''){
			$googleAddr[] = trim($location);
		}				
		$formattedAddress = implode(',',$googleAddr);		
		$latLong = getLatLong($formattedAddress);
		
		if(is_array($latLong) && $latLong['latitude']!='' && $latLong['longitude']!=''){
			//ok
			$latitude = $latLong['latitude'];
			$longitude = $latLong['longitude'];
		} else {
			$error = 'Unable to find lat-long from the location - '.$formattedAddress.'. Try again';
		}
		
		if(empty($error)) {						
			//check if exists
			$locArr = explode(',', $location);
			$locationDb = trim($locArr[0]);
			
			$chkRs = mysqli_query($con, "SELECT id FROM tbl_technician_operator WHERE location = '".$locationDb."'");
			if(mysqli_num_rows($chkRs)>0){
				$error = 'Technician already added for the place';
			} else {
				$insertQry = "INSERT INTO tbl_technician_operator(technician_id, location, latitude, longitude, radius) VALUES (
				'".mysqli_real_escape_string($con, $techId)."',
				 '".mysqli_real_escape_string($con, $locationDb)."',
				 '".mysqli_real_escape_string($con, $latitude)."',
				 '".mysqli_real_escape_string($con, $longitude)."',
				 '".mysqli_real_escape_string($con, $radius)."')";
				
				$execInsert = mysqli_query($con,$insertQry);					
				if($execInsert) {
					$insertId = mysqli_insert_id($con);
					mysqli_query($con, "UPDATE tbl_technician SET operator = 'yes' WHERE id = '".$techId."'");										
					$success = 'Technician Assigned for Location';				
				} else {
					$error = 'Unable to add technician as operator';
				}				
			}
					
		}		
	} 
}



?>