<div class="kpi-grid mb-3">
  <div class="kpi kpi-warning"><span class="kpi-label">Menunggu Keputusan</span><span class="kpi-value"><?= nf($stat['menunggu']) ?></span><span class="kpi-sub"><?= e(rp($stat['nilai_menunggu'])) ?></span></div>
  <div class="kpi kpi-success"><span class="kpi-label">Disetujui</span><span class="kpi-value"><?= nf($stat['disetujui']) ?></span></div>
  <div class="kpi kpi-danger"><span class="kpi-label">Ditolak</span><span class="kpi-value"><?= nf($stat['ditolak']) ?></span></div>
  <div class="kpi kpi-primary"><span class="kpi-label">Total Permintaan</span><span class="kpi-value"><?= nf($stat['total']) ?></span></div>
</div>
<form class="filter-bar" method="get" action="<?= url('/persetujuan') ?>">
  <div class="row g-2 align-items-end">
    <div class="col-12 col-md-4"><label class="form-label" for="ap_cari">Pencarian</label>
      <input type="search" class="form-control form-control-sm" id="ap_cari" name="cari" value="<?= attr($filters['cari'] ?? '') ?>" placeholder="Nomor permintaan atau judul…"></div>
    <div class="col-6 col-md-2"><label class="form-label" for="ap_st">Status</label>
      <select class="form-select form-select-sm" id="ap_st" name="status">
        <?php foreach (['pending'=>'Menunggu','approved'=>'Disetujui','rejected'=>'Ditolak','cancelled'=>'Dibatalkan'] as $k=>$v): ?>
          <option value="<?= $k ?>" <?= (($filters['status'] ?? 'pending')===$k)?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-2"><label class="form-label" for="ap_mod">Modul</label>
      <select class="form-select form-select-sm" id="ap_mod" name="module"><option value="">Semua</option>
        <?php foreach (['pembayaran'=>'Pembayaran','invoice'=>'Invoice','revisi'=>'Revisi Invoice','nota_kredit'=>'Nota Kredit','hapus_buku'=>'Hapus Buku','import'=>'Import'] as $k=>$v): ?>
          <option value="<?= $k ?>" <?= (($filters['module'] ?? '')===$k)?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?></select></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('persetujuan','export')): ?><a class="btn btn-outline-primary btn-sm" href="<?= url('/persetujuan-ekspor',['format'=>'xlsx']) ?>"><i class="bi bi-download"></i></a><?php endif; ?>
    </div>
  </div>
</form>
<div class="card">
  <div class="card-header"><i class="bi bi-check2-square me-1"></i>Permintaan Persetujuan <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>No. Permintaan</th><th class="ctr">Diajukan</th><th>Modul / Aksi</th><th>Judul</th>
      <th class="num">Nilai</th><th>Diajukan Oleh</th><th class="ctr">Tingkat</th><th class="ctr">Status</th><th class="ctr">Aksi</th></tr></thead>
    <tbody>
    <?php if (!$p['data']): ?><tr><td colspan="9"><div class="empty-state"><i class="bi bi-check2-circle"></i>Tidak ada permintaan persetujuan.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $r): ?>
      <tr>
        <td><a class="text-decoration-none fw-semibold" href="<?= url('/persetujuan/'.(int)$r['id']) ?>"><?= e($r['request_no']) ?></a></td>
        <td class="ctr small"><?= tglWaktu($r['created_at']) ?></td>
        <td class="small"><?= e($r['flow_name'] ?: $r['module'] . ' / ' . $r['action']) ?></td>
        <td class="small"><?= e(truncate($r['title'],56)) ?></td>
        <td class="num"><?= rph($r['amount']) ?></td>
        <td class="small"><?= e($r['requested_by_name'] ?: '-') ?></td>
        <td class="ctr small"><?= (int)$r['current_level'] ?>/<?= (int)$r['total_level'] ?></td>
        <td class="ctr"><?= badge(['pending'=>'Menunggu','approved'=>'Disetujui','rejected'=>'Ditolak','cancelled'=>'Dibatalkan'][$r['status']] ?? $r['status'],
              ['pending'=>'warning','approved'=>'success','rejected'=>'danger','cancelled'=>'secondary'][$r['status']] ?? 'secondary') ?></td>
        <td class="ctr"><a class="btn btn-sm btn-light py-0 px-2" href="<?= url('/persetujuan/'.(int)$r['id']) ?>">Tinjau</a></td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
