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

<div class="row g-3 mb-3">
    <div class="col-6 col-lg">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Payments Today</div>
            <div class="fs-3 fw-bold"><?= format_money($stats['today']) ?></div>
            <div class="text-muted small"><?= $stats['todayCount'] ?> transaction(s)</div>
        </div></div>
    </div>
    <div class="col-6 col-lg">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">This Month</div>
            <div class="fs-3 fw-bold"><?= format_money($stats['month']) ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Refunds Today</div>
            <div class="fs-3 fw-bold text-danger"><?= format_money($stats['refundedToday']) ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Pending (Hubtel)</div>
            <div class="fs-3 fw-bold text-warning"><?= $stats['pending'] ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Outstanding</div>
            <div class="fs-3 fw-bold text-danger"><?= format_money($stats['outstanding']) ?></div>
        </div></div>
    </div>
</div>

<div class="card">
    <div class="card-header">All Payments</div>
    <div class="table-responsive">
        <table class="table mb-0 align-middle">
            <thead><tr><th>Guest</th><th>Folio</th><th>Method</th><th>Amount</th><th>Status</th><th>When</th></tr></thead>
            <tbody>
            <?php if (empty($transactions)): ?>
                <tr><td colspan="6" class="text-center text-muted py-5">No payments recorded yet.</td></tr>
            <?php endif; ?>
            <?php foreach ($transactions as $tx):
                $badge = ['success' => 'success', 'refunded' => 'danger', 'pending' => 'warning', 'failed' => 'danger'][$tx->tx_status] ?? 'secondary';
                $label = $tx->tx_status === 'refunded' ? 'Refunded' : ucfirst($tx->tx_status);
                $methodLabel = $lookups->paymentMethods[$tx->method]['label'] ?? ucfirst(str_replace('_', ' ', $tx->method));
            ?>
                <tr class="<?= $tx->tx_status === 'refunded' ? 'text-danger' : '' ?>">
                    <td class="small fw-semibold"><?= esc($tx->guest_name ?? 'Unknown') ?></td>
                    <td class="small"><?= $tx->folio_id ? '<a href="' . site_url('folios/' . $tx->folio_id) . '" class="text-decoration-none">FOLIO-' . sprintf('%04d', $tx->folio_id) . '</a>' : '—' ?></td>
                    <td class="small text-muted"><?= esc($methodLabel) ?></td>
                    <td class="fw-semibold"><?= $tx->tx_status === 'refunded' ? '&minus; ' : '' ?><?= format_money($tx->amount) ?></td>
                    <td><span class="badge bg-<?= $badge ?>"><?= esc($label) ?></span></td>
                    <td class="small text-muted"><?= esc(date('d M Y, g:i a', strtotime($tx->when))) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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