<?php $qs = array_filter($filters, fn($v)=>$v!==''&&$v!==null); ?>
<form class="filter-bar" method="get" action="<?= url('/pembayaran') ?>">
  <div class="row g-2 align-items-end">
    <div class="col-12 col-md-4 col-lg-3"><label class="form-label" for="pf_cari">Pencarian</label>
      <input type="search" class="form-control form-control-sm" id="pf_cari" name="cari" value="<?= attr($filters['cari'] ?? '') ?>" placeholder="No. pembayaran, referensi, invoice, customer…"></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="pf_d1">Dari Tanggal</label>
      <input type="date" class="form-control form-control-sm" id="pf_d1" name="dari" value="<?= attr($filters['dari'] ?? '') ?>"></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="pf_d2">Sampai Tanggal</label>
      <input type="date" class="form-control form-control-sm" id="pf_d2" name="sampai" value="<?= attr($filters['sampai'] ?? '') ?>"></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="pf_bank">Bank</label>
      <select class="form-select form-select-sm" id="pf_bank" name="bank_id"><option value="">Semua Bank</option>
        <?php foreach ($banks as $b): ?><option value="<?= (int)$b['id'] ?>" <?= (($filters['bank_id'] ?? '')==$b['id'])?'selected':'' ?>><?= e($b['name']) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="pf_metode">Metode</label>
      <select class="form-select form-select-sm" id="pf_metode" 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-6 col-md-3 col-lg-2"><label class="form-label" for="pf_jenis">Jenis</label>
      <select class="form-select form-select-sm" id="pf_jenis" name="payment_type"><option value="">Semua</option>
        <?php foreach ($types as $k=>$v): ?><option value="<?= $k ?>" <?= (($filters['payment_type'] ?? '')===$k)?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?></select></div>
    <div class="col-6 col-md-3 col-lg-2"><label class="form-label" for="pf_status">Status</label>
      <select class="form-select form-select-sm" id="pf_status" name="status"><option value="">Semua</option>
        <?php foreach (['posted'=>'Diposting','draft'=>'Draf','reversed'=>'Dibalik'] as $k=>$v): ?>
          <option value="<?= $k ?>" <?= (($filters['status'] ?? '')===$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('/pembayaran') ?>">Reset</a>
    </div>
  </div>
</form>

<div class="kpi-grid mb-3">
  <div class="kpi kpi-primary"><span class="kpi-label">Jumlah Transaksi</span><span class="kpi-value"><?= nf($tot['jumlah']) ?></span></div>
  <div class="kpi kpi-success"><span class="kpi-label">Total Diterima</span><span class="kpi-value sm"><?= e(rp($tot['total'])) ?></span></div>
  <div class="kpi kpi-info"><span class="kpi-label">Teralokasi</span><span class="kpi-value sm"><?= e(rp($tot['teralokasi'])) ?></span></div>
  <div class="kpi kpi-secondary"><span class="kpi-label">Diposting</span><span class="kpi-value"><?= nf($tot['posted']) ?></span></div>
  <div class="kpi kpi-dark"><span class="kpi-label">Dibalik</span><span class="kpi-value"><?= nf($tot['dibalik']) ?></span></div>
  <div class="kpi kpi-warning"><span class="kpi-label">Menunggu Persetujuan</span><span class="kpi-value"><?= nf($tot['menunggu']) ?></span></div>
</div>

<div class="card">
  <div class="card-header"><i class="bi bi-wallet2 me-1"></i>Daftar Pembayaran <span class="text-body-secondary fw-normal">(<?= nf($p['total']) ?>)</span>
    <div class="card-actions">
      <?php if (can('pembayaran','create')): ?><a class="btn btn-sm btn-primary" href="<?= url('/pembayaran/tambah') ?>"><i class="bi bi-plus-lg me-1"></i>Catat Pembayaran</a><?php endif; ?>
      <?php if (can('pembayaran','export')): ?>
      <div class="dropdown"><button class="btn btn-sm btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown"><i class="bi bi-download me-1"></i>Ekspor</button>
        <ul class="dropdown-menu dropdown-menu-end">
          <?php foreach (['xlsx'=>'Excel','csv'=>'CSV','pdf'=>'PDF'] as $fm=>$lb): ?>
            <li><a class="dropdown-item" href="<?= url('/pembayaran/ekspor', array_merge($qs,['format'=>$fm])) ?>"><?= e($lb) ?></a></li><?php endforeach; ?>
        </ul></div>
      <?php endif; ?>
    </div>
  </div>
  <div class="table-wrap"><table class="table table-hover table-sticky mb-0">
    <thead><tr><th>No. Pembayaran</th><th class="ctr">Tanggal</th><th>Customer</th><th>Invoice</th><th>Metode</th><th>Bank</th>
      <th>Referensi</th><th>Jenis</th><th class="num">Nilai</th><th class="ctr">Status</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-wallet"></i>Belum ada transaksi pembayaran.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $r): ?>
      <tr class="<?= $r['status']==='reversed'?'text-body-tertiary':'' ?>">
        <td><a class="text-decoration-none fw-semibold" href="<?= url('/pembayaran/'.(int)$r['id']) ?>"><?= e($r['payment_no']) ?></a></td>
        <td class="ctr"><?= tgl($r['payment_date']) ?></td>
        <td><?= e(truncate($r['customer_name'] ?: '-',26)) ?></td>
        <td class="small"><?= e(truncate($r['daftar_invoice'] ?: '-',30)) ?>
          <?= (int)$r['jumlah_alokasi'] > 1 ? '<span class="badge bg-info-subtle text-info-emphasis">'.(int)$r['jumlah_alokasi'].' invoice</span>' : '' ?></td>
        <td><?= e($methods[$r['method']] ?? $r['method']) ?></td>
        <td><?= e($r['bank_name'] ?: '-') ?></td>
        <td class="small"><?= e($r['reference_no'] ?: '-') ?></td>
        <td><?= badge($types[$r['payment_type']] ?? $r['payment_type'],'secondary') ?></td>
        <td class="num cell-strong"><?= rph($r['amount']) ?></td>
        <td class="ctr"><?= badge(['posted'=>'Diposting','draft'=>'Draf','reversed'=>'Dibalik'][$r['status']] ?? $r['status'],
                    $r['status']==='posted'?'success':($r['status']==='reversed'?'dark':'warning')) ?>
          <?php if ($r['approval_status']==='pending'): ?><br><?= badge('Menunggu Persetujuan','warning') ?><?php endif; ?></td>
        <td class="ctr nowrap">
          <a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/pembayaran/'.(int)$r['id']) ?>" title="Detail"><i class="bi bi-eye"></i></a>
          <?php if (can('pembayaran','edit') && $r['status'] !== 'reversed'): ?>
            <a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/pembayaran/'.(int)$r['id'].'/ubah') ?>" title="Ubah"><i class="bi bi-pencil"></i></a><?php endif; ?>
          <?php if (can('pembayaran','print')): ?>
            <a class="btn btn-sm btn-light py-0 px-1" target="_blank" href="<?= url('/pembayaran/'.(int)$r['id'].'/cetak') ?>" title="Cetak kuitansi"><i class="bi bi-printer"></i></a><?php endif; ?>
        </td>
      </tr>
    <?php endforeach; ?>
    </tbody>
    <?php if ($p['data']): ?><tfoot><tr><td colspan="8">TOTAL HASIL FILTER</td><td class="num"><?= rph($tot['total']) ?></td><td colspan="2"></td></tr></tfoot><?php endif; ?>
  </table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
