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

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

<?php if (!empty($counts)): ?>
    <div class="row g-2 mb-3">
        <div class="col-md-3"><div class="rs-card p-2 text-center small"><strong><?= (int) ($counts['total'] ?? 0) ?></strong><br>Total</div></div>
        <div class="col-md-3"><div class="rs-card p-2 text-center small"><strong><?= (int) ($counts['open_count'] ?? 0) ?></strong><br>Open</div></div>
        <div class="col-md-3"><div class="rs-card p-2 text-center small"><strong><?= (int) ($counts['paid'] ?? 0) ?></strong><br>Paid</div></div>
        <div class="col-md-3"><div class="rs-card p-2 text-center small"><strong><?= number_format((float) ($counts['total_outstanding'] ?? 0), 2) ?></strong><br>Outstanding</div></div>
    </div>
<?php endif; ?>

<form method="get" action="<?= url('/Invoices') ?>" class="rs-card p-2 mb-3">
    <div class="row g-2 align-items-end">
        <div class="col-md-2"><input type="search" name="q" class="form-control form-control-sm" placeholder="Search number / customer…" value="<?= e($filters['q'] ?? '') ?>"></div>
        <div class="col-md-2"><select name="status" class="form-select form-select-sm"><option value="">All statuses</option><?php foreach (\App\Models\Invoice::STATUSES as $st): ?><option value="<?= $st ?>" <?= ($filters['status'] ?? '') === $st ? 'selected' : '' ?>><?= e($st) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-2"><select name="customer_id" class="form-select form-select-sm"><option value="">All customers</option><?php foreach ($customers as $c): ?><option value="<?= (int) $c['customer_id'] ?>" <?= (int) ($filters['customer_id'] ?? 0) === (int) $c['customer_id'] ? 'selected' : '' ?>><?= e($c['name']) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-1"><label class="small"><input type="checkbox" name="overdue" value="1" <?= !empty($filters['overdue']) ? 'checked' : '' ?>> Overdue</label></div>
        <div class="col-md-2"><select name="sort" class="form-select form-select-sm" onchange="this.form.submit()"><option value="newest" <?= ($sort ?? 'newest') === 'newest' ? 'selected' : '' ?>>Newest</option><option value="oldest" <?= ($sort ?? '') === 'oldest' ? 'selected' : '' ?>>Oldest</option><option value="due_asc" <?= ($sort ?? '') === 'due_asc' ? 'selected' : '' ?>>Due date ↑</option><option value="total_asc" <?= ($sort ?? '') === 'total_asc' ? 'selected' : '' ?>>Total ↑</option><option value="total_desc" <?= ($sort ?? '') === 'total_desc' ? 'selected' : '' ?>>Total ↓</option></select></div>
        <div class="col-md-1"><button type="submit" class="btn btn-sm btn-outline-primary w-100"><i class="fas fa-filter"></i></button></div>
    </div>
</form>

<div class="rs-card p-3">
<?php if (empty($invoices)): ?>
    <p class="text-muted small mb-0">No invoices found.</p>
<?php else: ?>
    <table class="table table-sm table-hover mb-2">
        <thead><tr><th>Number</th><th>Customer</th><th>Status</th><th class="text-end">Total</th><th class="text-end">Paid</th><th class="text-end">Balance</th><th>Due</th><th></th></tr></thead>
        <tbody>
            <?php foreach ($invoices as $inv): ?>
                <tr>
                    <td><a href="<?= url('/Invoices/show/' . (int) $inv['invoice_id']) ?>" class="fw-semibold text-decoration-none"><?= e($inv['invoice_number']) ?></a></td>
                    <td class="small"><?= e($inv['customer_name'] ?? '—') ?></td>
                    <td><span class="badge <?= $inv['status'] === 'paid' ? 'bg-success' : ($inv['status'] === 'overdue' ? 'bg-danger' : ($inv['status'] === 'partial' ? 'bg-warning text-dark' : 'bg-secondary')) ?> text-uppercase"><?= e($inv['status']) ?></span></td>
                    <td class="text-end"><?= number_format((float) $inv['grand_total'], 2) ?></td>
                    <td class="text-end"><?= number_format((float) $inv['amount_paid'], 2) ?></td>
                    <td class="text-end fw-bold"><?= number_format((float) $inv['balance_due'], 2) ?></td>
                    <td class="small text-muted"><?= e($inv['due_date'] ?? '—') ?></td>
                    <td class="text-end"><a href="<?= url('/Invoices/show/' . (int) $inv['invoice_id']) ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-eye"></i></a></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <?php $qs = array_filter(['q' => $filters['q'] ?? null, 'status' => $filters['status'] ?? null, 'customer_id' => $filters['customer_id'] ?? null, 'overdue' => $filters['overdue'] ?? null, 'per_page' => $pager['per_page'] !== 25 ? $pager['per_page'] : null, 'sort' => ($sort ?? 'newest') !== 'newest' ? $sort : null]);
    $pageUrl = function (int $n) use ($qs) { $qs['page'] = $n; return url('/Invoices') . '?' . http_build_query($qs); };
    $window = []; $window[] = 1;
    for ($i = max(2, $pager['page'] - 2); $i <= min($pager['last'] - 1, $pager['page'] + 2); $i++) $window[] = $i;
    if ($pager['last'] > 1) $window[] = $pager['last'];
    $window = array_values(array_unique($window));
    ?>
    <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 pt-1">
        <div class="small text-muted">Showing <strong><?= (int) $pager['from'] ?></strong>–<strong><?= (int) $pager['to'] ?></strong> of <strong><?= (int) $pager['total'] ?></strong></div>
        <nav><ul class="pagination pagination-sm mb-0">
            <li class="page-item <?= $pager['page'] <= 1 ? 'disabled' : '' ?>"><a class="page-link" href="<?= $pager['page'] > 1 ? $pageUrl($pager['page'] - 1) : '#' ?>">«</a></li>
            <?php $prev = 0; foreach ($window as $p):
                if ($prev > 0 && $p > $prev + 1) echo '<li class="page-item disabled"><span class="page-link">…</span></li>';
                ?><li class="page-item <?= $p == $pager['page'] ? 'active' : '' ?>"><a class="page-link" href="<?= $pageUrl($p) ?>"><?= $p ?></a></li><?php
                $prev = $p;
            endforeach; ?>
            <li class="page-item <?= $pager['page'] >= $pager['last'] ? 'disabled' : '' ?>"><a class="page-link" href="<?= $pager['page'] < $pager['last'] ? $pageUrl($pager['page'] + 1) : '#' ?>">»</a></li>
        </ul></nav>
    </div>
<?php endif; ?>
</div>