<?php $this->start('body');
    $rows    = $this->data['rows']    ?? [];
    $buckets = $this->data['buckets'] ?? [];
    $minDays = (int)($this->data['min_days'] ?? 1);
?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-exclamation-triangle me-2 text-muted"></i>Overdue Invoices
            <span class="text-muted ms-2" style="font-weight:400;font-size:.7rem;"><?= count($rows); ?> invoice<?= count($rows) === 1 ? '' : 's'; ?></span>
        </h5>
    </div>
    <div class="d-flex gap-1">
        <a href="<?= PROOT; ?>Sales/overdue?min_days=1" class="btn btn-sm <?= $minDays===1?'btn-dark':'btn-outline-secondary'; ?>">1d+</a>
        <a href="<?= PROOT; ?>Sales/overdue?min_days=14" class="btn btn-sm <?= $minDays===14?'btn-dark':'btn-outline-secondary'; ?>">14d+</a>
        <a href="<?= PROOT; ?>Sales/overdue?min_days=30" class="btn btn-sm <?= $minDays===30?'btn-dark':'btn-outline-secondary'; ?>">30d+</a>
        <a href="<?= PROOT; ?>Sales/overdue?min_days=60" class="btn btn-sm <?= $minDays===60?'btn-dark':'btn-outline-secondary'; ?>">60d+</a>
        <a href="<?= PROOT; ?>CustomerStatement" class="btn btn-sm btn-outline-secondary"><i class="fas fa-file-invoice-dollar me-1"></i>Statements</a>
    </div>
</div>

<div class="row g-2 mb-3">
    <div class="col"><div class="card border-0 shadow-sm"><div class="card-body p-2 text-center">
        <div class="text-muted small" style="font-size:.62rem;text-transform:capitalize;">Current (not yet due)</div>
        <div style="font-weight:700;color:#3F5C43;"><?= number_format((float)$buckets['current_bucket']); ?></div>
    </div></div></div>
    <div class="col"><div class="card border-0 shadow-sm" style="background:#FEF3E7;"><div class="card-body p-2 text-center">
        <div class="text-muted small" style="font-size:.62rem;text-transform:capitalize;">1 – 30 days</div>
        <div style="font-weight:700;color:#8B6914;"><?= number_format((float)$buckets['b_1_30']); ?></div>
    </div></div></div>
    <div class="col"><div class="card border-0 shadow-sm" style="background:#FEF3E7;"><div class="card-body p-2 text-center">
        <div class="text-muted small" style="font-size:.62rem;text-transform:capitalize;">31 – 60 days</div>
        <div style="font-weight:700;color:#8B6914;"><?= number_format((float)$buckets['b_31_60']); ?></div>
    </div></div></div>
    <div class="col"><div class="card border-0 shadow-sm" style="background:#F5EAE1;"><div class="card-body p-2 text-center">
        <div class="text-muted small" style="font-size:.62rem;text-transform:capitalize;">61 – 90 days</div>
        <div style="font-weight:700;color:#C48579;"><?= number_format((float)$buckets['b_61_90']); ?></div>
    </div></div></div>
    <div class="col"><div class="card border-0 shadow-sm" style="background:#F5EAE1;"><div class="card-body p-2 text-center">
        <div class="text-muted small" style="font-size:.62rem;text-transform:capitalize;">90+ days</div>
        <div style="font-weight:700;color:#C48579;"><?= number_format((float)$buckets['b_over_90']); ?></div>
    </div></div></div>
    <div class="col"><div class="card border-0 shadow-sm" style="background:#334155;"><div class="card-body p-2 text-center">
        <div style="font-size:.62rem;text-transform:capitalize;color:#94A3B8;">Total outstanding</div>
        <div style="font-weight:700;color:#fff;"><?= number_format((float)$buckets['total_outstanding']); ?></div>
    </div></div></div>
</div>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Invoice #</th>
                    <th>Customer</th>
                    <th>Project</th>
                    <th class="text-end">Total</th>
                    <th class="text-end">Paid</th>
                    <th class="text-end">Outstanding</th>
                    <th class="text-end">Due date</th>
                    <th class="text-end">Days over</th>
                    <th>Reminders</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $days = (int)$r['days_overdue'];
                    $col = $days <= 0 ? '#3F5C43' : ($days <= 30 ? '#8B6914' : '#C48579');
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>Sales/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars((string)$r['sale_no']); ?></strong></a>
                            <div class="text-muted" style="font-size:.6rem;"><?= htmlspecialchars((string)$r['invoice_date']); ?></div>
                        </td>
                        <td class="small"><?= htmlspecialchars((string)$r['customer_name']); ?></td>
                        <td class="small text-muted"><?= htmlspecialchars((string)($r['project_no'] ?? '—')); ?></td>
                        <td class="text-end small"><?= htmlspecialchars((string)$r['currency']); ?> <?= number_format((float)$r['total']); ?></td>
                        <td class="text-end small text-muted"><?= number_format((float)$r['amount_paid'] + (float)$r['wht_settled_amount']); ?></td>
                        <td class="text-end small" style="font-weight:700;color:<?= $col; ?>;"><?= number_format((float)$r['outstanding_amount']); ?></td>
                        <td class="text-end small text-muted"><?= htmlspecialchars((string)$r['due_date']); ?></td>
                        <td class="text-end small" style="font-weight:600;color:<?= $col; ?>;"><?= $days; ?>d</td>
                        <td class="small text-muted">
                            <?= (int)$r['reminder_count']; ?> sent
                            <?php if (!empty($r['last_reminder_sent_at'])): ?>
                                <div style="font-size:.6rem;">last: <?= htmlspecialchars(substr((string)$r['last_reminder_sent_at'], 0, 16)); ?></div>
                            <?php endif; ?>
                        </td>
                        <td class="text-end"><a href="<?= PROOT; ?>Sales/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="10" class="text-center text-muted py-4">Nothing overdue at this threshold — nice clean AR.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
