<?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']."Shipment.php");
	$obj_shipment = new Shipment();

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

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

		$N_user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";
		$N_cur_total = isset($_REQUEST['cur_total']) ? $_REQUEST['cur_total'] : "";

		if($N_user_id != ""){

			$new_total = $obj_shipment->total_new_shipment($obj_crud);
			if($new_total > 0){
				if($new_total > $N_cur_total){
					$total_data = $result[0]['total_data'];
					$_SESSION['total_new_shipment'] = $new_total;
					$result = $obj_shipment->get_new_shipment($obj_crud);
					$R_message = array("code" => "200", "message" => "Fetch success", "total_data" => $new_total, "data" => $result);
				} else {
					$R_message = array("code" => "400", "message" => "Total data still same");
				}
			} else {
				$R_message = array("code" => "400", "message" => "Failed Get Total");
			}

			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);
}
?>