<?php 
ob_clean();
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='add_reminder'){
	////////////// For Insert and Update ///////////////////////////
	utility::addslashesOnAllInput(array("query_string"));
    	
	$page_action = $_REQUEST['page_action_reminder'];	
	$id = $_REQUEST['id'];	
	$job_id = $_REQUEST['job_id'];	
	$note_text = $_REQUEST['note_text'];	
	$admin_id = $_REQUEST['admin_id'];	
	//$admin_id = implode(",",$_REQUEST['r_admin_id']);	
	$reminder_date = utility::setFormatedDate($_REQUEST['reminder_date']);
	$reminder_time = $_REQUEST['reminder_time'];
	$updated_on = date("Y-m-d");
	$updated_by = $_SESSION["user_id"];
	$created_on = date("Y-m-d");
	$note_type = "1";
	$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,note_date,reminder_time,note_type,source,updated_by,created_on) VALUES('$job_id','$admin_id','$job_user_id','$note_text','$reminder_date','$reminder_time','$note_type','$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='$reminder_date',reminder_time='$reminder_time', updated_by = '$updated_by' WHERE id = '$id' ";
		mysqli_query($conn,$sql);
	}
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_edit_data_reminder'){
	$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["reminder_date"] = $value;
		}else{
			$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_reminder'){
	$reminder_id = $_REQUEST['reminder_id'];
	mysqli_query($conn,"DELETE FROM notes WHERE id = '$reminder_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else{
	////////////// For Listing data through JSON ///////////////////////////
	$job_id = $_REQUEST["job_id"];
    $query_for_reminder_for_clients = "SELECT * FROM notes WHERE job_id = '$job_id' AND note_type = '1' ORDER BY id DESC ";
	$sql_for_reminder_for_clients = mysqli_query($conn,$query_for_reminder_for_clients);
	$result_for_reminder_for_clients = mysqli_num_rows($sql_for_reminder_for_clients);
	$reminder_for_clients = "";
	if($result_for_reminder_for_clients > 0){
		$reminder_for_clients .= '{';
		$reminder_for_clients .= '"data" : [';
		while($row_reminder_for_clients = mysqli_fetch_assoc($sql_for_reminder_for_clients)){ 
					
			$reminder_for_clients .= '{"client":"'  . ucfirst(utility::getTableFieldById('job','first_name','id',$row_reminder_for_clients['job_id'])) . '",';
			$reminder_for_clients .= '"id":"'  . $row_reminder_for_clients['id'] . '",';
			$reminder_for_clients .= '"note_text":"'  . utility::ellipsis(preg_replace('/\s\s+|"/', ' ',$row_reminder_for_clients['note_text']),50) . '",';
			$reminder_for_clients .= '"admin_id":"'  . Admin::getAdminsById($row_reminder_for_clients['admin_id']) . '",';
			$reminder_for_clients .= '"time":"'  . date("H:i",strtotime($row_reminder_for_clients['reminder_time'])) . '",';
			$reminder_for_clients .= '"note_date":"'  . utility::getFormatedDateReturn($row_reminder_for_clients['note_date']) . '"},';
			
		}
		$reminder_for_clients = substr($reminder_for_clients,0,-1);
		$reminder_for_clients .= "]";
		$reminder_for_clients .= '}';
	}else{
		$reminder_for_clients .= '{';
		$reminder_for_clients .= '"data" : [';
		$reminder_for_clients .= "]";
		$reminder_for_clients .= '}';
	}
	echo $reminder_for_clients;
}
die;
?>