<?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/duty_schedule.php'; 

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

$database = new Database();
$db = $database->getConnection();

$duty_schedule = new DutySchedule($db);

$data = json_decode(file_get_contents("php://input"));

$duty_schedule->year_month = $data->year . '-'. $data->month;
$duty_schedule->staff_id = $data->staff_id;

$duty_schedule->entry_by = isset($_SESSION['user']) ? $_SESSION['user'] : "";

$duty_schedule->delete();

	foreach ($data->schedule as $detail) {
		$duty_schedule->date = $detail->date;
		$duty_schedule->duty_in = $detail->duty_in;
		$duty_schedule->duty_out = $detail->duty_out;
		$duty_schedule->off_day = $detail->off_day;
		$duty_schedule->create();

	}

echo json_encode(array("message" => "created"));

?>
