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

<?php $lookups = new \Config\Lookups(); ?>

<div class="mb-3 d-flex justify-content-between align-items-start">
    <div>
        <a href="<?= site_url('group-bookings') ?>" class="text-decoration-none small">&larr; Group Bookings</a>
        <h1 class="h3 mb-1 mt-1"><?= esc($group->group_name) ?></h1>
        <p class="text-muted mb-0">
            <?= esc(date('d M Y', strtotime($group->check_in_date))) ?> &rarr; <?= esc(date('d M Y', strtotime($group->check_out_date))) ?>
            <?php if ($group->company_name): ?> &middot; <i class="bi bi-briefcase me-1"></i><?= esc($group->company_name) ?><?php endif; ?>
            <?php if ($group->organizer_name): ?> &middot; Organizer: <?= esc($group->organizer_name) ?><?php if ($group->organizer_phone): ?> (<?= esc($group->organizer_phone) ?>)<?php endif; ?><?php endif; ?>
        </p>
    </div>
    <div class="d-flex align-items-center gap-2">
        <span class="badge bg-<?= $group->status === 'cancelled' ? 'danger' : 'success' ?> fs-6"><?= esc(ucfirst($group->status)) ?></span>
        <?php if ($group->status !== 'cancelled'): ?>
        <form action="<?= site_url('group-bookings/' . $group->id . '/cancel') ?>" method="post" onsubmit="return confirm('Cancel this whole group booking? Rooms already checked in will be left as-is.');">
            <?= csrf_field() ?>
            <button type="submit" class="btn btn-sm btn-outline-danger">Cancel Group</button>
        </form>
        <?php endif; ?>
    </div>
</div>

<?php if ($group->notes): ?>
    <div class="alert alert-light border small"><?= nl2br(esc($group->notes)) ?></div>
<?php endif; ?>

<div class="card shadow-sm">
    <div class="card-header">Rooms (<?= count($rooms) ?>)</div>
    <div class="table-scroll">
        <table class="table align-middle mb-0">
            <thead>
                <tr>
                    <th>Room</th>
                    <th>Type</th>
                    <th>Guest</th>
                    <th>Rate/Night</th>
                    <th>Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($rooms)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-4">No rooms in this group.</td></tr>
                <?php endif; ?>
                <?php foreach ($rooms as $room): ?>
                    <?php $statusMeta = $lookups->bookingStatuses[$room->status] ?? ['label' => ucfirst($room->status), 'badge' => 'secondary']; ?>
                    <tr>
                        <td class="fw-semibold"><?= esc($room->room_number) ?></td>
                        <td><?= esc($room->room_type_name) ?></td>
                        <td>
                            <a href="<?= site_url('guests/' . $room->guest_id) ?>" class="text-decoration-none"><?= esc($room->guest_name) ?></a>
                            <div class="text-muted small"><?= esc($room->guest_phone) ?></div>
                        </td>
                        <td><?= format_money($room->rate_applied) ?></td>
                        <td><span class="badge bg-<?= esc($statusMeta['badge']) ?>"><?= esc($statusMeta['label']) ?></span></td>
                        <td class="text-end">
                            <a href="<?= site_url('bookings/' . $room->id) ?>" class="btn btn-sm btn-outline-secondary">View</a>
                            <?php if (! in_array($room->status, ['checked_in', 'checked_out', 'cancelled'], true)): ?>
                                <form action="<?= site_url('group-bookings/' . $group->id . '/rooms/' . $room->id . '/cancel') ?>" method="post" class="d-inline" onsubmit="return confirm('Cancel this room?');">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-danger">Cancel</button>
                                </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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