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

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

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

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

		$N_user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";
		$N_keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : "";
		
		if($N_user_id != ""){

			$result = $obj_client->get_data_select($obj_crud, $N_keyword);
			
			if(is_array($result)){
				$R_message = array("code" => "200", "message" => "Fetch success", "num_data" => count($result), "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_detail'){
		$R_message = array("code" => "400", "message" => "No Data");

		$N_user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";
		$N_client_id = isset($_REQUEST['client_id']) ? $_REQUEST['client_id'] : "";
		
		if($N_user_id != ""){

			$result = $obj_client->get_data_detail($obj_crud, $N_client_id);
			
			if(is_array($result)){
				$R_message = array("code" => "200", "message" => "Fetch success", "num_data" => count($result), "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_data_list'){
		$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'] : "";
		$itemperpage = isset($_REQUEST['item']) ? $_REQUEST['item'] : "";
		$region = isset($_REQUEST['region']) ? $_REQUEST['region'] : "";
		$keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : "";
		
		if($N_user_id != ""){

			$result = $obj_client->get_data_list($obj_crud, $N_page, $itemperpage, $region, $keyword);
			
			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'] == 'insert'){
		$R_message = array("code" => "400", "message" => "Something Wrong");

		$N_user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";
		$N_unit = isset($_REQUEST['unit']) ? $_REQUEST['unit'] : "";
		$N_title = isset($_REQUEST['title']) ? $_REQUEST['title'] : "";
		$cat_title = strtolower($N_title);
		
		if($N_user_id != ""){
			$result = $obj_client->insert_data($obj_crud, $N_unit, $cat_title);
			if($result != 0){
				$R_message = array("code" => "200", "message" => "Success Process Data");
			} else {
				$R_message = array("code" => "400", "message" => "Something Wrong");
			}
			echo json_encode($R_message); 

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

		$N_user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";
		$N_item_id = isset($_REQUEST['item_id']) ? $_REQUEST['item_id'] : "";
		$N_unit = isset($_REQUEST['unit']) ? $_REQUEST['unit'] : "";
		$N_title = isset($_REQUEST['title']) ? $_REQUEST['title'] : "";
		$cat_title = strtolower($N_title);
		
		if($N_user_id != ""){
			$result = $obj_client->update_data($obj_crud, $N_item_id, $N_unit, $cat_title);
			if($result != 0){
				$R_message = array("code" => "200", "message" => "Success Process Data");
			} else {
				$R_message = array("code" => "400", "message" => "Something Wrong");
			}
			echo json_encode($R_message); 

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

		$N_user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : "";
		$N_item_id = isset($_REQUEST['item_id']) ? $_REQUEST['item_id'] : "";
		
		if($N_user_id != ""){
			$result = $obj_client->delete_data($obj_crud, $N_item_id);
			if($result != 0){
				$R_message = array("code" => "200", "message" => "Success Process Data");
			} else {
				$R_message = array("code" => "400", "message" => "Something Wrong");
			}
			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);
}
?>