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

<div class="d-flex justify-content-end mb-3">
    <a href="<?= site_url('events/new') ?>" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>New Event</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Total Events</div>
            <div class="fs-3 fw-bold"><?= $stats['total'] ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Upcoming (30 days)</div>
            <div class="fs-3 fw-bold"><?= $stats['upcoming'] ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Confirmed</div>
            <div class="fs-3 fw-bold text-success"><?= $stats['statusCounts']['confirmed'] ?? 0 ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Inquiries</div>
            <div class="fs-3 fw-bold text-secondary"><?= $stats['statusCounts']['inquiry'] ?? 0 ?></div>
        </div></div>
    </div>
</div>

<div class="card">
    <div class="table-responsive">
        <table class="table mb-0 align-middle">
            <thead><tr><th>Date</th><th>Event</th><th>Client</th><th>Headcount</th><th>Status</th><th></th></tr></thead>
            <tbody>
            <?php if (empty($events)): ?>
                <tr><td colspan="6" class="text-center text-muted py-5">No events yet.</td></tr>
            <?php endif; ?>
            <?php foreach ($events as $event): ?>
                <tr class="cursor-pointer" onclick="window.location='<?= site_url('events/' . $event->id) ?>'" style="cursor:pointer">
                    <td class="small"><?= esc(date('d M Y', strtotime($event->event_date))) ?></td>
                    <td class="fw-semibold small"><?= esc($event->event_name) ?></td>
                    <td class="small text-muted"><?= esc($event->guest_name ?: $event->company_name) ?></td>
                    <td class="small"><?= esc($event->expected_headcount ?: '—') ?></td>
                    <td><span class="badge bg-<?= esc($lookups->eventStatuses[$event->status]['badge'] ?? 'secondary') ?>"><?= esc($lookups->eventStatuses[$event->status]['label'] ?? $event->status) ?></span></td>
                    <td class="text-end"><a href="<?= site_url('events/' . $event->id) ?>" class="btn btn-sm btn-outline-secondary" onclick="event.stopPropagation();"><i class="bi bi-eye"></i></a></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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