<?php 
ob_clean();
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='add_notes'){
	////////////// For Insert and Update ///////////////////////////
	utility::addslashesOnAllInput(array("query_string"));
    
	$page_action = $_REQUEST['page_action_notes'];	
	$id = $_REQUEST['id'];	
	$job_id= $_REQUEST['job_id'];
    //$admin_id= $_SESSION['user_id'];
	$admin_id= $_REQUEST['admin_id'];
	$note_type = 0;
    $note_text = $_REQUEST['note_text'];
	$note_date	 = utility::setFormatedDate($_REQUEST['note_date']);
	$scorecard_notes_option	= (isset($_REQUEST['scorecard_notes_option']) && $_REQUEST['scorecard_notes_option'] != '') ? $_REQUEST['scorecard_notes_option'] : '0';
	$created_on = date("Y-m-d");
	$updated_by = $_SESSION["user_id"];
		
	if($page_action == "add"){
		$sql= "INSERT INTO notes(job_id,admin_id,note_type,note_text,note_date,scorecard_notes_option,created_on,updated_by) VALUES('$job_id','$admin_id','$note_type','$note_text','$note_date','$scorecard_notes_option','$created_on','$updated_by')";
		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',scorecard_notes_option = '$scorecard_notes_option', updated_by = '$updated_by' WHERE id = '$id' ";
		mysqli_query($conn,$sql);
	}
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_edit_data_notes'){
	$id = $_REQUEST['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"){
			$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_notes'){
	$notes_id = $_REQUEST['notes_id'];
	mysqli_query($conn,"DELETE FROM notes WHERE id = '$notes_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else{
	////////////// For Listing data through JSON ///////////////////////////
	$job_id = $_REQUEST["job_id"];
    $query_for_notes = "SELECT * FROM notes WHERE job_id = '$job_id' AND note_type = 0 ORDER BY id DESC ";
	$sql_for_notes = mysqli_query($conn,$query_for_notes);
	$result_for_post_out = mysqli_num_rows($sql_for_notes);
	$post_out = "";
	if($result_for_post_out > 0){
		$post_out .= '{';
		$post_out .= '"data" : [';
		while($row_for_notes = mysqli_fetch_assoc($sql_for_notes)){
			
			$post_out .= '{"client":"'  . ucfirst(utility::getTableFieldById('job','first_name','id',$row_for_notes['job_id'])) . '",';
			$post_out .= '"id":"'  . $row_for_notes['id'] . '",';
			$post_out .= '"note_text":"'  . utility::ellipsis(preg_replace('/\s\s+|"/', ' ',$row_for_notes['note_text']),50) . '",';
			$post_out .= '"admin_id":"'  . Admin::getAdminsById($row_for_notes['admin_id']) . '",';
			$post_out .= '"note_date":"'  . utility::getFormatedDateReturn($row_for_notes['note_date']) . '"},';
		}
		$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;
?>