<?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'];
    //$admin_id= $_SESSION['user_id'];
	$admin_id= $_REQUEST['admin_id'];
    $note_text= $_REQUEST['note_text'];
	$note_type = '1';
	$note_date	 = utility::setFormatedDate($_REQUEST['note_date']);
	$reminder_time = $_REQUEST['reminder_time'];
	$source	 = $_REQUEST['source'];
	$created_on = date("Y-m-d H:i:s");
	$updated_by = $_SESSION["user_id"];
		
	if($page_action == "add"){
		$sql= "INSERT INTO notes (job_id,admin_id,note_text,note_date,reminder_time,note_type,source,updated_by,created_on) VALUES('$job_id','$admin_id','$note_text','$note_date','$reminder_time','$note_type','$source','".$_SESSION['user_id']."','$created_on')";
		mysqli_query($conn,$sql);
	}else{
		$sql= "UPDATE notes SET job_id = '$job_id',admin_id = '$admin_id',note_text = '$note_text',note_date = '$note_date',reminder_time = '$reminder_time',note_type = '$note_type' 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 notes 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 notes 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 == "note_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 notes WHERE note_type = '1' ORDER BY note_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 .= '{"client":"'  . $gsi->getJobUserById($row_for_post_out['job_id']) . '",';
			$post_out .= '{"client":"'  . ucfirst(utility::getTableFieldById('job','first_name','id',$row_for_post_out['job_id'])) . '",';
			$post_out .= '"note_date":"'  . utility::getFormatedDateReturn($row_for_post_out['note_date']) . '",';
			$post_out .= '"reminder_time":"'  . $row_for_post_out['reminder_time'] . '",';
			$post_out .= '"admin_id":"'  . Admin::getAdminsById($row_for_post_out['admin_id']) . '",';
			$post_out .= '"note_text":"'  . $row_for_post_out['note_text'] . '",';
			$post_out .= '"id":"'  . $row_for_post_out['id'] . '"},';
		}
		$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;
?>