<?php 
ob_clean();
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='add_contract'){
	////////////// For Insert and Update ///////////////////////////
	utility::addslashesOnAllInput(array("query_string"));
    
	$page_action = $_REQUEST['page_action_contract'];	
	$id = $_REQUEST['id'];
	$job_id = $_REQUEST['job_id'];	
	$policy_type	= (isset($_REQUEST['policy_type']) && $_REQUEST['policy_type'] != '') ? $_REQUEST['policy_type'] : '0';
    $contract_value = $_REQUEST['contract_value'];
	$renewal_date	 = utility::setFormatedDate($_REQUEST['renewal_date']);
	$contract_status	= (isset($_REQUEST['contract_status']) && $_REQUEST['contract_status'] != '') ? $_REQUEST['contract_status'] : '0';
	$source	 = $_REQUEST['source'];
	$created_on = date("Y-m-d");
	$updated_by = $_SESSION["user_id"];
		
	if($page_action == "add"){
		$sql= "INSERT INTO job_contract (job_id,policy_type,contract_value,renewal_date,contract_status,source,updated_by) VALUES('$job_id','$policy_type','$contract_value','$renewal_date','$contract_status','$source','".$_SESSION['user_id']."')";
		mysqli_query($conn,$sql);
	}else{
		$sql= "UPDATE job_contract SET job_id = '$job_id',policy_type = '$policy_type',contract_value = '$contract_value',renewal_date = '$renewal_date',renewal_date = '$renewal_date',contract_status = '$contract_status',updated_by = '".$_SESSION['user_id']."' WHERE id = '$id' ";
		mysqli_query($conn,$sql);
	}
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_edit_data_contract'){
	$id = $_REQUEST['id'];
	$sql= "SELECT * FROM job_contract WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_assoc($result_edit);

    $data_array = array();
	foreach($row_edit as $key => $value){
		if($key == "renewal_date"){
			$value = utility::getFormatedDateReturn($value);
			$data_array["renewal_date"] = $value;
		}else{
			$data_array[$key] = $value;
		}
	}
	$returnArray["data_array"] = $data_array;
	$returnArray["status"] = "success";
	
    echo json_encode($returnArray, JSON_FORCE_OBJECT);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_contract'){
	$contract_id = $_REQUEST['contract_id'];
	mysqli_query($conn,"DELETE FROM job_contract WHERE id = '$contract_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else{
	////////////// For Listing data through JSON ///////////////////////////
	$job_id = $_REQUEST["job_id"];
    $query_for_contract = "SELECT * FROM job_contract WHERE job_id = '$job_id' ORDER BY id DESC ";
	$sql_for_contract = mysqli_query($conn,$query_for_contract);
	$result_for_contract = mysqli_num_rows($sql_for_contract);
	$contract_for_clients = "";
	if($result_for_contract > 0){
		$contract_for_clients .= '{';
		$contract_for_clients .= '"data" : [';
		while($row_contract_for_clients = mysqli_fetch_assoc($sql_for_contract)){
			
			$contract_for_clients .= '{"job_id":"'  . $gsi->getEnqQuoteJobById($row_contract_for_clients['job_id']) . '",';
			$contract_for_clients .= '"policy_type":"'  .ucfirst(Keycode::getkeycodeById($row_contract_for_clients["policy_type"])). '",';
			$contract_for_clients .= '"contract_status":"'  .ucfirst(Keycode::getkeycodeById($row_contract_for_clients["contract_status"])) . '",';
			$contract_for_clients .= '"contract_value":"'  . $row_contract_for_clients['contract_value'] . '",';
			$contract_for_clients .= '"renewal_date":"'  . utility::getFormatedDateReturn($row_contract_for_clients['renewal_date']) . '",';
			$contract_for_clients .= '"id":"'  . $row_contract_for_clients['id'] . '"},';
		}
		$contract_for_clients = substr($contract_for_clients,0,-1);
		$contract_for_clients .= "]";
		$contract_for_clients .= '}';
	}else{
		$contract_for_clients .= '{';
		$contract_for_clients .= '"data" : [';
		$contract_for_clients .= "]";
		$contract_for_clients .= '}';
	}
	echo $contract_for_clients;
}
die;
?>