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

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



if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){		
	$leadsId = mysqli_real_escape_string($con, trim($_REQUEST['id']));	
		
	if(isset($_POST['submit_note'])){
		
		if(isset($_POST['delivery_date']) && strlen($_POST['delivery_date'])>0) {
			$deliveryDate = trim($_POST['delivery_date']);
		} else {
			$deliveryDate = '';
		}
		
		if(isset($_POST['note']) && $_POST['note']!=''){
			$note = addslashes(trim($_POST['note']));			
		} else {
			$error = 'Note should not be empty';
		}
		
		if(empty($error)){
			$curDate = date('Y-m-d H:i:s');
			$adminId = $_SESSION['adminId'];
			$insertNote = mysqli_query($con, "INSERT INTO tbl_leads_notes (leads_id, note, date, delivery_date, added_by) VALUES ('".$leadsId."', '".$note."', '".$curDate."', '".$deliveryDate."', '".$adminId."')");
			if($insertNote){
				if($deliveryDate!='' && $deliveryDate!='0000-00-00'){
					mysqli_query($con, "UPDATE tbl_new_leads SET delivery_date = '".$deliveryDate."' WHERE id = '".$leadsId."'");
				}
				$success = 'Notes added for the leads';
				header('Location:index.php?page=leads_note&id='.$leadsId.'&p='.$p.'&success='.$success.'#add_note');
				exit();
			}
		}
	}	
	
	//get customer details and dedcription
	$leadsRec = mysqli_fetch_array(mysqli_query($con, "SELECT client_name, phone, location, details, assignee FROM tbl_new_leads WHERE id = '".$leadsId."'"));
		
	//get notes if any
	$taskNotesRs = mysqli_query($con, "SELECT * FROM tbl_leads_notes WHERE leads_id = '".$leadsId."' ORDER BY id DESC");
	
} else {
	header('Location:index.php?page=tech_leads');
	exit();
}   



?>