<?php $this->start('body'); ?>
<?php $vendors = $this->data['vendors']; $totals = $this->data['totals']; ?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-clock me-2 text-muted"></i>Aged Payables</h5>
    <div class="text-muted small">Every open supplier bill, aged by days past due. Live &mdash; no snapshot.</div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= PROOT; ?>SupplierBills" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Bills</a>
    <button class="btn btn-sm btn-outline-primary" onclick="window.print()"><i class="fas fa-print me-1"></i>Print</button>
  </div>
</div>

<!-- Bucket totals strip -->
<div class="row g-2 mb-3">
  <div class="col"><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-5 fw-bold text-success">TZS <?= number_format($totals['current'], 0); ?></div>
  </div></div></div>
  <div class="col"><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-5 fw-bold text-info">TZS <?= number_format($totals['d1_30'], 0); ?></div>
  </div></div></div>
  <div class="col"><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-5 fw-bold text-warning">TZS <?= number_format($totals['d31_60'], 0); ?></div>
  </div></div></div>
  <div class="col"><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-5 fw-bold text-danger">TZS <?= number_format($totals['d61_90'], 0); ?></div>
  </div></div></div>
  <div class="col"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Over 90</div>
    <div class="fs-5 fw-bold text-danger">TZS <?= number_format($totals['d90_plus'], 0); ?></div>
  </div></div></div>
  <div class="col"><div class="card border-0 shadow-sm h-100 border-primary"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Total owing</div>
    <div class="fs-4 fw-bold text-primary">TZS <?= number_format($totals['grand'], 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>Supplier</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 fw-bold">Total</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($vendors)): ?>
          <tr><td colspan="8" class="text-center text-muted py-5">No open supplier bills.</td></tr>
        <?php else: ?>
          <?php foreach ($vendors as $v): ?>
            <tr>
              <td>
                <a href="<?= PROOT; ?>Suppliers/view/<?= (int)$v['vendor_id']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($v['vendor_name']); ?></a>
                <?php if ($v['vendor_tin']): ?><div class="small text-muted font-monospace">TIN <?= htmlspecialchars($v['vendor_tin']); ?></div><?php endif; ?>
                <div class="small text-muted"><?= count($v['bills']); ?> bill(s)</div>
              </td>
              <td class="money font-monospace small"><?= $v['current'] > 0 ? number_format($v['current'], 2) : '—'; ?></td>
              <td class="money font-monospace small"><?= $v['d1_30']  > 0 ? number_format($v['d1_30'], 2)  : '—'; ?></td>
              <td class="money font-monospace small text-warning"><?= $v['d31_60'] > 0 ? number_format($v['d31_60'], 2) : '—'; ?></td>
              <td class="money font-monospace small text-danger"><?= $v['d61_90'] > 0 ? number_format($v['d61_90'], 2) : '—'; ?></td>
              <td class="money font-monospace small text-danger"><?= $v['d90_plus'] > 0 ? number_format($v['d90_plus'], 2) : '—'; ?></td>
              <td class="money font-monospace fw-bold"><?= number_format($v['grand'], 2); ?></td>
              <td class="text-end">
                <div class="btn-group btn-group-sm">
                  <a href="<?= PROOT; ?>SupplierStatements/pdf/<?= (int)$v['vendor_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; ?>SupplierPayments/record?vendor_id=<?= (int)$v['vendor_id']; ?>" class="btn btn-outline-success" title="Record payment"><i class="fas fa-money-bill-transfer"></i></a>
                </div>
              </td>
            </tr>
            <?php foreach ($v['bills'] as $b): ?>
              <tr class="text-muted small" style="background:#F9FAFC;">
                <td class="ps-4">
                  <a href="<?= PROOT; ?>SupplierBills/view/<?= (int)$b['bill_id']; ?>" class="text-decoration-none font-monospace"><?= htmlspecialchars($b['bill_no']); ?></a>
                  <?php if ($b['supplier_ref']): ?> · <?= htmlspecialchars($b['supplier_ref']); ?><?php endif; ?>
                  <span class="ms-1"><?= humanDate($b['bill_date']); ?><?php if ($b['due_date']): ?> · due <?= humanDate($b['due_date']); ?><?php endif; ?></span>
                  <?php if ($b['days_overdue'] > 0): ?><span class="badge bg-danger ms-1"><?= (int)$b['days_overdue']; ?>d</span><?php endif; ?>
                </td>
                <td colspan="5"></td>
                <td class="money font-monospace"><?= number_format($b['balance_due'], 2); ?></td>
                <td></td>
              </tr>
            <?php endforeach; ?>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
      <tfoot class="table-light fw-bold">
        <tr>
          <th class="text-uppercase small">Totals</th>
          <th class="money font-monospace"><?= number_format($totals['current'], 2); ?></th>
          <th class="money font-monospace"><?= number_format($totals['d1_30'], 2); ?></th>
          <th class="money font-monospace text-warning"><?= number_format($totals['d31_60'], 2); ?></th>
          <th class="money font-monospace text-danger"><?= number_format($totals['d61_90'], 2); ?></th>
          <th class="money font-monospace text-danger"><?= number_format($totals['d90_plus'], 2); ?></th>
          <th class="money font-monospace text-primary"><?= number_format($totals['grand'], 2); ?></th>
          <th></th>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
<?php $this->end(); ?>
