<div class="kpi-grid mb-3">
  <div class="kpi kpi-primary"><span class="kpi-label">Total Janji Bayar</span><span class="kpi-value"><?= nf($stat['total']) ?></span></div>
  <div class="kpi kpi-info"><span class="kpi-label">Terbuka</span><span class="kpi-value"><?= nf($stat['terbuka']) ?></span><span class="kpi-sub"><?= e(rp($stat['nilai_terbuka'])) ?></span></div>
  <div class="kpi kpi-success"><span class="kpi-label">Dipenuhi</span><span class="kpi-value"><?= nf($stat['dipenuhi']) ?></span></div>
  <div class="kpi kpi-warning"><span class="kpi-label">Sebagian</span><span class="kpi-value"><?= nf($stat['sebagian']) ?></span></div>
  <div class="kpi kpi-danger"><span class="kpi-label">Ingkar / Gagal</span><span class="kpi-value"><?= nf($stat['gagal']) ?></span><span class="kpi-sub"><?= e(rp($stat['nilai_gagal'])) ?></span></div>
</div>
<form class="filter-bar" method="get" action="<?= url('/penagihan/janji-bayar') ?>">
  <div class="row g-2 align-items-end">
    <div class="col-12 col-md-4"><label class="form-label" for="jb_cari">Pencarian</label>
      <input type="search" class="form-control form-control-sm" id="jb_cari" name="cari" value="<?= attr($filters['cari'] ?? '') ?>" placeholder="Invoice atau customer…"></div>
    <div class="col-6 col-md-2"><label class="form-label" for="jb_st">Status</label>
      <select class="form-select form-select-sm" id="jb_st" name="status"><option value="">Semua</option>
        <?php foreach (['TERBUKA'=>'Terbuka','DIPENUHI'=>'Dipenuhi','SEBAGIAN'=>'Sebagian','GAGAL'=>'Gagal','DIBATALKAN'=>'Dibatalkan'] as $k=>$v): ?>
          <option value="<?= $k ?>" <?= (($filters['status'] ?? '')===$k)?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-2"><label class="form-label" for="jb_d1">Dari</label>
      <input type="date" class="form-control form-control-sm" id="jb_d1" name="dari" value="<?= attr($filters['dari'] ?? '') ?>"></div>
    <div class="col-6 col-md-2"><label class="form-label" for="jb_d2">Sampai</label>
      <input type="date" class="form-control form-control-sm" id="jb_d2" name="sampai" value="<?= attr($filters['sampai'] ?? '') ?>"></div>
    <div class="col-12 col-md-auto d-flex gap-2 ms-md-auto">
      <button class="btn btn-primary btn-sm" type="submit"><i class="bi bi-funnel me-1"></i>Terapkan</button>
      <?php if (can('penagihan','edit')): ?>
      <form method="post" action="<?= url('/penagihan/evaluasi-janji') ?>"><?= csrf() ?>
        <button class="btn btn-warning btn-sm" data-konfirmasi="Evaluasi seluruh janji bayar yang sudah lewat tanggalnya?" data-konfirmasi-warna="warning">
          <i class="bi bi-arrow-repeat me-1"></i>Evaluasi Janji Bayar</button></form>
      <?php endif; ?>
    </div>
  </div>
</form>
<div class="card">
  <div class="card-header"><i class="bi bi-hand-thumbs-up me-1"></i>Daftar Janji Bayar <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 class="ctr">Tgl Janji</th><th>Invoice</th><th>Customer</th><th class="ctr">Jatuh Tempo</th>
      <th class="num">Nilai Dijanjikan</th><th class="num">Realisasi</th><th class="num">Saldo Invoice</th><th class="ctr">Status</th><th class="ctr">Dievaluasi</th><th>Catatan</th></tr></thead>
    <tbody>
    <?php if (!$p['data']): ?><tr><td colspan="10"><div class="empty-state"><i class="bi bi-hand-thumbs-up"></i>Belum ada janji bayar tercatat.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $r): ?>
      <tr>
        <td class="ctr"><?= tgl($r['promise_date']) ?></td>
        <td><?= $r['invoice_no'] ? '<a href="'.attr(url('/invoice/'.(int)$r['invoice_id'])).'">'.e($r['invoice_no']).'</a>' : '-' ?></td>
        <td><?= e(truncate($r['customer_name'] ?: '-',26)) ?></td>
        <td class="ctr"><?= tgl($r['due_date']) ?></td>
        <td class="num cell-strong"><?= rph($r['promise_amount']) ?></td>
        <td class="num"><?= rph($r['actual_paid']) ?></td>
        <td class="num"><?= rph($r['outstanding_amount']) ?></td>
        <td class="ctr"><?= badge(['TERBUKA'=>'Terbuka','DIPENUHI'=>'Dipenuhi','SEBAGIAN'=>'Sebagian','GAGAL'=>'Gagal','DIBATALKAN'=>'Dibatalkan'][$r['status']] ?? $r['status'],
              ['TERBUKA'=>'info','DIPENUHI'=>'success','SEBAGIAN'=>'warning','GAGAL'=>'danger','DIBATALKAN'=>'secondary'][$r['status']] ?? 'secondary') ?></td>
        <td class="ctr small"><?= tglWaktu($r['evaluated_at']) ?></td>
        <td class="small"><?= e(truncate($r['notes'] ?: '-',40)) ?></td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
