<form class="filter-bar" method="get" action="<?= url('/penagihan/riwayat') ?>">
  <div class="row g-2 align-items-end">
    <div class="col-12 col-md-4 col-lg-3"><label class="form-label" for="rf_cari">Pencarian</label>
      <input type="search" class="form-control form-control-sm" id="rf_cari" name="cari" value="<?= attr($filters['cari'] ?? '') ?>" placeholder="Invoice, customer, narahubung, hasil…"></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="rf_d1">Dari</label>
      <input type="date" class="form-control form-control-sm" id="rf_d1" name="dari" value="<?= attr($filters['dari'] ?? '') ?>"></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="rf_d2">Sampai</label>
      <input type="date" class="form-control form-control-sm" id="rf_d2" name="sampai" value="<?= attr($filters['sampai'] ?? '') ?>"></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="rf_co">Petugas</label>
      <select class="form-select form-select-sm" id="rf_co" name="collection_officer_id"><option value="">Semua</option>
        <?php foreach ($officers as $o): ?><option value="<?= (int)$o['id'] ?>" <?= (($filters['collection_officer_id'] ?? '')==$o['id'])?'selected':'' ?>><?= e($o['name']) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="rf_st">Status</label>
      <select class="form-select form-select-sm" id="rf_st" name="collection_status_id"><option value="">Semua</option>
        <?php foreach ($statuses as $s): ?><option value="<?= (int)$s['id'] ?>" <?= (($filters['collection_status_id'] ?? '')==$s['id'])?'selected':'' ?>><?= e($s['name']) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="rf_m">Metode</label>
      <select class="form-select form-select-sm" id="rf_m" name="method"><option value="">Semua</option>
        <?php foreach ($methods as $k=>$v): ?><option value="<?= $k ?>" <?= (($filters['method'] ?? '')===$k)?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?></select></div>
    <div class="col-12 col-lg-auto d-flex gap-2 ms-lg-auto">
      <button class="btn btn-primary btn-sm" type="submit"><i class="bi bi-funnel me-1"></i>Terapkan</button>
      <a class="btn btn-light btn-sm" href="<?= url('/penagihan/riwayat') ?>">Reset</a>
    </div>
  </div>
</form>
<div class="card">
  <div class="card-header"><i class="bi bi-clock-history me-1"></i>Riwayat Follow-up <span class="text-body-secondary fw-normal">(<?= nf($p['total']) ?>)</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-plus-lg me-1"></i>Catat Follow-up</a><?php endif; ?></div></div>
  <div class="table-wrap"><table class="table table-hover table-sticky mb-0">
    <thead><tr><th class="ctr">Tanggal</th><th>Invoice</th><th>Customer</th><th>Metode</th><th>Narahubung</th><th>Hasil</th>
      <th class="ctr">Status</th><th class="ctr">Prioritas</th><th class="ctr">Janji Bayar</th><th class="ctr">Berikutnya</th><th>Oleh</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-telephone"></i>Belum ada riwayat follow-up.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $r): ?>
      <tr>
        <td class="ctr"><?= tgl($r['followup_date']) ?></td>
        <td><?= $r['invoice_no'] ? '<a href="'.attr(url('/invoice/'.(int)$r['invoice_id'])).'">'.e($r['invoice_no']).'</a><div class="small text-body-secondary">'.e(rp($r['outstanding_amount'])).'</div>' : '-' ?></td>
        <td><?= e(truncate($r['customer_name'] ?: '-',22)) ?></td>
        <td class="small"><?= e($methods[$r['method']] ?? $r['method']) ?></td>
        <td class="small"><?= e($r['contact_person'] ?: '-') ?><?php if ($r['contact_phone']): ?><div class="text-body-secondary"><?= e($r['contact_phone']) ?></div><?php endif; ?></td>
        <td class="small"><?= e(truncate($r['result'] ?: '-',56)) ?></td>
        <td class="ctr"><?= $r['status_name'] ? badge($r['status_name'], $r['status_color'] ?: 'secondary') : '-' ?></td>
        <td class="ctr"><?= badge($r['priority'], ['KRITIS'=>'danger','TINGGI'=>'warning','NORMAL'=>'secondary','RENDAH'=>'light'][$r['priority']] ?? 'secondary') ?></td>
        <td class="ctr small"><?= $r['promise_date'] ? tgl($r['promise_date']).'<div class="text-body-secondary">'.e(rp($r['promise_amount'])).'</div>' : '-' ?></td>
        <td class="ctr small"><?= tgl($r['next_followup_date']) ?></td>
        <td class="small"><?= e($r['officer_name'] ?: $r['created_by_name'] ?: '-') ?></td>
        <td class="ctr nowrap">
          <?php if (can('penagihan','edit')): ?><a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/penagihan/'.(int)$r['id'].'/ubah') ?>"><i class="bi bi-pencil"></i></a><?php endif; ?>
          <?php if (can('penagihan','delete')): ?>
            <form method="post" action="<?= url('/penagihan/'.(int)$r['id'].'/hapus') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1 text-danger" data-konfirmasi="Hapus catatan follow-up ini?"><i class="bi bi-trash"></i></button></form><?php endif; ?>
        </td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
