<?php
declare(strict_types=1);

/**
 * @var \App\View\AppView $this
 * @var \Cake\Collection\CollectionInterface<\App\Model\Entity\Booking> $bookings
 * @var array<string, int> $metrics
 */
$bookingList = $bookings->toList();
?>
<?php $this->assign('title', 'Bookings · CandleCraft Admin'); ?>

<section class="cc-admin-page">
    <div class="cc-admin-page__head">
        <div>
            <div class="cc-eyebrow" style="margin-bottom: 12px;">Bookings</div>
            <h1>All bookings</h1>
        </div>
        <div class="cc-admin-page__stats">
            <div class="cc-admin-stat"><strong><?= (int)$metrics['paid'] ?></strong><span>Paid</span></div>
            <div class="cc-admin-stat"><strong><?= (int)$metrics['pending'] ?></strong><span>Pending</span></div>
            <div class="cc-admin-stat"><strong><?= (int)$metrics['cancelled'] ?></strong><span>Cancelled</span></div>
            <div class="cc-admin-stat"><strong><?= (int)$metrics['total'] ?></strong><span>All-time</span></div>
        </div>
    </div>

    <table class="cc-inbox-table">
        <thead>
            <tr class="cc-inbox-row cc-inbox-row--head">
                <th scope="col">Customer</th>
                <th scope="col" data-priority="2">Teacher</th>
                <th scope="col" data-priority="2">Class time</th>
                <th scope="col" data-priority="3">Amount</th>
                <th scope="col" data-priority="1">Status</th>
                <th scope="col" style="text-align:right;">Ref</th>
            </tr>
        </thead>
        <tbody>
        <?php if (empty($bookingList)): ?>
            <tr><td colspan="6"><div class="cc-inbox-empty"><h3>No bookings yet.</h3><p>They'll show up here as they come in.</p></div></td></tr>
        <?php else: ?>
            <?php foreach ($bookingList as $b):
                $slot = $b->slot ?? null;
                $teacher = $b->teacher ?? null;
            ?>
                <tr class="cc-inbox-row cc-inbox-row--data">
                    <td>
                        <div class="cc-inbox-sender-name"><?= h($b->customer_name) ?></div>
                        <div class="cc-inbox-sender-email"><?= h($b->customer_email) ?></div>
                    </td>
                    <td data-priority="2"><?= h($teacher->full_name ?? '—') ?></td>
                    <td data-priority="2" class="cc-inbox-submitted"><?= h($slot && $slot->starts_at ? $slot->starts_at->format('d M · g:i A') : '—') ?></td>
                    <td data-priority="3"><strong>A$<?= h($b->getAmountDollars()) ?></strong></td>
                    <td data-priority="1"><span class="cc-badge cc-badge--<?= h($b->status) ?>"><?= h(ucfirst($b->status)) ?></span></td>
                    <td style="text-align: right; font-size: 12.5px; color: var(--cc-ink-3); white-space: nowrap;">
                        CC-<?= sprintf('%06d', (int)$b->id) ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
        </tbody>
    </table>
</section>
