<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-receipt me-2 text-primary"></i>Expenses</h5>
    <a class="btn btn-sm btn-primary" href="<?= url('/Expenses/create') ?>"><i class="fas fa-plus me-1"></i>New Expense</a>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<form method="get" action="<?= url('/Expenses') ?>" class="rs-card p-2 mb-3">
    <div class="row g-2 align-items-end">
        <div class="col-md-4"><input type="search" name="q" class="form-control form-control-sm" placeholder="Search title / vendor / code…" value="<?= e($filters['q'] ?? '') ?>"></div>
        <div class="col-md-3"><select name="status" class="form-select form-select-sm"><option value="">Any status</option><?php foreach (['draft','submitted','approved','rejected','paid','cancelled'] as $s): ?><option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>><?= $s ?></option><?php endforeach; ?></select></div>
        <div class="col-md-3"><select name="category_id" class="form-select form-select-sm"><option value="">All categories</option><?php foreach ($categories as $c): ?><option value="<?= (int) $c['category_id'] ?>" <?= (int) ($filters['category_id'] ?? 0) === (int) $c['category_id'] ? 'selected' : '' ?>><?= e($c['name']) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-2"><button type="submit" class="btn btn-sm btn-outline-primary w-100"><i class="fas fa-filter me-1"></i>Filter</button></div>
    </div>
</form>

<div class="rs-card p-3">
<?php if (empty($expenses)): ?>
    <p class="text-muted small mb-0">No expenses found.</p>
<?php else: ?>
    <table class="table table-sm table-hover mb-0">
        <thead><tr><th>Date</th><th>Code</th><th>Title</th><th>Category</th><th class="text-end">Amount</th><th>Submitter</th><th>Status</th></tr></thead>
        <tbody>
            <?php foreach ($expenses as $x): ?>
                <tr>
                    <td class="small text-muted"><?= e($x['expense_date'] ?? '—') ?></td>
                    <td class="small"><?= e($x['code'] ?? '#' . $x['expense_id']) ?></td>
                    <td><a href="<?= url('/Expenses/show/' . (int) $x['expense_id']) ?>" class="fw-semibold text-decoration-none"><?= e($x['title']) ?></a></td>
                    <td class="small text-muted"><?= e($x['category_name'] ?? '—') ?></td>
                    <td class="text-end"><?= e($x['currency']) ?> <?= number_format((float) $x['amount'], 2) ?></td>
                    <td class="small text-muted"><?= e($x['submitter_username'] ?? '—') ?></td>
                    <td><span class="badge bg-secondary text-uppercase"><?= e($x['status']) ?></span></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>
</div>
