<?php 
ob_clean();
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='add_timesheet_for_clients'){
	////////////// For Insert and Update ///////////////////////////
	utility::addslashesOnAllInput(array("query_string","t_admin_id"));
    
	$page_action = $_REQUEST['page_action_timesheet'];	
	$id = $_REQUEST['timesheet_id'];	
	$job_id = $_REQUEST['job_id'];	
	$admin_id = implode(",",$_REQUEST['t_admin_id']);
	$timesheet_text = $_REQUEST['timesheet_text'];
	$timesheet_option = $_REQUEST['timesheet_option'];
	$entry_for_month = $gsi->getEntryForMonthByJobId($job_id);
	$source	 = $_REQUEST['source'];
	$timesheet_date	 = utility::setFormatedDate($_REQUEST['timesheet_date']);
	$timesheet_time = $gsi->getTimesheetTimeInRound($_REQUEST['timesheet_time']);
	$from_time = $_REQUEST['from_time'];
	$to_time = $_REQUEST['to_time'];
	$created_on = date("Y-m-d");
		
	if($page_action == "add"){
	$sql= "INSERT INTO timesheet (job_id,source,admin_id,timesheet_text,timesheet_option,entry_for_month,timesheet_date,timesheet_time,from_time,to_time,updated_by,created_on) VALUES('$job_id','$source','$admin_id','$timesheet_text','$timesheet_option','$entry_for_month','$timesheet_date','$timesheet_time','$from_time','$to_time','".$_SESSION['user_id']."','$created_on')";
	mysqli_query($conn,$sql);
	}else{
		$sql= "UPDATE timesheet SET job_id = '$job_id',admin_id = '$admin_id',timesheet_text = '$timesheet_text',timesheet_option = '$timesheet_option',timesheet_date = '$timesheet_date',timesheet_time = '$timesheet_time',from_time = '$from_time',to_time = '$to_time',updated_by = '".$_SESSION['user_id']."' WHERE id = '$id' ";
		mysqli_query($conn,$sql);
	}
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_timesheet_for_clients'){
	$timesheet_for_clients_id = $_REQUEST['timesheet_for_clients_id'];
	mysqli_query($conn,"DELETE FROM timesheet WHERE id = '$timesheet_for_clients_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='edit_timesheet_for_clients'){
	$id = $_REQUEST['timesheet_for_clients_id'];
	$sql= "SELECT * FROM timesheet WHERE id = '$id' ";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_assoc($result_edit);
	$data_array = array();
	$dataArray["status"] = 0;
	$dataArray["timesheet_text"] = $row_edit['timesheet_text'];
	$dataArray["timesheet_option"] = $row_edit['timesheet_option'];
	$dataArray["admin_id"] = explode(",",$row_edit['admin_id']);
	$dataArray["timesheet_date"] = utility::getFormatedDateReturn($row_edit['timesheet_date']);
	$dataArray["timesheet_time"] = $row_edit['timesheet_time'];
	$dataArray["from_time"] = $row_edit['from_time'];
	$dataArray["to_time"] = $row_edit['to_time'];
	$dataArray["status"]		= 1;
	echo json_encode($dataArray);
    //echo json_encode($returnArray, JSON_FORCE_OBJECT);
}else{
	////////////// For Listing data through JSON ///////////////////////////
	$id=$_REQUEST['id'];
   $query_for_timesheet_for_clients = "SELECT * FROM timesheet WHERE job_id='$id' ORDER BY timesheet_date DESC ";
	$sql_for_timesheet_for_clients = mysqli_query($conn,$query_for_timesheet_for_clients);
	$result_for_timesheet_for_clients = mysqli_num_rows($sql_for_timesheet_for_clients);
	$timesheet_for_clients = "";
	if($result_for_timesheet_for_clients > 0){
		$timesheet_for_clients .= '{';
		$timesheet_for_clients .= '"data" : [';
		while($row_timesheet_for_clients = mysqli_fetch_array($sql_for_timesheet_for_clients)){ 
			
			$timesheet_for_clients .= '{"client":"'  . ucfirst(utility::getTableFieldById('job','first_name','id',$row_timesheet_for_clients['job_id'])) . '",';
			$timesheet_for_clients .= '"description":"'  .utility::ellipsis(strip_tags(trim(preg_replace('/\s\s+/', ' ', $row_timesheet_for_clients['timesheet_text']))),50). '",';
			$timesheet_for_clients .= '"timesheet_option":"'  . Keycode::getkeycodeById($row_timesheet_for_clients['timesheet_option']) . '",';
			$timesheet_for_clients .= '"consultant":"'  . ucfirst(Admin::getAdminsById($row_timesheet_for_clients['admin_id'])) . '",';
			$timesheet_for_clients .= '"timesheet_date":"'  . utility::getFormatedDateReturn($row_timesheet_for_clients['timesheet_date']) . '",';
			$timesheet_for_clients .= '"timesheet_time":"'  . date("H:i",strtotime($row_timesheet_for_clients['timesheet_time'])) . '",';
			$timesheet_for_clients .= '"from_time":"'  . date("H:i",strtotime($row_timesheet_for_clients['from_time'])) . '",';
			$timesheet_for_clients .= '"to_time":"'  . date("H:i",strtotime($row_timesheet_for_clients['to_time'])) . '",';
			$timesheet_for_clients .= '"id":"'  . $row_timesheet_for_clients['id'] . '"},';
		}
		$timesheet_for_clients = substr($timesheet_for_clients,0,-1);
		$timesheet_for_clients .= "]";
		$timesheet_for_clients .= '}';
	}else{
		$timesheet_for_clients .= '{';
		$timesheet_for_clients .= '"data" : [';
		$timesheet_for_clients .= "]";
		$timesheet_for_clients .= '}';
	}
	echo $timesheet_for_clients;
}
die;
?>