<div class="d-flex gap-2 mb-3">
  <a class="btn btn-sm btn-light" href="<?= url('/aging') ?>"><i class="bi bi-arrow-left me-1"></i>Kembali ke Aging</a>
</div>
<?php if ($bucket < 0): ?>
  <div class="alert alert-info">Pilih salah satu bucket pada halaman Aging Piutang untuk melihat rincian invoice.</div>
<?php endif; ?>
<div class="card">
  <div class="card-header"><i class="bi bi-list-ul me-1"></i>Rincian Invoice
    <span class="text-body-secondary fw-normal ms-1">(<?= nf(count($rows)) ?> invoice)</span></div>
  <div class="table-wrap" style="max-height:70vh"><table class="table table-sm table-hover table-sticky mb-0">
    <thead><tr><th>Invoice</th><th>Faktur Pajak</th><th class="ctr">Tgl Invoice</th><th class="ctr">Jatuh Tempo</th>
      <th>Customer</th><th>Outlet</th><th>Area</th><th>Sales</th>
      <th class="num">Tagihan</th><th class="num">Dibayar</th><th class="num">Saldo</th><th class="ctr">Hari</th><th class="ctr">Status</th></tr></thead>
    <tbody>
    <?php $t=0; foreach ($rows as $r): $t += (float)$r['outstanding_amount']; ?>
      <tr>
        <td><a class="text-decoration-none fw-semibold" href="<?= url('/invoice/'.(int)$r['id']) ?>"><?= e($r['invoice_no']) ?></a></td>
        <td class="small font-monospace"><?= e($r['tax_invoice_no'] ?: '-') ?></td>
        <td class="ctr"><?= tgl($r['invoice_date']) ?></td>
        <td class="ctr"><?= tgl($r['due_date']) ?></td>
        <td><?= e(truncate($r['customer_name'],24)) ?></td>
        <td class="small"><?= e(truncate($r['outlet_name'] ?: '-',20)) ?></td>
        <td class="small"><?= e($r['area_name'] ?: '-') ?></td>
        <td class="small"><?= e($r['salesperson_name'] ?: '-') ?></td>
        <td class="num"><?= rph($r['net_amount']) ?></td>
        <td class="num"><?= rph($r['paid_amount']) ?></td>
        <td class="num cell-strong"><?= rph($r['outstanding_amount']) ?></td>
        <td class="ctr"><?= badge(nf($r['hari_aging']).' hr', agingColor((int)$r['hari_aging'])) ?></td>
        <td class="ctr"><?= statusBadge($r['payment_status_code']) ?></td>
      </tr>
    <?php endforeach; ?>
    <?php if (!$rows): ?><tr><td colspan="13"><div class="empty-state"><i class="bi bi-inbox"></i>Tidak ada invoice pada bucket ini.</div></td></tr><?php endif; ?>
    </tbody>
    <?php if ($rows): ?><tfoot><tr><td colspan="10">TOTAL</td><td class="num"><?= rph($t) ?></td><td colspan="2"></td></tr></tfoot><?php endif; ?>
  </table></div>
</div>
