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

<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">Revenue Today</div>
            <div class="fs-3 fw-bold"><?= format_money($stats['revenueToday']) ?></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">Orders Today</div>
            <div class="fs-3 fw-bold"><?= $stats['ordersToday'] ?></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">Avg. Spend</div>
            <div class="fs-3 fw-bold"><?= format_money($stats['avgSpend']) ?></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">Open Orders</div>
            <div class="fs-3 fw-bold"><?= $stats['openOrders'] ?></div>
        </div></div>
    </div>
</div>

<div class="d-flex flex-wrap gap-2 mb-3">
    <a href="<?= site_url('pos/orders') ?>" class="btn btn-primary btn-sm"><i class="bi bi-receipt me-1"></i>Orders</a>
    <a href="<?= site_url('dining/reservations') ?>" class="btn btn-outline-primary btn-sm"><i class="bi bi-calendar-check me-1"></i>Table Reservations</a>
    <a href="<?= site_url('pos/menu') ?>" class="btn btn-outline-primary btn-sm"><i class="bi bi-journal-text me-1"></i>Menu</a>
    <a href="<?= site_url('pos/kitchen') ?>" class="btn btn-outline-primary btn-sm"><i class="bi bi-fire me-1"></i>Kitchen Display</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-lg-7">
        <div class="card h-100">
            <div class="card-header">Today's Sales Overview</div>
            <div class="card-body"><canvas id="salesChart" height="110"></canvas></div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="card h-100">
            <div class="card-header">Top Selling Items</div>
            <ul class="list-group list-group-flush">
                <?php if (empty($topItems)): ?>
                    <li class="list-group-item text-center text-muted py-4">No sales recorded yet.</li>
                <?php endif; ?>
                <?php foreach ($topItems as $item): ?>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span class="small"><?= esc($item->item_name) ?></span>
                        <span class="text-end">
                            <span class="fw-semibold"><?= format_money($item->revenue) ?></span>
                            <span class="text-muted small">(<?= (int) $item->qty ?>)</span>
                        </span>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-lg-7">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                Open Orders
                <a href="<?= site_url('pos/orders') ?>" class="small text-decoration-none">View all</a>
            </div>
            <div class="table-responsive">
                <table class="table mb-0 align-middle small">
                    <thead><tr><th>Order</th><th>Type</th><th>Table / Guest</th><th>Status</th></tr></thead>
                    <tbody>
                    <?php if (empty($activeOrders)): ?>
                        <tr><td colspan="4" class="text-center text-muted py-4">No open orders.</td></tr>
                    <?php endif; ?>
                    <?php foreach ($activeOrders as $order): ?>
                        <tr class="cursor-pointer" onclick="window.location='<?= site_url('pos/orders/' . $order->id) ?>'" style="cursor:pointer;">
                            <td>#<?= $order->id ?></td>
                            <td class="text-muted"><?= esc(ucfirst(str_replace('_', ' ', $order->order_type))) ?></td>
                            <td class="text-muted"><?= esc($order->table_number ? 'Table ' . $order->table_number : ($order->guest_name ?: '—')) ?></td>
                            <td><span class="badge bg-light text-dark border"><?= esc(ucfirst(str_replace('_', ' ', $order->status))) ?></span></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                Today's Reservations
                <a href="<?= site_url('dining/reservations') ?>" class="small text-decoration-none">View all</a>
            </div>
            <ul class="list-group list-group-flush">
                <?php if (empty($reservationsToday)): ?>
                    <li class="list-group-item text-center text-muted py-4">No reservations today.</li>
                <?php endif; ?>
                <?php foreach ($reservationsToday as $r): ?>
                    <li class="list-group-item d-flex justify-content-between align-items-center small">
                        <span><?= esc($r->guest_name ?: $r->walk_in_name) ?> · <?= (int) $r->party_size ?> guests</span>
                        <span class="text-muted"><?= esc(date('g:i a', strtotime($r->reservation_time))) ?></span>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>
</div>

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

<?= $this->section('scripts') ?>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.4/dist/chart.umd.min.js"></script>
<script>
new Chart(document.getElementById('salesChart'), {
    type: 'line',
    data: {
        labels: [<?= implode(',', array_map(static fn ($h) => '"' . date('ga', mktime($h, 0)) . '"', array_keys($hourly))) ?>],
        datasets: [{
            label: 'Sales',
            data: [<?= implode(',', $hourly) ?>],
            borderColor: getComputedStyle(document.documentElement).getPropertyValue('--brand').trim() || '#4f46e5',
            backgroundColor: 'color-mix(in srgb, var(--brand) 15%, transparent)',
            fill: true,
            tension: 0.35,
            pointRadius: 2,
        }],
    },
    options: { plugins: { legend: { display: false } } },
});
</script>
<?= $this->endSection() ?>
