<?php 
	header("Access-Control-Allow-Origin: *");
	header("Content-Type: application/json; charset=UTF-8");
	header("Access-Control-Allow-Methods: POST");
	header("Access-Control-Max-Age: 3600");
	header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

	include_once '../config/database.php';
	include_once '../objects/attendance.php';	

	date_default_timezone_set('Asia/Bangkok'); 
	session_start();

	$database = new Database();
	$db = $database->getConnection();
	 
	$attendance = new Attendance($db);
	$data = json_decode(file_get_contents("php://input"));
	
		$attendance->id = $data->id;
        $attendance->att_date = $data->date;
        $attendance->staff_id = $data->staff_id;
		$attendance->duty_in = $data->duty_in_time;
		$attendance->duty_out = $data->duty_out_time;
		$attendance->second_duty_in = $data->second_duty_in_time;
		$attendance->second_duty_out = $data->second_duty_out_time;
		$attendance->entry_date_time = date("Y-m-d H:i:s");


		if($attendance->exist()){
			$attendance->updateMiss();
			$msg_arr = array(
				"message" => "updated"
			);
		}else if($attendance->createMiss()){
			$msg_arr = array(
				"message" => "created"
			);
		}else{
			$msg_arr = array(
				"message" => "error"
			);
		}
	
	echo json_encode($msg_arr);
?>