<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-0">Room <?= esc($room->room_number) ?></h1>
        <p class="text-muted mb-0">
            <?= esc($room->room_type_name) ?><?= $room->floor ? ' · Floor ' . esc($room->floor) : '' ?> ·
            <span class="badge bg-<?= esc($lookups->roomStatuses[$room->status]['badge'] ?? 'secondary') ?>"><?= esc($lookups->roomStatuses[$room->status]['label'] ?? ucfirst($room->status)) ?></span>
        </p>
    </div>
    <a href="<?= site_url('rooms/' . $room->id . '/edit') ?>" class="btn btn-outline-secondary"><i class="bi bi-pencil me-1"></i>Edit</a>
</div>

<div class="row">
    <div class="col-lg-8 mb-3">
        <?php if ($currentBooking): ?>
        <div class="card shadow-sm mb-3">
            <div class="card-body">
                <h2 class="h6">Current Guest</h2>
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <a href="<?= site_url('guests/' . $currentBooking->guest_id) ?>" class="fw-semibold text-decoration-none"><?= esc($currentBooking->guest_name) ?></a>
                        <div class="text-muted small"><?= esc($currentBooking->check_in_date) ?> &rarr; <?= esc($currentBooking->check_out_date) ?></div>
                    </div>
                    <a href="<?= site_url('bookings/' . $currentBooking->id) ?>" class="btn btn-sm btn-outline-primary">View Booking</a>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <div class="card shadow-sm mb-3">
            <div class="card-body">
                <h2 class="h6">Upcoming Reservations</h2>
                <?php if (empty($upcoming)): ?>
                    <p class="text-muted small mb-0">Nothing booked for this room yet.</p>
                <?php else: ?>
                    <table class="table table-sm mb-0">
                        <thead class="table-light"><tr><th>Guest</th><th>Check In</th><th>Check Out</th><th>Status</th></tr></thead>
                        <tbody>
                        <?php foreach ($upcoming as $b): ?>
                            <tr>
                                <td><a href="<?= site_url('bookings/' . $b->id) ?>" class="text-decoration-none"><?= esc($b->guest_name) ?></a></td>
                                <td class="small"><?= esc(date('d M Y', strtotime($b->check_in_date))) ?></td>
                                <td class="small"><?= esc(date('d M Y', strtotime($b->check_out_date))) ?></td>
                                <td><span class="badge bg-<?= esc($lookups->bookingStatuses[$b->status]['badge'] ?? 'secondary') ?>"><?= esc($lookups->bookingStatuses[$b->status]['label'] ?? ucfirst($b->status)) ?></span></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
            </div>
        </div>

        <div class="row g-3">
            <div class="col-md-6">
                <div class="card shadow-sm h-100">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        Housekeeping History
                        <a href="<?= site_url('housekeeping/new') ?>" class="small text-decoration-none">+ New Task</a>
                    </div>
                    <ul class="list-group list-group-flush">
                        <?php if (empty($housekeepingTasks)): ?>
                            <li class="list-group-item text-center text-muted small py-3">No housekeeping tasks yet.</li>
                        <?php endif; ?>
                        <?php foreach ($housekeepingTasks as $task): ?>
                            <li class="list-group-item d-flex justify-content-between align-items-center small">
                                <a href="<?= site_url('housekeeping/' . $task->id) ?>" class="text-decoration-none"><?= esc($lookups->housekeepingTaskTypes[$task->task_type]['label'] ?? $task->task_type) ?></a>
                                <span class="badge bg-<?= esc($lookups->housekeepingTaskStatuses[$task->status]['badge'] ?? 'secondary') ?>"><?= esc($lookups->housekeepingTaskStatuses[$task->status]['label'] ?? ucfirst($task->status)) ?></span>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card shadow-sm h-100">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        Maintenance History
                        <a href="<?= site_url('maintenance/new') ?>" class="small text-decoration-none">+ New Order</a>
                    </div>
                    <ul class="list-group list-group-flush">
                        <?php if (empty($workOrders)): ?>
                            <li class="list-group-item text-center text-muted small py-3">No work orders yet.</li>
                        <?php endif; ?>
                        <?php foreach ($workOrders as $wo): ?>
                            <li class="list-group-item d-flex justify-content-between align-items-center small">
                                <a href="<?= site_url('maintenance/' . $wo->id) ?>" class="text-decoration-none"><?= esc($wo->title) ?></a>
                                <span class="badge bg-<?= esc($lookups->workOrderStatuses[$wo->status]['badge'] ?? 'secondary') ?>"><?= esc($lookups->workOrderStatuses[$wo->status]['label'] ?? ucfirst($wo->status)) ?></span>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="card shadow-sm mb-3">
            <div class="card-header">Room Info</div>
            <div class="card-body">
                <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Room Type</span><span class="fw-semibold"><?= esc($room->room_type_name) ?></span></div>
                <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Floor</span><span class="fw-semibold"><?= esc($room->floor ?: '—') ?></span></div>
                <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Base Rate</span><span class="fw-semibold"><?= format_money($room->base_rate) ?>/night</span></div>
                <div class="d-flex justify-content-between small"><span class="text-muted">Status</span><span class="badge bg-<?= esc($lookups->roomStatuses[$room->status]['badge'] ?? 'secondary') ?>"><?= esc($lookups->roomStatuses[$room->status]['label'] ?? ucfirst($room->status)) ?></span></div>
            </div>
        </div>

        <div class="card shadow-sm">
            <div class="card-header">Quick Actions</div>
            <div class="card-body d-flex flex-column gap-2">
                <form action="<?= site_url('rooms/' . $room->id . '/status') ?>" method="post">
                    <?= csrf_field() ?>
                    <label class="form-label small fw-semibold">Change Status</label>
                    <div class="input-group input-group-sm">
                        <select class="form-select" name="status">
                            <?php foreach ($lookups->roomStatuses as $key => $meta): ?>
                                <option value="<?= esc($key) ?>" <?= $room->status === $key ? 'selected' : '' ?>><?= esc($meta['label']) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <button type="submit" class="btn btn-outline-primary">Set</button>
                    </div>
                </form>
                <a href="<?= site_url('rooms/board') ?>" class="btn btn-outline-secondary btn-sm mt-2">Back to Room Management</a>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
