<?php $this->start('body'); ?>
<?php $rows = $this->data['rows']; $t = $this->data['totals']; $asOf = $this->data['as_of']; ?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-clock me-2 text-muted"></i>Aged Receivables
    <span class="text-muted fw-normal fs-6">· as of <?= humanDate($asOf); ?></span>
  </h5>
  <form method="GET" action="<?= PROOT; ?>AgedReceivables" class="d-flex gap-2 align-items-center">
    <label class="small text-muted mb-0">As of</label>
    <input type="date" name="as_of" class="form-control form-control-sm" style="width:180px;" value="<?= htmlspecialchars($asOf); ?>">
    <button class="btn btn-sm btn-primary"><i class="fas fa-sync"></i></button>
  </form>
</div>

<?= flashHtml(); ?>

<!-- Bucket totals -->
<div class="row g-2 mb-3">
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Current</div><div class="fs-6 fw-bold"><?= number_format((float)$t['bucket_current'], 0); ?></div></div></div></div>
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">1–30 days</div><div class="fs-6 fw-bold text-info"><?= number_format((float)$t['bucket_0_30'], 0); ?></div></div></div></div>
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">31–60 days</div><div class="fs-6 fw-bold text-warning"><?= number_format((float)$t['bucket_31_60'], 0); ?></div></div></div></div>
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">61–90 days</div><div class="fs-6 fw-bold text-warning"><?= number_format((float)$t['bucket_61_90'], 0); ?></div></div></div></div>
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">90+ days</div><div class="fs-6 fw-bold text-danger"><?= number_format((float)$t['bucket_90_plus'], 0); ?></div></div></div></div>
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100 bg-light"><div class="card-body py-2"><div class="text-uppercase text-muted small">Total</div><div class="fs-6 fw-bold"><?= number_format((float)$t['total_outstanding'], 0); ?></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>Customer</th><th class="money">Invoices</th>
          <th class="money">Current</th><th class="money">1–30</th><th class="money">31–60</th><th class="money">61–90</th><th class="money">90+</th>
          <th class="money">Total</th><th>Oldest</th><th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($rows as $r):
          $oldest = (int)$r['oldest_overdue_days'];
          $sev = $oldest > 90 ? 'danger' : ($oldest > 60 ? 'warning' : ($oldest > 30 ? 'info' : 'secondary'));
        ?>
          <tr>
            <td class="small"><a href="<?= PROOT; ?>CustomerInvoices?customer_id=<?= (int)$r['customer_id']; ?>&status=issued" class="fw-semibold text-decoration-none"><?= htmlspecialchars($r['customer_name']); ?></a>
              <?php if ($r['customer_phone']): ?><div class="text-muted small"><?= htmlspecialchars($r['customer_phone']); ?></div><?php endif; ?></td>
            <td class="money small"><?= (int)$r['invoice_count']; ?></td>
            <td class="money"><?= number_format((float)$r['bucket_current'], 0); ?></td>
            <td class="money"><?= number_format((float)$r['bucket_0_30'], 0); ?></td>
            <td class="money text-warning"><?= number_format((float)$r['bucket_31_60'], 0); ?></td>
            <td class="money text-warning"><?= number_format((float)$r['bucket_61_90'], 0); ?></td>
            <td class="money fw-semibold <?= (float)$r['bucket_90_plus'] > 0 ? 'text-danger' : ''; ?>"><?= number_format((float)$r['bucket_90_plus'], 0); ?></td>
            <td class="money fw-bold"><?= number_format((float)$r['total_outstanding'], 0); ?></td>
            <td><span class="badge bg-<?= $sev; ?>"><?= $oldest > 0 ? $oldest . ' days' : 'current'; ?></span></td>
            <td class="text-end">
              <div class="btn-group btn-group-sm">
                <a href="<?= PROOT; ?>CustomerStatements/pdf/<?= (int)$r['customer_id']; ?>?from=<?= date('Y-01-01'); ?>&to=<?= date('Y-m-d'); ?>" target="_blank" class="btn btn-outline-primary" title="Statement PDF"><i class="fas fa-file-invoice"></i></a>
                <a href="<?= PROOT; ?>CustomerReceipts/record?customer_id=<?= (int)$r['customer_id']; ?>" class="btn btn-outline-primary" title="Record receipt"><i class="fas fa-hand-holding-usd"></i></a>
              </div>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($rows)): ?>
          <tr><td colspan="10" class="text-center text-muted py-5">No open receivables 🎉</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<?php $this->end(); ?>
