<?php
include("../connect.php"); 
$response = array();
if(isset($_POST['a_name']) && isset($_POST['a_phone']) && isset($_POST['manager_id']) && isset($_POST['agent_code']))
{
	$ver = $_POST['manager_id'];
	$agent_id = $_POST['agent_id'];
	$a_name=$_POST['a_name'];
	$a_phone=$_POST['a_phone'];
	$agent_code=$_POST['agent_code'];
	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d")." ".date('h:i:sa');
	$res_product_stock = mysqli_query($con,"SELECT agent_name FROM  tbl_agent where agent_phone='$a_phone' and agent_id!='$agent_id'");
	if(mysqli_num_rows($res_product_stock)<=0)
	{	
		$res_customer = mysqli_query($con,"SELECT * FROM  tbl_agent where agent_id='$agent_id'");
		while($fetch_detail=mysqli_fetch_assoc($res_customer))
		{
			$query = mysqli_query($con,"INSERT INTO `tbl_agent_history`(`agent_id`,`agent_name`, `agent_phone`, `agent_code`, `manager_id`, `status`, `insert_datetime`,`update_by`) VALUES ('$agent_id','".$fetch_detail['agent_name']."','".$fetch_detail['agent_phone']."','".$fetch_detail['agent_code']."','".$fetch_detail['manager_id']."','".$fetch_detail['status']."','$cdate','$ver')");	
			
			$uq2=mysqli_query($con,"update tbl_agent set agent_name='$a_name',agent_phone='$a_phone',agent_code='$agent_code',update_datetime='$cdate' where agent_id='$agent_id'"); 
			
			$response['success']='1';
			$response['details']="Update Successful";	
		}			
	}
	else
	{
		$response['success']='0';
		$response['details']="Agent already exists";
	}
}
else
{
	$response['success']='0';
	$response['details']="Required field missing";
}	
echo json_encode($response);
?>