<?php
$tenant = service('tenantContext')->hasTenant() ? service('tenantContext')->getTenant() : null;
// safe_hex_color(), not esc(..., 'css') — see app/Helpers/theme_helper.php.
$primaryColor   = safe_hex_color($tenant->primary_color ?? null, '#4f46e5');
$secondaryColor = safe_hex_color($tenant->secondary_color ?? null, '#f59e0b');
$hotelName      = $tenant->hotel_name ?? 'MaxYields PMS';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include APPPATH . 'Views/public/_shell_head.php'; ?>
</head>
<body>
<div class="public-shell public-shell-wide">
    <div class="d-flex align-items-center gap-2 mb-4">
        <div class="brand-mark"><i class="bi bi-buildings-fill"></i></div>
        <div class="brand-name"><?= esc($hotelName) ?></div>
    </div>

    <?php if (session()->getFlashdata('error')): ?>
        <div class="alert alert-danger py-2 small"><?= esc(session()->getFlashdata('error')) ?></div>
    <?php endif; ?>

    <div class="public-card mb-3">
        <h1 class="h5 mb-3">Check Availability</h1>
        <form action="<?= site_url('widget/booking') ?>" method="get" class="row g-2 align-items-end">
            <div class="col-6 col-md-4">
                <label class="form-label small fw-semibold">Check In</label>
                <input type="date" class="form-control" name="check_in" min="<?= date('Y-m-d') ?>" value="<?= esc($checkIn ?? '') ?>" required>
            </div>
            <div class="col-6 col-md-4">
                <label class="form-label small fw-semibold">Check Out</label>
                <input type="date" class="form-control" name="check_out" min="<?= date('Y-m-d', strtotime('+1 day')) ?>" value="<?= esc($checkOut ?? '') ?>" required>
            </div>
            <div class="col-12 col-md-4">
                <button type="submit" class="btn btn-brand w-100"><i class="bi bi-search me-1"></i>Search</button>
            </div>
        </form>
    </div>

    <?php if ($searched): ?>
        <?php if (empty($roomTypes)): ?>
            <div class="public-card text-center text-muted py-4">
                <i class="bi bi-emoji-frown fs-3 d-block mb-2"></i>
                No rooms available for those dates — try different dates.
            </div>
        <?php else: ?>
            <?php foreach ($roomTypes as $type): $amenities = $type['amenities'] ? (json_decode($type['amenities'], true) ?: []) : []; ?>
                <div class="public-card mb-3 d-flex flex-column flex-sm-row gap-3">
                    <?php if ($type['photo_path']): ?>
                        <img src="<?= site_url('uploads/' . $type['photo_path']) ?>" alt="<?= esc($type['name']) ?>" style="width: 100%; max-width: 180px; height: 130px; object-fit: cover; border-radius: 10px;">
                    <?php endif; ?>
                    <div class="flex-grow-1">
                        <div class="d-flex justify-content-between align-items-start">
                            <h2 class="h6 mb-1"><?= esc($type['name']) ?></h2>
                            <span class="badge bg-success-subtle text-success-emphasis"><?= (int) $type['available'] ?> left</span>
                        </div>
                        <?php if ($type['description']): ?><p class="text-muted small mb-2"><?= esc($type['description']) ?></p><?php endif; ?>
                        <div class="d-flex flex-wrap gap-2 mb-2">
                            <span class="text-muted small"><i class="bi bi-people me-1"></i>Sleeps <?= (int) $type['capacity'] ?></span>
                            <?php foreach (array_slice($amenities, 0, 4) as $a): ?>
                                <span class="text-muted small"><i class="bi bi-check2 me-1"></i><?= esc($a) ?></span>
                            <?php endforeach; ?>
                        </div>
                        <div class="d-flex justify-content-between align-items-center">
                            <div><span class="fs-5 fw-bold"><?= format_money($type['rate']) ?></span> <span class="text-muted small">/ night</span></div>
                            <a href="<?= site_url('widget/booking/form?room_type_id=' . $type['id'] . '&check_in=' . $checkIn . '&check_out=' . $checkOut) ?>" class="btn btn-brand btn-sm">Book</a>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    <?php endif; ?>

    <p class="text-center text-muted small mt-4 mb-0">&copy; <?= date('Y') ?> <?= esc($hotelName) ?> · Powered by MaxYields PMS</p>
</div>
</body>
</html>
