<?php 
ob_clean();
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='add_notes_for_clients'){
	////////////// For Insert and Update ///////////////////////////
	utility::addslashesOnAllInput(array("query_string"));
    
	$page_action = $_REQUEST['page_action_notes'];	
	$id = $_REQUEST['notes_id'];	
	$job_id = $_REQUEST['job_id'];	
	$note_text = $_REQUEST['note_text'];	
	$admin_id = $_REQUEST['admin_id'];	
	$note_date = utility::setFormatedDate($_REQUEST['note_date']);	
	$updated_on = date("Y-m-d");
	$updated_by = $_SESSION["user_id"];
	$created_on = date("Y-m-d");
	$scorecard_notes_option	= (isset($_REQUEST['scorecard_notes_option']) && $_REQUEST['scorecard_notes_option'] != '') ? $_REQUEST['scorecard_notes_option'] : '0';
	$job_user_id	= (isset($_REQUEST['job_user_id']) && $_REQUEST['job_user_id'] != '') ? $_REQUEST['job_user_id'] : '0';
	$source	 = $_REQUEST['source'];
		
	if($page_action == "add"){
	$sql= "INSERT INTO notes (job_id,admin_id,job_user_id,note_text,scorecard_notes_option,note_date,source,updated_by,created_on) VALUES('$job_id','$admin_id','$job_user_id','$note_text','$scorecard_notes_option','$note_date','$source','".$admin_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',updated_on = '$updated_on', 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_notes_for_clients'){
	$notes_for_clients_id = $_REQUEST['notes_for_clients_id'];
	mysqli_query($conn,"DELETE FROM notes WHERE id = '$notes_for_clients_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit_notes_for_clients'){
	$id = $_REQUEST['notes_for_clients_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 ///////////////////////////
	$id=$_REQUEST['id'];
    $query_for_notes_for_clients = "SELECT * FROM notes WHERE job_id='$id' AND note_type = 0 ORDER BY note_date DESC ";
	$sql_for_notes_for_clients = mysqli_query($conn,$query_for_notes_for_clients);
	$result_for_notes_for_clients = mysqli_num_rows($sql_for_notes_for_clients);
	$notes_for_clients = "";
	if($result_for_notes_for_clients > 0){
		$notes_for_clients .= '{';
		$notes_for_clients .= '"data" : [';
		while($row_notes_for_clients = mysqli_fetch_array($sql_for_notes_for_clients)){ 
			$notes_for_clients .= '{"client":"'  . ucfirst(utility::getTableFieldById('job','first_name','id',$row_notes_for_clients['job_id'])) . '",';
			$notes_for_clients .= '"description":"'  .utility::ellipsis(ucfirst($row_notes_for_clients['note_text']),50). '",';
			$notes_for_clients .= '"consultant":"'  . ucfirst(Admin::getAdminById($row_notes_for_clients['admin_id'])) . '",';
			$notes_for_clients .= '"date":"'  . utility::getFormatedDateReturn($row_notes_for_clients['note_date']) . '",';
			$notes_for_clients .= '"id":"'  . $row_notes_for_clients['id'] . '"},';
		}
		$notes_for_clients = substr($notes_for_clients,0,-1);
		$notes_for_clients .= "]";
		$notes_for_clients .= '}';
	}else{
		$notes_for_clients .= '{';
		$notes_for_clients .= '"data" : [';
		$notes_for_clients .= "]";
		$notes_for_clients .= '}';
	}
	echo $notes_for_clients;
}
die;
?>