<?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';
include_once '../helpers/rental_metadata.php';
 
$database = new Database();
$db = $database->getConnection();

$reservation = new Reservation($db);
$reservation_detail = new ReservationDetail($db);

$data = json_decode(file_get_contents("php://input"));  
$reservation->reservation_no = $data->reservation_no;
$reservation->room_no = ($data->room_no)?$data->room_no:'';
$reservation->getOneRow();

$reservation_detail->reservation_no = $data->reservation_no;
$reservation_detail->room_no = ($data->room_no)?$data->room_no:'';

$reservation_items = array();
$stmt = $reservation_detail->getReservationDetail();

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

		$item=array(
            "date" => $date,     
            "description" => $description ?? '',
            "room_no" => $room_no,         
            "room_rate" => $room_rate,
            "extra_bed" => $extra_bed,
            "extra_bed_rate" => $extra_bed_rate,
            "extra_person" => $extra_person,
            "extra_person_rate" => $extra_person_rate,
            "total" => $room_rate+($extra_bed*$extra_bed_rate)+($extra_person*$extra_person_rate)
        );
        array_push($reservation_items, $item);
    }
}

$reservation = array(
    "date_time" => $reservation->date_time,
    "reservation_no" => $reservation->reservation_no,
    "folio_no" => $reservation->folio_no,
    "discount_amount" => $reservation->discount_amount ?? 0,
    "agent_id" => $reservation->agent_id,
    "agent_name" => $reservation->agent_name,
    "guest_id" => $reservation->guest_id,	
    "guest_name" => $reservation->guest_name,	
    "remark" => parse_booking_metadata($reservation->remark)['note'],
    "rental_meta" => parse_booking_metadata($reservation->remark),
    "adult" => $reservation->adult,
    "child" => $reservation->child,
    "room_no" => $reservation->room_no,
    "room_type" => $reservation->room_type,
    "bed_type" => $reservation->bed_type,
    "extra_bed" => $reservation->extra_bed,
    "extra_person" => $reservation->extra_person,
    "reservation_items" => $reservation_items
);
echo json_encode($reservation);
?>
