<?php
header('content-type: application/json');
header("access-control-allow-origin: *");
require_once(dirname(__FILE__)."/../packages/require_api.php");

if(isset($_GET['action'])){

	require_once($global['root-model']."Crud.php");
	$obj_crud = new Crud();

	require_once($global['root-model']."Admin.php");
	$obj_admin = new Admin();

	require_once($global['root-model']."PO.php");
	$obj_po = new PO();

	require_once($global['root-model']."Region.php");
	$obj_region = new Region();

	//===================================== Get Data ========================================
	if($_GET['action'] == 'get_data'){

		$R_message = array("code" => "400", "message" => "No Data");

		$N_user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";
		$N_page = isset($_REQUEST['page']) ? $_REQUEST['page'] : "1";
		$itemperpage = isset($_REQUEST['item']) ? $_REQUEST['item'] : "10";
		$N_status = isset($_REQUEST['status']) ? $_REQUEST['status'] : "open";
		$N_keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : "";
		$N_region = isset($_REQUEST['region']) ? $_REQUEST['region'] : "";
		$N_start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : "";
		$N_end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : "";
		$N_sortby = isset($_REQUEST['sortby']) ? $_REQUEST['sortby'] : "";
		$N_acsg_pic = isset($_REQUEST['acsg_pic']) ? $_REQUEST['acsg_pic'] : "";
		if($N_acsg_pic == 1){
			$acs_ref_admin = $N_user_id;
		} else {
			$acs_ref_admin = "";
		}

		if($N_start_date != ""){
			$N_start_format = date("Y-m-d", strtotime($N_start_date));
		} else {
			$N_start_format = "";
		}
		if($N_end_date != ""){
			$N_end_format = date("Y-m-d", strtotime($N_end_date)).' 23:59:00';
		} else {
			$N_end_format = "";
		}
		if($N_user_id != ""){

			$result = $obj_po->get_data($obj_crud, $N_page, $itemperpage, $N_status, $N_keyword, $N_start_format, $N_end_format,$N_region, $N_sortby, $acs_ref_admin);
			if(is_array($result)){
				$total_data = $result[0]['total_data_all'];
				$total_page = $result[0]['total_page'];
				$remaining = $total_data - ((($N_page-1) * $itemperpage) + count($result));
				$R_message = array("code" => "200", "message" => "Fetch success", "num_data" => count($result), "remaining" => $remaining, "total_data" => $total_data, "total_page" => $total_page, "data" => $result);
			}

			echo json_encode($R_message); 

		} else {
			$R_message = array("code" => "402", "message" => "Unauthorized access");
			echo json_encode($R_message);
		}
	} else if($_GET['action'] == 'get_total'){
		$R_message = array("code" => "400", "message" => "No Data");

		$N_user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";
		$N_status = isset($_REQUEST['status']) ? $_REQUEST['status'] : "open";
		$N_keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : "";
		$N_region = isset($_REQUEST['region']) ? $_REQUEST['region'] : "";
		$N_start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : "";
		$N_end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : "";
		$N_acsg_pic = isset($_REQUEST['acsg_pic']) ? $_REQUEST['acsg_pic'] : "";
		if($N_acsg_pic == 1){
			$acs_ref_admin = $N_user_id;
		} else {
			$acs_ref_admin = "";
		}
		if($N_start_date != ""){
			$N_start_format = date("Y-m-d", strtotime($N_start_date));
		} else {
			$N_start_format = "";
		}
		if($N_end_date != ""){
			$N_end_format = date("Y-m-d", strtotime($N_end_date)).' 23:59:00';
		} else {
			$N_end_format = "";
		}
		if($N_user_id != ""){

			$res_open = $obj_po->total_data($obj_crud,'open',$N_keyword,$N_start_format,$N_end_format,$N_region,$acs_ref_admin);
			$res_prepared = $obj_po->total_data($obj_crud,'prepared',$N_keyword,$N_start_format,$N_end_format,$N_region,$acs_ref_admin);
			$res_progress = $obj_po->total_data($obj_crud,'progress',$N_keyword,$N_start_format,$N_end_format,$N_region,$acs_ref_admin);
			$res_complete = $obj_po->total_data($obj_crud,'complete',$N_keyword,$N_start_format,$N_end_format,$N_region,$acs_ref_admin);
			$res_cancel = $obj_po->total_data($obj_crud,'cancel',$N_keyword,$N_start_format,$N_end_format,$N_region,$acs_ref_admin);
			
			$R_message = array("code" => "200", "message" => "Fetch success", "total_open" => $res_open, "total_progress" => $res_progress, "total_prepared" => $res_prepared, "total_complete" => $res_complete, "total_cancel" => $res_cancel);

			echo json_encode($R_message); 

		} else {
			$R_message = array("code" => "402", "message" => "Unauthorized access");
			echo json_encode($R_message);
		}
	} else {
		$R_message = array("code" => "404", "message" => "Action Not Found");
		echo json_encode($R_message);
	}
}//end gate
else{
	$R_message = array("code" => "404", "message" => "Not Found");
	echo json_encode($R_message);
}
?>