<div class="d-flex flex-wrap gap-2 mb-3">
  <form class="d-flex gap-2 ms-auto" method="get">
    <input type="search" class="form-control form-control-sm" name="cari" value="<?= attr($cari) ?>" placeholder="Cari customer / kode / NPWP" style="min-width:240px">
    <div class="form-check form-check-inline align-self-center">
      <input class="form-check-input" type="checkbox" id="hb" name="hanya_bersaldo" value="1" <?= App\Core\Request::get('hanya_bersaldo') ? 'checked':'' ?>>
      <label class="form-check-label small" for="hb">Hanya bersaldo</label></div>
    <button class="btn btn-sm btn-primary"><i class="bi bi-search"></i></button>
  </form>
</div>
<div class="card">
  <div class="card-header"><i class="bi bi-person-lines-fill me-1"></i>Kartu Piutang Customer <span class="text-body-secondary fw-normal">(<?= nf($p['total']) ?>)</span></div>
  <div class="table-wrap"><table class="table table-hover table-sticky mb-0">
    <thead><tr><th>Kode</th><th>Customer</th><th>Area</th><th>Sales</th><th>Petugas Penagihan</th>
      <th class="num">Invoice</th><th class="num">Total Tagihan</th><th class="num">Dibayar</th><th class="num">Saldo</th>
      <th class="num">Terlambat</th><th class="num">Rata Hari</th><th class="num">Umur Tertua</th><th class="ctr">Risiko</th><th class="ctr">Aksi</th></tr></thead>
    <tbody>
    <?php if (!$p['data']): ?><tr><td colspan="14"><div class="empty-state"><i class="bi bi-people"></i>Tidak ada customer yang sesuai.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $c): ?>
      <tr>
        <td class="small font-monospace"><?= e($c['code']) ?></td>
        <td><a class="text-decoration-none fw-semibold" href="<?= url('/kartu-piutang/'.(int)$c['id']) ?>"><?= e(truncate($c['name'],30)) ?></a>
          <?php if ($c['npwp']): ?><div class="small text-body-secondary"><?= e($c['npwp']) ?></div><?php endif; ?></td>
        <td class="small"><?= e($c['area_name'] ?: '-') ?></td>
        <td class="small"><?= e($c['salesperson_name'] ?: '-') ?></td>
        <td class="small"><?= e($c['collection_officer_name'] ?: '—') ?></td>
        <td class="num"><?= nf($c['jumlah_invoice']) ?></td>
        <td class="num"><?= rph($c['total_bersih']) ?></td>
        <td class="num"><?= rph($c['total_bayar']) ?></td>
        <td class="num cell-strong"><?= rph($c['total_saldo']) ?></td>
        <td class="num"><?= (float)$c['total_terlambat'] > 0 ? '<span class="text-danger">'.e(rp($c['total_terlambat'])).'</span>' : '-' ?></td>
        <td class="num"><?= $c['rata_hari'] === null ? '-' : nf($c['rata_hari'],0) ?></td>
        <td class="num"><?= $c['maks_umur'] === null ? '-' : badge(nf($c['maks_umur']).' hr', agingColor((int)$c['maks_umur'])) ?></td>
        <td class="ctr"><?= badge(['RENDAH'=>'Rendah','SEDANG'=>'Sedang','TINGGI'=>'Tinggi','SANGAT_TINGGI'=>'Sangat Tinggi'][$c['risk_category']] ?? '-',
              ['RENDAH'=>'success','SEDANG'=>'info','TINGGI'=>'warning','SANGAT_TINGGI'=>'danger'][$c['risk_category']] ?? 'secondary') ?></td>
        <td class="ctr"><a class="btn btn-sm btn-light py-0 px-2" href="<?= url('/kartu-piutang/'.(int)$c['id']) ?>">Buka</a></td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
