<?php 
include("../config.php");
if(!empty($_POST)){
	$act_code          	= mysqli_real_escape_string($conn, strtoupper($_POST['act_code']));
	$name         	 	= mysqli_real_escape_string($conn, strtoupper($_POST['name']));
	$pan_no       		= mysqli_real_escape_string($conn, strtoupper($_POST['pan_no']));
	$pan_img       	 	= mysqli_real_escape_string($conn, strtoupper($_POST['pan_imgs']));
	
	$company_id          = $_SESSION['company_id'];
	$branch_id           = $_SESSION['branch_id'];
	$created_datetime    = date('Y-m-d H:i:s');
	$created_id          = $_SESSION['user_detail']['id'];
	$updated_datetime    = date('Y-m-d H:i:s');
	$updated_id          = $_SESSION['user_detail']['id'];
	$attachment = NULL;
	if(NULL != $_FILES) {
		$attachment = $_FILES;
	}

	if(isset($attachment) && !empty($attachment)) {
		$file_extension = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);

		if (!in_array($file_extension, $config['attachment']['valid_types'])) {
			echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                    die();
		}else if ($config['attachment']['file_size'] < $_FILES["image"]["size"]) {
      		echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
            die();
        }
	}
	
	$image_name         = rand(1,99999).'.'.basename($_FILES['image']['name']);
	$upload_file_path   = $config['attachment']['folder_path'] . $image_name;
	
    @move_uploaded_file($attachment["image"]["tmp_name"], $upload_file_path);

	if($_POST["status"] != '0'){
  	 	$type               = 'update_agent_details';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);

		$insert = "UPDATE `agent` SET `act_code`='".$act_code."',`name`='".$name."',`pan_no`='".$pan_no."',`pan_img`='".$image_name."',`company_id`='".$company_id."',`branch_id`='".$branch_id."',`updated_id`='".$updated_id."',`updated_datetime`='".$updated_datetime."' WHERE id = '".$_POST['id']."'";

  	 	$result = mysqli_query($conn,$insert) or die( mysqli_error($conn) );
		$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$acttivity_res = mysqli_query($conn,$activiti);

  	}else{
		$type               = 'manage_agent_details';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
		$insert = "INSERT INTO `agent`(`act_code`, `name`, `pan_no`, `pan_img`, `status`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$act_code."','".$name."','".$pan_no."','".$image_name."','1','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";

		$result = mysqli_query($conn,$insert);
		
		$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
		$acttivity_res = mysqli_query($conn,$activiti);

	
  }
  echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=account_mst/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=account_mst/add.php') );die();
} 