<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

include_once '../config/database.php'; 
include_once '../objects/room.php'; 

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

$room = new Room($db);

// Get the room type ID from the POST request
$roomTypeId = isset($_POST['room_type_id']) ? $_POST['room_type_id'] : die();

// Prepare the statement to fetch rooms by type
$stmt = $room->getRoomByType($roomTypeId); // Pass room type ID as parameter
$num = $stmt->rowCount();

$response = array();
$response["success"] = false; // Initialize success status
$response["rooms"] = array();

if ($num > 0) {
    $response["success"] = true; // Set success to true if rooms found

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

        $item = array(
            "room_number" => $room_number, // Room number
            "room_rate" => (float)$room_rate, // Room rate
            "bed_type" => $bed_type // Bed type
        );

        array_push($response["rooms"], $item);
    }
} else {
    $response["message"] = "No rooms found for this type."; // Message if no rooms found
}

http_response_code(200);
echo json_encode($response);
?>
