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

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

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

$room = new Room($db);

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

$room->id = isset($data->id) ? $data->id : null;
$room->room_number = isset($data->room_number) ? $data->room_number : null;
$room->room_type = isset($data->room_type) ? $data->room_type : null; 
$room->bed_type = isset($data->bed_type) ? $data->bed_type : null;
$room->max_occupancy = isset($data->max_occupancy) ? $data->max_occupancy : 0;
$room->room_rate = isset($data->room_rate) ? $data->room_rate : 0;
$room->extra_bed = isset($data->extra_bed) ? $data->extra_bed : 0;
$room->extra_person = isset($data->extra_person) ? $data->extra_person : 0;
$room->remark = isset($data->remark) ? $data->remark : null;
$room->entry_by = isset($_SESSION['userid']) ? $_SESSION['userid'] : "";
$room->update_by = isset($_SESSION['userid']) ? $_SESSION['userid'] : "";

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