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

<div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
    <h1 class="h3 mb-0">Restaurant Reservations</h1>
    <div class="d-flex gap-2">
        <form action="<?= site_url('dining/reservations') ?>" method="get" class="d-flex gap-2">
            <input type="date" class="form-control form-control-sm" name="date" value="<?= esc($date) ?>" onchange="this.form.submit()">
        </form>
        <a href="<?= site_url('dining/reservations/new?date=' . $date) ?>" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg"></i> New Reservation</a>
    </div>
</div>

<h2 class="h6 text-muted">Floor Plan — <?= esc($date) ?></h2>
<div class="row row-cols-2 row-cols-md-4 row-cols-lg-6 g-3 mb-4">
    <?php foreach ($tables as $table): ?>
        <?php $booked = in_array($table->id, $bookedTableIds, true); ?>
        <div class="col">
            <div class="card h-100 <?= $booked ? 'status-occupied' : 'status-clean' ?> border-0 shadow-sm">
                <div class="card-body text-center py-3">
                    <div class="fs-5 fw-bold"><?= esc($table->table_number) ?></div>
                    <div class="small text-muted"><?= esc($table->area_name) ?> · <?= (int) $table->seating_capacity ?> seats</div>
                    <span class="badge bg-<?= $booked ? 'primary' : 'success' ?> mt-1"><?= $booked ? 'Booked today' : 'Available' ?></span>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
    <?php if (empty($tables)): ?>
        <div class="col-12"><div class="alert alert-warning">No tables set up yet. <a href="<?= site_url('dining/tables') ?>">Add tables &rarr;</a></div></div>
    <?php endif; ?>
</div>

<h2 class="h6 text-muted">Reservations — <?= esc($date) ?></h2>
<div class="card shadow-sm">
    <div class="table-scroll">
        <table class="table table-hover mb-0 align-middle">
            <thead class="table-light">
                <tr><th>Time</th><th>Guest</th><th>Party</th><th>Table</th><th>Status</th><th class="text-end">Actions</th></tr>
            </thead>
            <tbody>
                <?php if (empty($reservations)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-4">No reservations for this date.</td></tr>
                <?php endif; ?>
                <?php foreach ($reservations as $r): ?>
                    <tr>
                        <td><?= esc(substr($r->reservation_time, 0, 5)) ?></td>
                        <td><?= esc($r->guest_name ?: $r->walk_in_name) ?>
                            <?php if (! empty($r->guest_segment) && isset($lookups->guestSegments[$r->guest_segment])): $segMeta = $lookups->guestSegments[$r->guest_segment]; ?>
                                <span class="badge bg-<?= esc($segMeta['badge']) ?>"><?= esc($segMeta['label']) ?></span>
                            <?php endif; ?>
                            <?php if (! empty($r->dining_tier) && isset($lookups->diningMembershipTiers[$r->dining_tier])): $dineMeta = $lookups->diningMembershipTiers[$r->dining_tier]; ?>
                                <span class="badge bg-<?= esc($dineMeta['badge']) ?>"><i class="bi bi-cup-hot"></i> <?= esc($dineMeta['label']) ?></span>
                            <?php endif; ?>
                            <span class="text-muted small"><?= esc($r->guest_phone ?: $r->walk_in_phone) ?></span></td>
                        <td><?= (int) $r->party_size ?></td>
                        <td><?= esc($r->table_number ?: '—') ?></td>
                        <td><span class="badge bg-<?= esc($lookups->restaurantReservationStatuses[$r->status]['badge'] ?? 'secondary') ?>"><?= esc($lookups->restaurantReservationStatuses[$r->status]['label'] ?? $r->status) ?></span></td>
                        <td class="text-end">
                            <?php if ($r->status === 'booked'): ?>
                                <form action="<?= site_url('dining/reservations/' . $r->id . '/seat') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?><button class="btn btn-sm btn-outline-success">Seat</button>
                                </form>
                                <form action="<?= site_url('dining/reservations/' . $r->id . '/no-show') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?><button class="btn btn-sm btn-outline-dark">No-show</button>
                                </form>
                                <form action="<?= site_url('dining/reservations/' . $r->id . '/cancel') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?><button class="btn btn-sm btn-outline-danger">Cancel</button>
                                </form>
                            <?php elseif ($r->status === 'seated'): ?>
                                <form action="<?= site_url('dining/reservations/' . $r->id . '/complete') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?><button class="btn btn-sm btn-outline-primary">Complete</button>
                                </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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