<?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/reservation.php'; 
include_once '../objects/reservation_detail.php'; 

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

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

$reservation = new CheckIn($db);
$reservation_detail = new CheckInDetail($db);

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

$reservation->reservation_no = isset($data->reservation_no) ? $data->reservation_no : null;

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

$deposit->reservation_no = isset($data->reservation_no) ? $data->reservation_no : null;
$deposit->room_no = isset($data->room_no) ? $data->room_no : null;

$validate->reservation_no = isset($data->reservation_no) ? $data->reservation_no : null;
$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";

// $reservation_detail->reservation_no = isset($data->reservation_no) ? $data->reservation_no : null;
// $reservation_detail->room_no = isset($data->room_no) ? $data->room_no : null;

    if ($check_in->existFromReservation()) {
        echo json_encode(array("message" => "duplicate"));
        exit();
    } elseif ($validate->deleteFromCheckInReserve()) {

        if($check_in->create()){
            $check_in_detail->checkin_id = $check_in->id;
            $validate->checkin_id = $check_in->id;

            $deposit->checkin_id = $check_in->id;
            $deposit->updateFromReserveCheckIn();

            // $reservation_detail->checkin_id = $check_in->id;
            // $reservation_detail->updateFromReserveCheckIn();
    
            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"));
    } else {
        echo json_encode(array("message" => "error"));
    }

?>
