<?php
$leadRec = mysqli_fetch_array(mysqli_query($con, 'SELECT technician_id, client_name, phone, location, details, date, status FROM tbl_new_leads WHERE id='.$_REQUEST['id']));
$edit_id = $_REQUEST['id'];

if(isset($_POST['update'])) {
	
	if(isset($_POST['details']) && $_POST['details']!=''){
		$details = mysqli_real_escape_string($con,trim($_POST['details']));		
	} else {
		$error = 'Please enter details';
	}
	
	if(isset($_POST['location']) && $_POST['location']!=''){
		$location = mysqli_real_escape_string($con,trim($_POST['location']));		
		$placeArr = explode(',', $location);
		$location = $placeArr[0];
	} else {
		$error = 'Please enter client location';
	}
	
	if(isset($_POST['phone']) && $_POST['phone']!=''){
		$phone = mysqli_real_escape_string($con,trim($_POST['phone']));		
		if(is_numeric($phone)){
			if(strlen($phone)>9 && strlen($phone)<12){
				//ok
			} else {
				$error = 'Please enter a valid phone number';
			}
		} else {
			$error = 'Please enter numeric digits only without spaces for phone';
		}
	} else {
		$error = 'Please enter client phone number';
	}
	
	if(isset($_POST['client_name']) && $_POST['client_name']!=''){
		$client_name = mysqli_real_escape_string($con,trim($_POST['client_name']));		
	} else {
		$error = 'Please enter client name';
	}
	
	if(empty($error)){
		
		$update = mysqli_query($con, "UPDATE tbl_new_leads SET client_name =  '".$client_name."',
														   phone = '".$phone."',
														   location =  '".$location."',
														   details = '".$details."' WHERE id=".$edit_id);
		if($update){
			$success = 'Lead has been added successfully';		
			header("Location: index.php?page=tech_leads_edit&success=".$success.'&id='.$edit_id);
			exit();
		}
	}
}
?>