<?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","project_id"));
    
	$page_action = $_REQUEST['page_action_post_out'];	
	$id = $_REQUEST['id'];	
	$job_id = $_REQUEST['job_id'];
	$project_id = implode(",",$_REQUEST['project_id']);		
    $post_type = $_REQUEST['post_type'];
    $post_out_date = utility::setFormatedDate($_REQUEST['post_out_date']);
    $posting_method = (isset($_REQUEST['posting_method']) && $_REQUEST['posting_method'] != "") ? $_REQUEST['posting_method'] : 0;
    $recipient = (isset($_REQUEST['recipient']) && $_REQUEST['recipient'] != "") ? $_REQUEST['recipient'] : 0;
    $post_out_notes = $_REQUEST['post_out_notes'];
    $callback_request = $_REQUEST['callback_request'];
    $post_out_time = $_REQUEST['post_out_time'];
    $graduation = $_REQUEST['graduation'];
    $gender = $_REQUEST['gender'];
	$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,callback_request,post_out_time,graduation,gender,created_on,updated_by) VALUES('$job_id','$project_id','$post_type','$post_out_date','$posting_method','$recipient','$post_out_notes','$callback_request','$post_out_time','$graduation','$gender','$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', callback_request = '$callback_request',post_out_time = '$post_out_time',graduation = '$graduation',gender = '$gender', updated_by = '$updated_by' WHERE id = '$id' ";
		mysqli_query($conn,$sql);
	}
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_edit_data_post_out'){
	$id = $_REQUEST['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"){
			$value = utility::getFormatedDateReturn($value);
		}
		$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_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{
	////////////// For Listing data through JSON ///////////////////////////
	$job_id = $_REQUEST["job_id"];
    $query_for_post_out = "SELECT * FROM post_out WHERE job_id = '$job_id' ORDER BY id 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)){
			$callback_request = "";
			if($row_for_post_out['callback_request'] == 1){
				$callback_request = "No";
			}elseif($row_for_post_out['callback_request'] == 2){
				$callback_request = "Yes";
			}
			$graduation = ($row_for_post_out['graduation'] == 1) ? "Yes" : "No";
			$gender = ($row_for_post_out['gender'] == 0) ? "Male" : "Female";
			
			$post_out .= '{"post_type":"'  . keycode::getkeycodeById($row_for_post_out['post_type']) . '",';
			$post_out .= '"id":"'  . $row_for_post_out['id'] . '",';
			$post_out .= '"project":"'  . $gsi->getOrganisationById($row_for_post_out['project_id']) . '",';
			$post_out .= '"post_out_date":"'  . utility::getFormatedDateReturn($row_for_post_out['post_out_date']) . '",';
			$post_out .= '"post_out_time":"'  . $row_for_post_out['post_out_time'] . '",';
			$post_out .= '"graduation":"'  . $graduation . '",';
			$post_out .= '"gender":"'  . $gender . '",';
			$post_out .= '"posting_method":"'  . keycode::getkeycodeById($row_for_post_out['posting_method']) . '",';
			$post_out .= '"recipient":"'  . keycode::getkeycodeById($row_for_post_out['recipient']) . '",';
			$post_out .= '"callback_request":"'  . $callback_request . '",';
			$post_out .= '"post_out_notes":"'  . utility::ellipsis(preg_replace('/\s\s+|"/', ' ',$row_for_post_out['post_out_notes']),50) . '"},';
		}
		$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;
?>