<?php
require_once __DIR__ . '/../../includes/bootstrap.php';
require_login();

if (!function_exists('rooms_column_exists')) {
    function rooms_column_exists(PDO $pdo, string $table, string $column): bool
    {
        static $cache = [];
        $key = $table . '.' . $column;

        if (array_key_exists($key, $cache)) {
            return $cache[$key];
        }

        try {
            $stmt = $pdo->prepare("
                SELECT COUNT(*)
                FROM INFORMATION_SCHEMA.COLUMNS
                WHERE TABLE_SCHEMA = DATABASE()
                  AND TABLE_NAME = :table_name
                  AND COLUMN_NAME = :column_name
            ");
            $stmt->execute([
                ':table_name'  => $table,
                ':column_name' => $column,
            ]);

            $cache[$key] = ((int)$stmt->fetchColumn() > 0);
        } catch (Throwable $e) {
            $cache[$key] = false;
        }

        return $cache[$key];
    }
}

$pageTitle = 'Rooms';

if (!function_exists('h')) {
    function h($value): string
    {
        return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('fetch_room_types_by_property')) {
    function fetch_room_types_by_property(PDO $pdo, int $propertyId = 0): array
    {
        $hasPropertyId = rooms_column_exists($pdo, 'room_types', 'property_id');
        $hasShortcode  = rooms_column_exists($pdo, 'room_types', 'shortcode');
        $hasStatus     = rooms_column_exists($pdo, 'room_types', 'status');
        $hasIsActive   = rooms_column_exists($pdo, 'room_types', 'is_active');

        $shortcodeSelect = $hasShortcode ? "shortcode" : "'' AS shortcode";

        $buildSql = function (bool $filterByProperty) use ($shortcodeSelect, $hasStatus, $hasIsActive): string {
            $sql = "
                SELECT id, title, {$shortcodeSelect}
                FROM room_types
                WHERE 1=1
            ";

            if ($filterByProperty) {
                $sql .= " AND property_id = :property_id ";
            }

            if ($hasStatus) {
                $sql .= " AND (status = 1 OR status = '1' OR LOWER(status) = 'active') ";
            } elseif ($hasIsActive) {
                $sql .= " AND is_active = 1 ";
            }

            $sql .= " ORDER BY title ASC ";
            return $sql;
        };

        $rows = [];

        if ($hasPropertyId && $propertyId > 0) {
            $stmt = $pdo->prepare($buildSql(true));
            $stmt->execute([':property_id' => $propertyId]);
            $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }

        if (!$rows) {
            $stmt = $pdo->prepare($buildSql(false));
            $stmt->execute();
            $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }

        return $rows ?: [];
    }
}

if (!function_exists('fetch_room_types_grouped')) {
    function fetch_room_types_grouped(PDO $pdo): array
    {
        $hasPropertyId = rooms_column_exists($pdo, 'room_types', 'property_id');
        $hasShortcode  = rooms_column_exists($pdo, 'room_types', 'shortcode');
        $hasStatus     = rooms_column_exists($pdo, 'room_types', 'status');
        $hasIsActive   = rooms_column_exists($pdo, 'room_types', 'is_active');

        $shortcodeSelect = $hasShortcode ? "shortcode" : "'' AS shortcode";
        $propertySelect  = $hasPropertyId ? "property_id" : "0 AS property_id";

        $sql = "
            SELECT id, title, {$shortcodeSelect}, {$propertySelect}
            FROM room_types
            WHERE 1=1
        ";

        if ($hasStatus) {
            $sql .= " AND (status = 1 OR status = '1' OR LOWER(status) = 'active') ";
        } elseif ($hasIsActive) {
            $sql .= " AND is_active = 1 ";
        }

        $sql .= " ORDER BY title ASC ";

        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];

        $grouped = [
            'all' => [],
            'byProperty' => [],
        ];

        foreach ($rows as $row) {
            $clean = [
                'id' => (int)($row['id'] ?? 0),
                'title' => (string)($row['title'] ?? ''),
                'shortcode' => (string)($row['shortcode'] ?? ''),
                'property_id' => (int)($row['property_id'] ?? 0),
            ];

            if ($clean['id'] <= 0 || $clean['title'] === '') {
                continue;
            }

            $grouped['all'][] = $clean;

            $pid = (int)$clean['property_id'];
            if ($pid > 0) {
                if (!isset($grouped['byProperty'][$pid])) {
                    $grouped['byProperty'][$pid] = [];
                }
                $grouped['byProperty'][$pid][] = $clean;
            }
        }

        return $grouped;
    }
}

if (!function_exists('fetch_properties')) {
    function fetch_properties(PDO $pdo): array
    {
        $stmt = $pdo->query("
            SELECT id, name, code
            FROM properties
            WHERE is_active = 1
            ORDER BY name ASC
        ");
        return $stmt ? $stmt->fetchAll(PDO::FETCH_ASSOC) : [];
    }
}

$form = [
    'id'           => 0,
    'property_id'  => '',
    'floor_id'     => '1',
    'room_no'      => '',
    'room_type_id' => '',
    'status'       => 'Clean',
];

$errors = [];
$success = '';
$editing = false;

$statusOptions = [
    'Clean',
    'Dirty',
    'Inspected',
    'Occupied',
    'Blocked',
    'Maintenance',
    'Out of Order',
];

if (isset($_GET['saved']) && (int)$_GET['saved'] === 1) {
    $success = 'Room saved successfully.';
}
if (isset($_GET['deleted']) && (int)$_GET['deleted'] === 1) {
    $success = 'Room deleted successfully.';
}

$properties = fetch_properties($pdo);

$propertyFilter = (int)($_GET['property_id'] ?? 0);
$roomTypeFilter = (int)($_GET['room_type_id'] ?? 0);
$floorFilter    = (int)($_GET['floor_id'] ?? 0);

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_room'])) {
    $form['id']           = (int)($_POST['id'] ?? 0);
    $form['property_id']  = trim((string)($_POST['property_id'] ?? ''));
    $form['floor_id']     = (string)max(1, (int)($_POST['floor_id'] ?? 1));
    $form['room_no']      = trim((string)($_POST['room_no'] ?? ''));
    $form['room_type_id'] = trim((string)($_POST['room_type_id'] ?? ''));
    $form['status']       = trim((string)($_POST['status'] ?? 'Clean'));

    $editing = $form['id'] > 0;
    $propertyFilter = (int)$form['property_id'];

    if ($form['property_id'] === '' || (int)$form['property_id'] <= 0) {
        $errors[] = 'Please select a property.';
    }
    if ($form['floor_id'] === '' || (int)$form['floor_id'] <= 0) {
        $errors[] = 'Floor is required.';
    }
    if ($form['room_no'] === '') {
        $errors[] = 'Room number is required.';
    } elseif (!is_numeric($form['room_no'])) {
        $errors[] = 'Room number must be numeric.';
    }
    if ($form['room_type_id'] === '' || (int)$form['room_type_id'] <= 0) {
        $errors[] = 'Please select a room type.';
    }
    if ($form['status'] === '') {
        $errors[] = 'Status is required.';
    }

    try {
        $stmt = $pdo->prepare("
            SELECT id
            FROM rooms
            WHERE property_id = :property_id
              AND room_no = :room_no
              AND id <> :id
            LIMIT 1
        ");
        $stmt->execute([
            ':property_id' => (int)$form['property_id'],
            ':room_no'     => (int)$form['room_no'],
            ':id'          => (int)$form['id'],
        ]);
        if ($stmt->fetch(PDO::FETCH_ASSOC)) {
            $errors[] = 'A room with that room number already exists for this property.';
        }
    } catch (Throwable $e) {
        $errors[] = $e->getMessage();
    }

    if (!$errors) {
        try {
            if ($editing) {
                $stmt = $pdo->prepare("
                    UPDATE rooms
                    SET property_id = :property_id,
                        floor_id = :floor_id,
                        room_no = :room_no,
                        room_type_id = :room_type_id,
                        status = :status
                    WHERE id = :id
                ");
                $stmt->execute([
                    ':property_id'  => (int)$form['property_id'],
                    ':floor_id'     => (int)$form['floor_id'],
                    ':room_no'      => (int)$form['room_no'],
                    ':room_type_id' => (int)$form['room_type_id'],
                    ':status'       => $form['status'],
                    ':id'           => (int)$form['id'],
                ]);
            } else {
                $stmt = $pdo->prepare("
                    INSERT INTO rooms
                    (
                        property_id,
                        floor_id,
                        room_no,
                        room_type_id,
                        status
                    )
                    VALUES
                    (
                        :property_id,
                        :floor_id,
                        :room_no,
                        :room_type_id,
                        :status
                    )
                ");
                $stmt->execute([
                    ':property_id'  => (int)$form['property_id'],
                    ':floor_id'     => (int)$form['floor_id'],
                    ':room_no'      => (int)$form['room_no'],
                    ':room_type_id' => (int)$form['room_type_id'],
                    ':status'       => $form['status'],
                ]);
            }

            header('Location: rooms.php?saved=1&property_id=' . (int)$form['property_id']);
            exit;
        } catch (Throwable $e) {
            $errors[] = $e->getMessage();
        }
    }
}

if (isset($_GET['delete']) && (int)($_GET['delete']) > 0) {
    $deleteId = (int)$_GET['delete'];

    try {
        $stmt = $pdo->prepare("DELETE FROM rooms WHERE id = :id");
        $stmt->execute([':id' => $deleteId]);

        header('Location: rooms.php?deleted=1&property_id=' . (int)$propertyFilter);
        exit;
    } catch (Throwable $e) {
        $errors[] = $e->getMessage();
    }
}

if (isset($_GET['edit']) && (int)($_GET['edit']) > 0) {
    $editId = (int)$_GET['edit'];

    try {
        $stmt = $pdo->prepare("
            SELECT *
            FROM rooms
            WHERE id = :id
            LIMIT 1
        ");
        $stmt->execute([':id' => $editId]);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($row) {
            $editing = true;
            $form = [
                'id'           => (int)$row['id'],
                'property_id'  => (string)($row['property_id'] ?? ''),
                'floor_id'     => (string)(int)($row['floor_id'] ?? 1),
                'room_no'      => (string)$row['room_no'],
                'room_type_id' => (string)$row['room_type_id'],
                'status'       => (string)$row['status'],
            ];
            $propertyFilter = (int)$form['property_id'];
        } else {
            $errors[] = 'Room not found.';
        }
    } catch (Throwable $e) {
        $errors[] = $e->getMessage();
    }
}

$roomTypes = fetch_room_types_by_property($pdo, (int)($form['property_id'] ?: $propertyFilter));
$roomTypesPreload = fetch_room_types_grouped($pdo);

$search = trim((string)($_GET['q'] ?? ''));
$hasRoomTypeShortcode = rooms_column_exists($pdo, 'room_types', 'shortcode');
$roomTypeShortcodeSelect = $hasRoomTypeShortcode ? 'rt.shortcode' : "'' AS shortcode";

$sql = "
    SELECT
        r.*,
        p.name AS property_name,
        p.code AS property_code,
        rt.title AS room_type,
        {$roomTypeShortcodeSelect}
    FROM rooms r
    LEFT JOIN properties p ON p.id = r.property_id
    LEFT JOIN room_types rt ON rt.id = r.room_type_id
    WHERE 1=1
";
$params = [];

if ($propertyFilter > 0) {
    $sql .= " AND r.property_id = :property_id ";
    $params[':property_id'] = $propertyFilter;
}

if ($roomTypeFilter > 0) {
    $sql .= " AND r.room_type_id = :room_type_id ";
    $params[':room_type_id'] = $roomTypeFilter;
}

if ($floorFilter > 0) {
    $sql .= " AND r.floor_id = :floor_id ";
    $params[':floor_id'] = $floorFilter;
}

if ($search !== '') {
    $searchValue = '%' . $search . '%';

    $searchParts = [
        "CAST(r.room_no AS CHAR) LIKE :search_room_no",
        "r.status LIKE :search_status",
        "rt.title LIKE :search_room_type",
        "p.name LIKE :search_property_name",
        "p.code LIKE :search_property_code",
    ];

    $params[':search_room_no']       = $searchValue;
    $params[':search_status']        = $searchValue;
    $params[':search_room_type']     = $searchValue;
    $params[':search_property_name'] = $searchValue;
    $params[':search_property_code'] = $searchValue;

    if ($hasRoomTypeShortcode) {
        $searchParts[] = "rt.shortcode LIKE :search_shortcode";
        $params[':search_shortcode'] = $searchValue;
    }

    $sql .= " AND (" . implode(" OR ", $searchParts) . ") ";
}

$sql .= " ORDER BY p.name ASC, r.floor_id ASC, r.room_no ASC";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rooms = $stmt->fetchAll(PDO::FETCH_ASSOC);

$floors = [];
foreach ($rooms as $r) {
    if (!empty($r['floor_id'])) {
        $floors[(int)$r['floor_id']] = (int)$r['floor_id'];
    }
}
for ($i = 1; $i <= 20; $i++) {
    $floors[$i] = $i;
}
ksort($floors);

require_once __DIR__ . '/../../includes/header.php';
?>

<div class="hero-band">
    <div class="row g-4 align-items-center">
        <div class="col-lg-8">
            <div class="page-eyebrow text-white-50"><i class="bi bi-grid-3x3-gap"></i> Masters</div>
            <div class="hero-title mt-2">Rooms</div>
            <div class="hero-subtitle mt-2">
                Manage physical rooms by property, room type, floor, and operational status.
            </div>
        </div>
        <div class="col-lg-4 text-lg-end">
            <a href="rooms.php<?php echo $propertyFilter > 0 ? '?property_id=' . (int)$propertyFilter : ''; ?>" class="btn btn-outline-light">
                <i class="bi bi-plus-circle me-2"></i>New Room
            </a>
        </div>
    </div>
</div>

<?php if ($success): ?>
    <div class="alert alert-success"><?php echo h($success); ?></div>
<?php endif; ?>

<?php if ($errors): ?>
    <div class="alert alert-danger">
        <div class="fw-bold mb-2">Please fix the following:</div>
        <ul class="mb-0">
            <?php foreach ($errors as $err): ?>
                <li><?php echo h($err); ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>

<div class="row g-4">
    <div class="col-xl-5">
        <div class="form-section">
            <div class="form-section-title">
                <i class="bi bi-door-open"></i>
                <?php echo $editing ? 'Edit Room' : 'New Room'; ?>
            </div>

            <form method="post" class="row g-3" id="roomForm">
                <input type="hidden" name="id" value="<?php echo (int)$form['id']; ?>">

                <div class="col-12">
                    <label class="form-label">Property</label>
                    <select name="property_id" id="property_id" class="form-select" required>
                        <option value="">Select property</option>
                        <?php foreach ($properties as $p): ?>
                            <option value="<?php echo (int)$p['id']; ?>" <?php echo (int)$form['property_id'] === (int)$p['id'] ? 'selected' : ''; ?>>
                                <?php echo h($p['name']); ?> - <?php echo h($p['code']); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Floor</label>
                    <select name="floor_id" class="form-select" required>
                        <?php foreach ($floors as $floor): ?>
                            <option value="<?php echo (int)$floor; ?>" <?php echo (int)$form['floor_id'] === (int)$floor ? 'selected' : ''; ?>>
                                Floor <?php echo (int)$floor; ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Room No</label>
                    <input type="number" min="1" name="room_no" class="form-control" value="<?php echo h($form['room_no']); ?>" required>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-select" required>
                        <?php foreach ($statusOptions as $status): ?>
                            <option value="<?php echo h($status); ?>" <?php echo $form['status'] === $status ? 'selected' : ''; ?>>
                                <?php echo h($status); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-12">
                    <label class="form-label">Room Type</label>
                    <select name="room_type_id" id="room_type_id" class="form-select" required>
                        <option value="">Select room type</option>
                        <?php foreach ($roomTypes as $rt): ?>
                            <option value="<?php echo (int)$rt['id']; ?>" <?php echo (int)$form['room_type_id'] === (int)$rt['id'] ? 'selected' : ''; ?>>
                                <?php echo h($rt['title']); ?><?php echo !empty($rt['shortcode']) ? ' - ' . h($rt['shortcode']) : ''; ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-12 d-flex gap-2">
                    <button type="submit" name="save_room" value="1" class="btn btn-primary">
                        <i class="bi bi-save me-2"></i><?php echo $editing ? 'Update Room' : 'Save Room'; ?>
                    </button>
                    <a href="rooms.php<?php echo $propertyFilter > 0 ? '?property_id=' . (int)$propertyFilter : ''; ?>" class="btn btn-light">Reset</a>
                </div>
            </form>
        </div>
    </div>

    <div class="col-xl-7">
        <div class="card panel-card">
            <div class="card-header">
                <div class="d-flex flex-wrap justify-content-between align-items-center gap-2">
                    <h5 class="mb-0">Rooms</h5>

                    <form method="get" class="d-flex flex-wrap gap-2">
                        <select name="property_id" id="property_filter" class="form-select">
                            <option value="0">All properties</option>
                            <?php foreach ($properties as $p): ?>
                                <option value="<?php echo (int)$p['id']; ?>" <?php echo $propertyFilter === (int)$p['id'] ? 'selected' : ''; ?>>
                                    <?php echo h($p['name']); ?> - <?php echo h($p['code']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>

                        <select name="floor_id" class="form-select">
                            <option value="0">All floors</option>
                            <?php foreach ($floors as $floor): ?>
                                <option value="<?php echo (int)$floor; ?>" <?php echo $floorFilter === (int)$floor ? 'selected' : ''; ?>>
                                    Floor <?php echo (int)$floor; ?>
                                </option>
                            <?php endforeach; ?>
                        </select>

                        <select name="room_type_id" id="room_type_filter" class="form-select">
                            <option value="0">All room types</option>
                            <?php foreach (fetch_room_types_by_property($pdo, $propertyFilter) as $rt): ?>
                                <option value="<?php echo (int)$rt['id']; ?>" <?php echo $roomTypeFilter === (int)$rt['id'] ? 'selected' : ''; ?>>
                                    <?php echo h($rt['title']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>

                        <input type="text" name="q" class="form-control" placeholder="Search rooms..." value="<?php echo h($search); ?>">
                        <button class="btn btn-outline-secondary">Search</button>
                    </form>
                </div>
            </div>

            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-modern align-middle mb-0">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Property / Room</th>
                                <th>Floor</th>
                                <th>Room Type</th>
                                <th>Status</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!$rooms): ?>
                                <tr>
                                    <td colspan="6" class="text-center text-muted py-4">No rooms found.</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($rooms as $row): ?>
                                    <?php
                                    $badgeClass = 'text-bg-light text-dark border';
                                    $statusLower = strtolower((string)$row['status']);
                                    if ($statusLower === 'clean') $badgeClass = 'text-bg-success';
                                    elseif ($statusLower === 'dirty') $badgeClass = 'text-bg-danger';
                                    elseif ($statusLower === 'occupied') $badgeClass = 'text-bg-primary';
                                    elseif ($statusLower === 'maintenance') $badgeClass = 'text-bg-warning text-dark';
                                    elseif ($statusLower === 'blocked') $badgeClass = 'text-bg-secondary';
                                    ?>
                                    <tr>
                                        <td><?php echo (int)$row['id']; ?></td>
                                        <td>
                                            <div class="fw-semibold">Room <?php echo h($row['room_no']); ?></div>
                                            <div class="small text-muted">
                                                <?php echo h($row['property_name'] ?? ''); ?>
                                                <?php if (!empty($row['property_code'])): ?>
                                                    路 <?php echo h($row['property_code']); ?>
                                                <?php endif; ?>
                                            </div>
                                        </td>
                                        <td>Floor <?php echo (int)$row['floor_id']; ?></td>
                                        <td>
                                            <div><?php echo h($row['room_type'] ?? ''); ?></div>
                                            <div class="small text-muted"><?php echo h($row['shortcode'] ?? ''); ?></div>
                                        </td>
                                        <td>
                                            <span class="badge <?php echo $badgeClass; ?>">
                                                <?php echo h($row['status']); ?>
                                            </span>
                                        </td>
                                        <td class="text-end">
                                            <div class="d-inline-flex gap-2">
                                                <a href="rooms.php?edit=<?php echo (int)$row['id']; ?>&property_id=<?php echo (int)$row['property_id']; ?>" class="btn btn-sm btn-primary">Edit</a>
                                                <a href="rooms.php?delete=<?php echo (int)$row['id']; ?>&property_id=<?php echo (int)$propertyFilter; ?>"
                                                   class="btn btn-sm btn-outline-danger"
                                                   onclick="return confirm('Delete this room?');">
                                                    Delete
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const propertyField = document.getElementById('property_id');
    const roomTypeField = document.getElementById('room_type_id');
    const propertyFilterField = document.getElementById('property_filter');
    const roomTypeFilterField = document.getElementById('room_type_filter');

    const roomTypesCache = <?php echo json_encode($roomTypesPreload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;

    function resetSelect(selectEl, placeholder, allValue = '') {
        if (!selectEl) return;
        selectEl.innerHTML = '';

        const opt = document.createElement('option');
        opt.value = allValue;
        opt.textContent = placeholder;
        selectEl.appendChild(opt);
    }

    function getRoomTypesForProperty(propertyId) {
        const pid = String(propertyId || '');

        if (pid !== '' && pid !== '0' && roomTypesCache.byProperty && roomTypesCache.byProperty[pid] && roomTypesCache.byProperty[pid].length) {
            return roomTypesCache.byProperty[pid];
        }

        return roomTypesCache.all || [];
    }

    function fillRoomTypes(selectEl, rows, placeholder, selectedValue, allValue = '') {
        resetSelect(selectEl, placeholder, allValue);

        rows.forEach(function (row) {
            const opt = document.createElement('option');
            opt.value = row.id;
            opt.textContent = row.title + (row.shortcode ? ' - ' + row.shortcode : '');

            if (String(selectedValue || '') === String(row.id)) {
                opt.selected = true;
            }

            selectEl.appendChild(opt);
        });
    }

    function refreshRoomTypeSelect(propertyId, selectEl, placeholder, selectedValue, allValue = '') {
        if (!selectEl) return;
        const rows = getRoomTypesForProperty(propertyId);
        fillRoomTypes(selectEl, rows, placeholder, selectedValue, allValue);
    }

    if (propertyField && roomTypeField) {
        propertyField.addEventListener('change', function () {
            refreshRoomTypeSelect(this.value, roomTypeField, 'Select room type', '', '');
        });

        if (!roomTypeField.options.length || roomTypeField.options.length <= 1) {
            refreshRoomTypeSelect(propertyField.value, roomTypeField, 'Select room type', '<?php echo h((string)$form['room_type_id']); ?>', '');
        }
    }

    if (propertyFilterField && roomTypeFilterField) {
        propertyFilterField.addEventListener('change', function () {
            refreshRoomTypeSelect(this.value, roomTypeFilterField, 'All room types', '', '0');
        });

        if (!roomTypeFilterField.options.length || roomTypeFilterField.options.length <= 1) {
            refreshRoomTypeSelect(propertyFilterField.value, roomTypeFilterField, 'All room types', '<?php echo h((string)$roomTypeFilter); ?>', '0');
        }
    }
});
</script>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>