<?php
session_start();
require_once ('../../config/config.inc.php');
require_once (DB);
$debug = false;

require_once ('authenticate.php');
require_once ('functions.php');
if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){
	$techId = trim($_REQUEST['id']);
} else { $errorMain = 'Something went wrong'; }

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';
				}				
			}
					
		}		
	} 
}

?>
<html>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<!--<script src="assets/plugins/jquery/jquery.min.js" ></script>-->
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css" rel="stylesheet"></link>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCDgFNMkZRmDv6tkxbKut5eIqaQbcM8zS8&sensor=true&libraries=places"></script>
<!---->
<script type="text/javascript" src="js/jquery.placepicker.js"></script>
<script>
$.noConflict();
      $(document).ready(function() {
        // Basic usage
        $(".placepicker").placepicker();
      }); // END document.ready
</script>
<style>
      .placepicker-map {
        width: 100%;
        height: 300px;
      }
      .another-map-class {
        width: 100%;
        height: 300px;
      }
      .pac-container {
        border-radius: 5px;
      }
</style>
<style>
.row{
	width:100%;
	margin-top:5px;
	margin-bottom:5px;
}
.row_left {
  float: left;
  padding: 5px;
  text-align: right;
  width: 40%;
}
.row_right{
	width:50%;
	padding: 5px;
	float:right;
}
</style>
<body>
	<div style="width:600px;height: 250px;">
		<?php if(empty($errorMain)){ ?>
		<?php if(isset($error)){ ?>
			<div style="color: red;"><?php echo $error; ?></div>
		<?php } else if(isset($success)){ ?>
			<div style="color: green;"><?php echo $success; ?></div>
		<?php }?>
		<form name="opForm" id="opForm" action="" method="post">
		<table width="100%">
			<tr>			
				<td colspan="2" align="center">		
					<b>Make Operator </b>
				</td>
			</tr>
		<tr class="row">
			<td class="row_left">		
				Technician
			</td>
			<td class="row_right">		
				<b><?php echo getTechnicianName($techId); ?></b>
			</td>
		</tr>
		<tr class="row">
			<td class="row_left">		
				Enter Location
			</td>
			<td class="row_right">		
				<input type="text" name="location" id="location" class="placepicker" value="<?php if(isset($_POST['location']) && $_POST['location']!=''){ echo $_POST['location']; }?>">
			</td>
		</tr>
		<tr class="row">
			<td class="row_left">		
				Radius (in Km)
			</td>
			<td class="row_right">		
				<input type="text" name="radius" id="radius" value="<?php if(isset($_POST['radius']) && $_POST['radius']!=''){ echo $_POST['radius']; }?>">
			</td>
		</tr>
		<tr style="text-align: center;" class="row">
			<td class="row_left">		
				&nbsp;
			</td>	
			<td class="row_right">		
				<input type="submit" name="submit" value="Submit">
			</td>									
		</tr>
		</table>
		</form>
		<?php } else { ?>
			<h3><?php echo $errorMain; ?></h3>
		<?php } ?>
	</div>
</body>
</html>