<?php
session_start();
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';

date_default_timezone_set('Asia/Bangkok');

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

$reservation = new Reservation($db);
$data = json_decode(file_get_contents("php://input"));

$all_reservation_details = array();
$stmt = $reservation->getReservationDetailByDateType($data->type);
$num = $stmt->rowCount();

if ($num > 0) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);

        $detail_item = array(
            "reservation_no" => $reservation_no,
            "agent_name" => $agent_name,
            "guest_name" => $guest_name,
            "checkin_date" => $checkin_date,
            "checkout_date" => $checkout_date,
            "nights" => $nights,
            "room_no" => $room_no,
            "date_time" => $date_time,
            "created_at" => $created_at
        );
        array_push($all_reservation_details, $detail_item);
    }
}

$reservation = $reservation->getReservation($data->type);
$arr = array(
    "reservation" => $reservation,
    "all_reservation_details" => $all_reservation_details,
);

echo json_encode($arr);

?>
