<?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

$isEdit = FALSE;
$debug = false;

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

} 

if(isset($_POST['add'])) {
	
	if(isset($_POST['radius']) && strlen($_POST['radius'])>0) {
		$radius = trim($_POST['radius']);
	} else {
		$error = 'Please enter Radius in KM';
	}		
	
	if(isset($_POST['place']) && strlen($_POST['place'])>0) {
		$place = trim($_POST['place']);
		$nameArr = explode(',', $place);
		$place = $nameArr[0];
	} else {
		$error = 'Please enter Location Name';
	}	
	
	
	if(empty($error)) {
		
		$googleAddr = array();
		$latitude = $longitude = '';
		
		//get lat long of the city		
		if($place!=''){
			$googleAddr[] = trim($place);
		}	
		$formattedAddress = implode(',',$googleAddr);		
		$latLong = getLatLong($formattedAddress);
		//print_r($latLong);
		//exit;
		
		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)) {
						
			/* Adding New staff Details */		
			$insertQry = "INSERT INTO tbl_city(name, latitude, longitude, radius) VALUES (
			'".mysqli_real_escape_string($con, $place)."',
			'".mysqli_real_escape_string($con, $latitude)."',
			'".mysqli_real_escape_string($con, $longitude)."',
			 '".mysqli_real_escape_string($con, $radius)."')";
			
			$execInsert = mysqli_query($con,$insertQry);
			$insert_id = mysqli_insert_id($con);		
			
			if($execInsert) {
							
				$success = 'Location Added Successfully';
				header('Location:index.php?page=manage_location&success='.$success);
				exit();			
			}	
		
		}			
	} 
}

if(isset($_POST['update'])) {
	
	if(isset($_POST['radius']) && strlen($_POST['radius'])>0) {
		$radius = trim($_POST['radius']);
	} else {
		$error = 'Please enter Radius in KM';
	}		
	
	if(isset($_POST['place']) && strlen($_POST['place'])>0) {
		$place = trim($_POST['place']);
		$nameArr = explode(',', $place);
		$place = $nameArr[0];
		// check Dup
			$check = mysqli_query($con,"SELECT id FROM tbl_city WHERE name='".mysqli_real_escape_string($con,$place)."' AND id != '".$editId."'");
			if(isset($checkEmail) && mysqli_num_rows($checkEmail)>0) {
				$error = 'Already Exists';
			}
	} else {
		$error = 'Please enter Location Name';
	}	
	
		
	if(empty($error)) {
		
		$googleAddr = array();
		$latitude = $longitude = '';
		
		//get lat long of the city		
		if($place!=''){
			$googleAddr[] = trim($place);
		}	
		$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)) {
			
			$updateQry = "UPDATE tbl_city SET name = '".$place."', latitude = '".$latitude."', longitude = '".$longitude."', radius = '".$radius."' WHERE id = '".$editId."'";			
			$execUpdate = mysqli_query($con,$updateQry);						
			if($execUpdate) {
				$success = 'Staff Updated Successfully';
				header('Location:index.php?page=manage_location&success='.$success);
				exit();			
			}		
		}		
	} 
}

?>