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

$isEdit = FALSE;
$debug = false;
$pageVars = '';


if(isset($_REQUEST['tech_id']) && $_REQUEST['tech_id']!=''){		
	$techId = mysqli_real_escape_string($con, trim($_REQUEST['tech_id']));	
	$techRec = mysqli_query($con, "SELECT name FROM tbl_technician WHERE id = '".$techId."'");	
} else {
	header('Location:index.php?page=manage_technician');
		exit();
}

if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){	
	$isEdit = TRUE;
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));	
	$editRs = mysqli_query($con, "SELECT * FROM tbl_operator_locations WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);
	} else {
		$error = 'Staff does not exist!';
	}
} 
 

if(isset($_REQUEST['del_id']) && $_REQUEST['del_id']!=''){		
	$delId = mysqli_real_escape_string($con, trim($_REQUEST['del_id']));	
	$delete = mysqli_query($con, "DELETE FROM tbl_operator_locations WHERE id = '".$delId."'");
	if($delete){
		$success = 'The location has been removed from the operator';		
		header('Location:index.php?page=operator_location&tech_id='.$techId.'&success='.$success);
		exit();		
	} else {
		$error = 'Staff does not exist!';
	}
}

if(isset($_POST['add'])) {
		
	if(isset($_POST['place']) && strlen($_POST['place'])>0) {
		$place = trim($_POST['place']);
		$placeArr = explode(',', $place);
		$place = $placeArr[0];	
		//check if the place already aded to any operator
		$chkRs = mysqli_query($con, "SELECT operator_id FROM tbl_operator_locations WHERE place = '".$place."'");
		if(mysqli_num_rows($chkRs)>0){
			//check if this operator
			$chkRec = mysqli_fetch_array($chkRs);
			if($chkRec['operator_id']==$techId){
				$error = 'The place already added for this operator';	
			} else {
				$error = 'The place already selected for another operator';	
			}			
		}
	} else {
		$error = 'Please enter place';
	}	
	
	
	if(empty($error)) {							
			
			$insertQry = "INSERT INTO tbl_operator_locations(operator_id, place) VALUES (
			'".mysqli_real_escape_string($con, $techId)."',
			'".mysqli_real_escape_string($con, $place)."')";
			
			$execInsert = mysqli_query($con,$insertQry);
			$insert_id = mysqli_insert_id($con);		
			
			if($execInsert) {
				
				if(isset($_GET['action']) && $_GET['action'] == 'make_operator') {					
					$update = mysqli_query($con,"UPDATE tbl_technician SET operator = 'yes' WHERE id='".$techId."'");
					if($update){
						//ok
					} else {
						$error = 'Unable to set the technician as operator';
					}
				}
				if(empty($error)){
					$success = 'Location Added Successfully';
					header('Location:index.php?page=operator_location&tech_id='.$techId.'&success='.$success);
					exit();	
				}						
			}	
			
	} 
}

if(isset($_POST['update'])) {
	
	if(isset($_POST['place']) && strlen($_POST['place'])>0) {
		$place = trim($_POST['place']);
		$placeArr = explode(',', $place);
		$place = $placeArr[0];	
		//check if the place already aded to any operator
		$chkRs = mysqli_query($con, "SELECT id FROM tbl_operator_locations WHERE place = '".$place."' AND operator_id != '".$techId."'");
		if(mysqli_num_rows($chkRs)>0){
			$error = 'The place already selected for another operator';	
		}
	} else {
		$error = 'Please enter place';
	}	
		
	if(empty($error)) {
		
			
			$updateQry = "UPDATE tbl_operator_locations SET place = '".$place."', radius = '".$radius."' WHERE id = '".$editId."'";			
			$execUpdate = mysqli_query($con,$updateQry);						
			if($execUpdate) {
				$success = 'Location Updated Successfully';
				header('Location:index.php?page=operator_location&tech_id='.$techId.'&success='.$success);
				exit();			
			}		
				
	} 
}

$select = "SELECT * FROM tbl_operator_locations WHERE operator_id = '".$techId."' ORDER BY id DESC";
$pagination = new pagination_noseo($select);
$execDetails = $pagination->createPages(30);

$pageLnk = 'index.php?page='.$page.'&'.$pageVars;
$pageNums = $pagination->pageNumber(5, $pageLnk);

?>