<?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/check_in.php'; 
include_once '../objects/check_in_detail.php'; 
include_once '../objects/validate.php';

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

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

$check_in = new CheckIn($db);
$check_in_detail = new CheckInDetail($db);
$validate = new Validate($db);

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

$check_in->reservation_no = "";
$check_in->guest_id = isset($data->guest_id) ? $data->guest_id : null;
$check_in->guest_name = isset($data->guest_name) ? $data->guest_name : null;
$check_in->room_type = isset($data->room_type) ? $data->room_type : null;
$check_in->bed_type = isset($data->bed_type) ? $data->bed_type : null;
$check_in->room_no = isset($data->room_no) ? $data->room_no : null;
$check_in->adult = isset($data->adult) ? $data->adult : null;
$check_in->child = isset($data->child) ? $data->child : null;
$check_in->remark = isset($data->remark) ? $data->remark : null;
$check_in->entry_by = isset($_SESSION['user']) ? $_SESSION['user'] : "";
$check_in->update_by = isset($_SESSION['user']) ? $_SESSION['user'] : "";

$validate->reservation_no = "";
$validate->room_type = isset($data->room_type) ? $data->room_type : null;
$validate->bed_type = isset($data->bed_type) ? $data->bed_type : null;
$validate->room_no = isset($data->room_no) ? $data->room_no : null;
$validate->status = "checkin";

    // if ($check_in->existFromReservation()) {
    //     echo json_encode(array("message" => "duplicate"));
    //     exit();
    // } else{

        if($check_in->create()){
            $check_in_detail->checkin_id = $check_in->id;
            $validate->checkin_id = $check_in->id;
            foreach ($data->stayDetails as $detail) {
    
                $check_in_detail->date = $detail->date;
                $check_in_detail->room_rate = $detail->room_rate;
                $check_in_detail->extra_bed = $detail->extra_bed;
                $check_in_detail->extra_person = $detail->extra_person;
                $check_in_detail->extra_bed_rate = $detail->extra_bed_rate;
                $check_in_detail->extra_person_rate = $detail->extra_person_rate;
    
                $check_in_detail->create();

                $validate->date = $detail->date;
                $validate->room_rate = $detail->room_rate;
                $validate->extra_bed = $detail->extra_bed;
                $validate->extra_person = $detail->extra_person;
                $validate->extra_bed_rate = $detail->extra_bed_rate;
                $validate->extra_person_rate = $detail->extra_person_rate;
    
                $validate->create();
    
            }
        }

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

?>
