<?php 
ob_clean();
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='add_post_out'){
	////////////// For Insert and Update ///////////////////////////
	utility::addslashesOnAllInput(array("query_string"));
    
	$page_action = $_REQUEST['page_action'];	
	$id = $_REQUEST['id'];	
	$job_id = $_REQUEST['job_id'];	
	$project_id = $_REQUEST['project_id'];	
    $post_type = $_REQUEST['post_type'];
    $post_out_date = utility::setFormatedDate($_REQUEST['post_out_date']);
    $posting_method = $_REQUEST['posting_method'];
    $recipient = $_REQUEST['recipient'];
    $post_out_notes = $_REQUEST['post_out_notes'];
	$created_on = date("Y-m-d");
	$updated_by = $_SESSION["user_id"];
		
	if($page_action == "add"){
		$sql= "INSERT INTO post_out(job_id,project_id,post_type,post_out_date,posting_method,recipient,post_out_notes,created_on,updated_by) VALUES('$job_id','$project_id','$post_type','$post_out_date','$posting_method','$recipient','$post_out_notes','$created_on','$updated_by')";
		mysqli_query($conn,$sql);
	}else{
		$sql= "UPDATE post_out SET job_id = '$job_id',project_id = '$project_id', post_type = '$post_type', post_out_date = '$post_out_date', posting_method = '$posting_method', recipient = '$recipient', post_out_notes = '$post_out_notes', updated_by = '$updated_by' WHERE id = '$id' ";
		mysqli_query($conn,$sql);
	}
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_post_out'){
	$post_out_id = $_REQUEST['post_out_id'];
	mysqli_query($conn,"DELETE FROM post_out WHERE id = '$post_out_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit_post_out'){
	$id = $_REQUEST['post_out_id'];
	
	$sql= "SELECT * FROM post_out 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 == "post_out_date"){
			$data_array[$key] = utility::getFormatedDateReturn($value);
		}else{
			$data_array[$key] = $value;
		}
	}
	$returnArray["data_array"] = $data_array;
	$returnArray["status"] = "success";
	
    echo json_encode($returnArray, JSON_FORCE_OBJECT);
}else{
	////////////// For Listing data through JSON ///////////////////////////
    $query_for_post_out = "SELECT * FROM post_out ORDER BY post_out_date DESC ";
	$sql_for_post_out = mysqli_query($conn,$query_for_post_out);
	$result_for_post_out = mysqli_num_rows($sql_for_post_out);
	$post_out = "";
	if($result_for_post_out > 0){
		$post_out .= '{';
		$post_out .= '"data" : [';
		while($row_for_post_out = mysqli_fetch_array($sql_for_post_out)){
			$post_out .= '{"post_type":"'  . keycode::getkeycodeById($row_for_post_out['post_type']) . '",';
			$post_out .= '"client":"'  . $gwaccounting->getJobUserById($row_for_post_out['job_id']) . '",';
			$post_out .= '"project":"'  . $gwaccounting->getClientProjectById($row_for_post_out['project_id']) . '",';
			$post_out .= '"id":"'  . $row_for_post_out['id'] . '",';
			$post_out .= '"post_out_date":"'  . utility::getFormatedDateReturn($row_for_post_out['post_out_date']) . '",';
			$post_out .= '"posting_method":"'  . keycode::getkeycodeById($row_for_post_out['posting_method']) . '",';
			$post_out .= '"recipient":"'  . keycode::getkeycodeById($row_for_post_out['recipient']) . '",';
			$post_out .= '"post_out_notes":"'  . $row_for_post_out['post_out_notes'] . '"},';
		}
		$post_out = substr($post_out,0,-1);
		$post_out .= "]";
		$post_out .= '}';
	}else{
		$post_out .= '{';
		$post_out .= '"data" : [';
		$post_out .= "]";
		$post_out .= '}';
	}
	echo $post_out;
}
die;
?>