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

$isEdit = FALSE;
$debug = true;
$curDate = date('Y-m-d H:i:s');
$adminId = $_SESSION['adminId'];

if(isset($_REQUEST['p'])){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

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

} 

if(isset($_POST['add'])) {
		
	if(strlen(trim($_POST['details'])) > 0) {
		$details = trim($_POST['details']);
	} else {
		$error = 'Enter the details';
	}	
		
	if(isset($_POST['client_location']) && strlen($_POST['client_location'])>0) {
		$client_location = trim($_POST['client_location']);
		//$placeArr = explode(',', $client_location);
		//$client_location = $placeArr[0];
	} else {
		$client_location = '';
	}
	
	if(isset($_POST['client_phone']) && strlen($_POST['client_phone'])>0) {
		$client_phone = trim($_POST['client_phone']);
		if(is_numeric($client_phone) && strlen($client_phone)>7 && strlen($client_phone)<13){
			//ok
		} else {
			$error = 'Enter a valid phone number without spaces and special character';
		}
	} else {
		$client_phone = '';
	}
	
	if(isset($_POST['client_name']) && strlen($_POST['client_name'])>0) {
		$client_name = trim($_POST['client_name']);
	} else {
		$error = 'Please enter client name';
	}	
		
	if(empty($error)) {
						
		/* Adding New staff Details */		
		$insertQry = "INSERT INTO tbl_new_leads(client_name, phone, location, details, admin_id, added_by, date, status) VALUES (
		'".mysqli_real_escape_string($con, $client_name)."',
		 '".mysqli_real_escape_string($con, $client_phone)."',
		 '".mysqli_real_escape_string($con, $client_location)."',
		 '".mysqli_real_escape_string($con, $details)."',
		 '".$adminId."',
		 'admin',
		 '".$curDate."',
		 'Pending')";
		
		$execInsert = mysqli_query($con,$insertQry);
		$insert_id = mysqli_insert_id($con);		
		
		if($execInsert) {
					
			$success = 'Leads Added Successfully';
			header('Location:index.php?page=tech_leads&p='.$p.'&success='.$success);
			exit();			
		}				
	} 
}



?>