<?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'];
	$project_id	= (isset($_REQUEST['project_id']) && $_REQUEST['project_id'] != '') ? $_REQUEST['project_id'] : '0';
	$timesheet_option= $_REQUEST['timesheet_option'];	
    $source =  $gsi->getEnqQuoteJobStatusById($_REQUEST['job_id']);	
    //$admin_id = implode(",",$_REQUEST['admin_id']);
    $admin_id = $_REQUEST['admin_id'];
	$timesheet_text = $_REQUEST['timesheet_text'];
	$entry_for_month = $gsi->getEntryForMonthByJobId($job_id);
	$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 H:i:s");
	$updated_by = $_SESSION["user_id"];
		
	if($page_action == "add"){
		$sql= "INSERT INTO timesheet (job_id,project_id,timesheet_option,source,admin_id,timesheet_text,entry_for_month,timesheet_date,timesheet_time,from_time,to_time,updated_by,created_on) VALUES('$job_id','$project_id','$timesheet_option','$source','$admin_id','$timesheet_text','$entry_for_month','$timesheet_date','$timesheet_time','$from_time','$to_time','$updated_by','$created_on')";
		mysqli_query($conn,$sql);
	}else{
		$sql= "UPDATE timesheet SET job_id = '$job_id',project_id = '$project_id',timesheet_option = '$timesheet_option',admin_id = '$admin_id',timesheet_text = '$timesheet_text',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_post_out'){
	$post_out_id = $_REQUEST['post_out_id'];
	mysqli_query($conn,"DELETE FROM timesheet 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 timesheet 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 == "timesheet_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 timesheet  ORDER BY id 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)){
			$units = $gsi->getUnitByTime($row_for_post_out['timesheet_time']);
			//$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 .= '"timesheet_text":"'  . strip_tags(trim(preg_replace('/\s\s+/', ' ', $row_for_post_out['timesheet_text']))) . '",';
			$post_out .= '"timesheet_option":"'  . Keycode::getkeycodeById($row_for_post_out['timesheet_option']) . '",';
			$post_out .= '"admin_id":"'  . Admin::getAdminsById($row_for_post_out['admin_id']) . '",';
			$post_out .= '"timesheet_date":"'  . utility::getFormatedDateReturn($row_for_post_out['timesheet_date']) . '",';
			$post_out .= '"timesheet_time":"'  . $row_for_post_out['timesheet_time'] . '",';
			$post_out .= '"from_time":"'  . $row_for_post_out['from_time'] . '",';
			$post_out .= '"to_time":"'  . $row_for_post_out['to_time'] . '",';
			$post_out .= '"units":"'  . $units . '",';
			$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;
?>