<?php $qs = array_filter(array_merge($filters,['tanggal_lapor'=>$asOf,'kategori'=>$kategori]), fn($v)=>$v!==''&&$v!==null&&!is_array($v)); ?>
<div class="row g-2 mb-3">
  <?php foreach ($counts as $k => $w): ?>
    <div class="col-6 col-md-4 col-xl">
      <a class="d-block text-decoration-none p-2 rounded border h-100 <?= $kategori===$k ? 'border-'.$w['color'].' bg-'.$w['color'].'-subtle' : 'bg-white' ?>"
         href="<?= url('/penagihan', array_merge($qs,['kategori'=>$k,'hal'=>1])) ?>">
        <div class="d-flex align-items-center gap-2 mb-1">
          <i class="bi bi-<?= e($w['icon']) ?> text-<?= e($w['color']) ?>"></i>
          <small class="fw-semibold text-<?= e($w['color']) ?>-emphasis" style="font-size:11px"><?= e($w['label']) ?></small></div>
        <div class="fw-bold" style="font-size:16px"><?= nf($w['jumlah']) ?></div>
        <div class="text-body-secondary" style="font-size:11.5px"><?= e(rp($w['nilai'])) ?></div>
      </a>
    </div>
  <?php endforeach; ?>
</div>

<?= \App\Core\View::partial('partials/filter_piutang', [
      'filters' => $filters, 'opsi' => $opsi, 'action' => url('/penagihan'), 'asOf' => $asOf,
      'hidden' => ['kategori' => $kategori],
      'tampilkan' => ['cari','area','bu','co','sales','asof','nilai'],
    ]) ?>

<form method="post" action="<?= url('/penagihan/tugaskan') ?>">
<?= csrf() ?>
<div class="card">
  <div class="card-header"><i class="bi bi-<?= e($counts[$kategori]['icon']) ?> me-1"></i><?= e($counts[$kategori]['label']) ?>
    <span class="text-body-secondary fw-normal ms-1">(<?= nf($p['total']) ?> invoice)</span>
    <div class="card-actions">
      <?php if (can('penagihan','create')): ?><a class="btn btn-sm btn-primary" href="<?= url('/penagihan/tambah') ?>"><i class="bi bi-telephone-plus me-1"></i>Catat Follow-up</a><?php endif; ?>
      <?php if (can('penagihan','export')): ?>
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/penagihan/ekspor', array_merge($qs,['format'=>'xlsx'])) ?>"><i class="bi bi-file-earmark-excel me-1"></i>Excel</a>
        <a class="btn btn-sm btn-outline-danger" href="<?= url('/penagihan/ekspor', array_merge($qs,['format'=>'pdf'])) ?>"><i class="bi bi-file-earmark-pdf me-1"></i>PDF</a>
      <?php endif; ?>
    </div>
  </div>

  <?php if (can('penagihan','edit')): ?>
  <div class="stat-strip d-none" data-bulk-bar>
    <span class="s"><span class="lbl">Terpilih</span><span class="val"><span data-bulk-count>0</span> invoice</span></span>
    <div class="d-flex gap-2 align-items-center ms-auto">
      <select class="form-select form-select-sm" name="collection_officer_id" style="width:auto" required>
        <option value="">Pilih petugas penagihan…</option>
        <?php foreach ($officers as $o): ?><option value="<?= (int)$o['id'] ?>"><?= e($o['name']) ?></option><?php endforeach; ?>
      </select>
      <button class="btn btn-sm btn-primary" type="submit">Tugaskan</button>
    </div>
  </div>
  <?php endif; ?>

  <div class="table-wrap"><table class="table table-hover table-sticky mb-0">
    <thead><tr>
      <?php if (can('penagihan','edit')): ?><th style="width:32px"><input type="checkbox" class="form-check-input" data-check-all=".row-check"></th><?php endif; ?>
      <th><?= sortLink('invoice_no','Invoice',(string)App\Core\Request::get('urut','outstanding_amount'),(string)App\Core\Request::get('arah','desc')) ?></th>
      <th><?= sortLink('customer','Customer',(string)App\Core\Request::get('urut','outstanding_amount'),(string)App\Core\Request::get('arah','desc')) ?></th>
      <th>Narahubung</th><th class="ctr"><?= sortLink('due_date','Jatuh Tempo',(string)App\Core\Request::get('urut','outstanding_amount'),(string)App\Core\Request::get('arah','desc')) ?></th>
      <th class="ctr"><?= sortLink('aging','Hari Lewat',(string)App\Core\Request::get('urut','outstanding_amount'),(string)App\Core\Request::get('arah','desc')) ?></th>
      <th class="num"><?= sortLink('outstanding_amount','Saldo',(string)App\Core\Request::get('urut','outstanding_amount'),(string)App\Core\Request::get('arah','desc')) ?></th>
      <th>Petugas</th><th class="ctr">Status Penagihan</th><th class="ctr">Follow-up Terakhir</th><th class="ctr">Janji Bayar</th><th class="ctr">Aksi</th>
    </tr></thead>
    <tbody>
    <?php if (!$p['data']): ?><tr><td colspan="12"><div class="empty-state"><i class="bi bi-check2-circle"></i>Tidak ada invoice pada kategori ini.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $r): $hl = $r['hari_lewat'] === null ? null : (int)$r['hari_lewat']; ?>
      <tr>
        <?php if (can('penagihan','edit')): ?><td><input type="checkbox" class="form-check-input row-check" name="ids[]" value="<?= (int)$r['id'] ?>"></td><?php endif; ?>
        <td class="nowrap"><a class="text-decoration-none fw-semibold" href="<?= url('/invoice/'.(int)$r['id']) ?>"><?= e($r['invoice_no']) ?></a></td>
        <td><a class="text-decoration-none" href="<?= url('/kartu-piutang/'.(int)$r['customer_id']) ?>"><?= e(truncate($r['customer_name'],24)) ?></a>
          <?php if ($r['outlet_name']): ?><div class="small text-body-secondary"><?= e(truncate($r['outlet_name'],24)) ?></div><?php endif; ?></td>
        <td class="small"><?= e($r['contact_person'] ?: '-') ?><?php if ($r['customer_phone']): ?><div class="text-body-secondary"><?= e($r['customer_phone']) ?></div><?php endif; ?></td>
        <td class="ctr"><?= tgl($r['due_date']) ?></td>
        <td class="ctr"><?= $hl===null?'-':badge(nf($hl).' hr', agingColor($hl)) ?></td>
        <td class="num cell-strong"><?= rph($r['outstanding_amount']) ?></td>
        <td class="small"><?= e($r['officer_name'] ?: '—') ?></td>
        <td class="ctr"><?= $r['status_penagihan'] ? badge($r['status_penagihan'], $r['status_warna'] ?: 'secondary') : '<span class="text-body-tertiary small">belum ada</span>' ?></td>
        <td class="ctr small"><?= tgl($r['followup_terakhir']) ?></td>
        <td class="ctr small"><?= $r['janji_bayar'] ? tgl($r['janji_bayar']) . '<div class="text-body-secondary">' . e(rp($r['janji_nilai'])) . '</div>' : '-' ?></td>
        <td class="ctr nowrap">
          <?php if (can('penagihan','create')): ?>
            <a class="btn btn-sm btn-primary py-0 px-1" href="<?= url('/penagihan/tambah',['invoice_id'=>(int)$r['id']]) ?>" title="Catat follow-up"><i class="bi bi-telephone"></i></a><?php endif; ?>
          <a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/invoice/'.(int)$r['id']) ?>" title="Detail invoice"><i class="bi bi-eye"></i></a>
        </td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
</form>
