<div class="kpi-grid mb-3">
  <div class="kpi kpi-primary"><span class="kpi-label">Total Nota Kredit</span><span class="kpi-value"><?= nf($stat['total']) ?></span></div>
  <div class="kpi kpi-warning"><span class="kpi-label">Menunggu Persetujuan</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><span class="kpi-sub"><?= e(rp($stat['nilai_disetujui'])) ?></span></div>
</div>
<form class="filter-bar" method="get" action="<?= url('/nota-kredit') ?>">
  <div class="row g-2 align-items-end">
    <div class="col-12 col-md-4"><label class="form-label" for="cn_cari">Pencarian</label>
      <input type="search" class="form-control form-control-sm" id="cn_cari" name="cari" value="<?= attr($filters['cari'] ?? '') ?>" placeholder="No. nota kredit, invoice, customer…"></div>
    <div class="col-6 col-md-2"><label class="form-label" for="cn_type">Jenis</label>
      <select class="form-select form-select-sm" id="cn_type" name="cn_type"><option value="">Semua</option>
        <?php foreach ($types as $k=>$v): ?><option value="<?= $k ?>" <?= (($filters['cn_type'] ?? '')===$k)?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-2"><label class="form-label" for="cn_st">Status</label>
      <select class="form-select form-select-sm" id="cn_st" name="status"><option value="">Semua</option>
        <?php foreach (['pending'=>'Menunggu','approved'=>'Disetujui','rejected'=>'Ditolak','draft'=>'Draf'] as $k=>$v): ?>
          <option value="<?= $k ?>" <?= (($filters['status'] ?? '')===$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('nota_kredit','create')): ?><a class="btn btn-primary btn-sm" href="<?= url('/nota-kredit/tambah') ?>"><i class="bi bi-plus-lg me-1"></i>Tambah</a><?php endif; ?>
      <?php if (can('nota_kredit','export')): ?><a class="btn btn-outline-primary btn-sm" href="<?= url('/nota-kredit/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-file-earmark-minus me-1"></i>Daftar Nota Kredit &amp; Penyesuaian <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.</th><th class="ctr">Tanggal</th><th>Jenis</th><th>Invoice</th><th>Customer</th>
      <th class="num">Nilai</th><th class="num">Saldo Invoice</th><th class="ctr">Status</th><th>Alasan</th><th>Dibuat/Disetujui</th><th class="ctr">Aksi</th></tr></thead>
    <tbody>
    <?php if (!$p['data']): ?><tr><td colspan="11"><div class="empty-state"><i class="bi bi-file-earmark-minus"></i>Belum ada nota kredit.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $r): ?>
      <tr>
        <td class="fw-semibold"><?= e($r['cn_no']) ?></td>
        <td class="ctr"><?= tgl($r['cn_date']) ?></td>
        <td><?= badge($types[$r['cn_type']] ?? $r['cn_type'], $r['cn_type']==='HAPUS_BUKU'?'danger':'secondary') ?></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'] ?: '-',24)) ?></td>
        <td class="num cell-strong"><?= rph($r['amount']) ?></td>
        <td class="num"><?= rph($r['outstanding_amount']) ?></td>
        <td class="ctr"><?= badge(['pending'=>'Menunggu','approved'=>'Disetujui','rejected'=>'Ditolak','draft'=>'Draf'][$r['status']] ?? $r['status'],
              ['pending'=>'warning','approved'=>'success','rejected'=>'danger'][$r['status']] ?? 'secondary') ?></td>
        <td class="small"><?= e(truncate($r['reason'],40)) ?></td>
        <td class="small"><?= e($r['created_by_name'] ?: '-') ?><?php if ($r['approved_by_name']): ?><div class="text-success"><?= e($r['approved_by_name']) ?></div><?php endif; ?></td>
        <td class="ctr nowrap">
          <?php if (can('nota_kredit','approve') && $r['status'] === 'pending'): ?>
            <form method="post" action="<?= url('/nota-kredit/'.(int)$r['id'].'/setujui') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1 text-success" data-konfirmasi="Setujui nota kredit ini? Saldo invoice akan berkurang." data-konfirmasi-warna="success"><i class="bi bi-check-lg"></i></button></form>
            <form method="post" action="<?= url('/nota-kredit/'.(int)$r['id'].'/tolak') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1 text-danger" data-konfirmasi="Tolak nota kredit ini?"><i class="bi bi-x-lg"></i></button></form>
          <?php endif; ?>
          <?php if (can('nota_kredit','delete')): ?>
            <form method="post" action="<?= url('/nota-kredit/'.(int)$r['id'].'/hapus') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1 text-danger" data-konfirmasi="Hapus nota kredit ini? Saldo invoice akan dihitung ulang."><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>
