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

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

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

$staff = new Staff($db);

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

$staff->id = $data->id;
$staff->name = $data->name;
$staff->position = $data->position; 
$staff->phone = $data->phone;
$staff->email = $data->email;
$staff->nrc = $data->nrc;
$staff->gender = $data->gender;
$staff->dob = $data->dob;
$staff->join_date = $data->join_date;
$staff->address = $data->address;

if($data->resign_date){
	$staff->resign = 1;
	$staff->resign_date = $data->resign_date;
}else{
	$staff->resign = 0;
	$staff->resign_date = '';
}

$staff->entry_by = $_SESSION['user'];
$staff->update_by = $_SESSION['user'];

if ($data->id) {
    if ($staff->update()) {
        echo json_encode(array("message" => "updated"));
    } else {
        echo json_encode(array("message" => "error"));
    }
} else {
    if ($staff->exist()) {
        echo json_encode(array("message" => "duplicate"));
    } elseif ($staff->create()) {
        echo json_encode(array("message" => "created"));
    } else {
        echo json_encode(array("message" => "error"));
    }
}
?>
