<?php $qs = array_filter($filters, fn($v) => $v !== '' && $v !== null && !is_array($v)); ?>
<?= \App\Core\View::partial('partials/filter_piutang', [
      'filters' => $filters, 'opsi' => $opsi, 'action' => url('/invoice'),
      'tampilkan' => ['cari','area','bu','sales','jobcat','revcat','status','doc_status','tanggal','jatuh_tempo','nilai','outstanding'],
    ]) ?>

<div class="kpi-grid mb-3">
  <div class="kpi kpi-primary"><span class="kpi-label">Jumlah Invoice</span><span class="kpi-value"><?= nf($tot['jumlah']) ?></span><span class="kpi-sub"><?= nf($tot['jumlah_customer']) ?> customer</span></div>
  <div class="kpi kpi-secondary"><span class="kpi-label">Nominal Bruto</span><span class="kpi-value sm"><?= e(rp($tot['bruto'])) ?></span><span class="kpi-sub">Termasuk PPN</span></div>
  <div class="kpi kpi-info"><span class="kpi-label">Total Tagihan</span><span class="kpi-value sm"><?= e(rp($tot['bersih'])) ?></span><span class="kpi-sub">Potongan <?= e(rp($tot['potongan'])) ?></span></div>
  <div class="kpi kpi-success"><span class="kpi-label">Pembayaran</span><span class="kpi-value sm"><?= e(rp($tot['dibayar'])) ?></span></div>
  <div class="kpi kpi-warning"><span class="kpi-label">Saldo Piutang</span><span class="kpi-value sm"><?= e(rp($tot['saldo'])) ?></span></div>
</div>

<form method="post" action="<?= url('/invoice/massal') ?>" id="formMassal">
<?= csrf() ?>
<div class="card">
  <div class="card-header">
    <i class="bi bi-receipt me-1"></i>Daftar Invoice <span class="text-body-secondary fw-normal">(<?= nf($p['total']) ?>)</span>
    <div class="card-actions">
      <?php if (can('invoice','create')): ?><a class="btn btn-sm btn-primary" href="<?= url('/invoice/tambah') ?>"><i class="bi bi-plus-lg me-1"></i>Tambah Invoice</a><?php endif; ?>
      <?php if (can('invoice','export')): ?>
      <div class="dropdown">
        <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" 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 (.xlsx)','csv'=>'CSV','pdf'=>'PDF'] as $fm=>$lb): ?>
          <li><a class="dropdown-item" href="<?= url('/invoice/ekspor', array_merge($qs,['format'=>$fm,'urut'=>$sort,'arah'=>$dir])) ?>"><?= e($lb) ?></a></li>
          <?php endforeach; ?>
          <li><a class="dropdown-item" target="_blank" href="<?= url('/invoice/ekspor', array_merge($qs,['format'=>'print'])) ?>"><i class="bi bi-printer me-2"></i>Cetak</a></li>
        </ul>
      </div>
      <?php endif; ?>
    </div>
  </div>

  <?php if (can('invoice','edit')): ?>
  <div class="stat-strip d-none" data-bulk-bar>
    <span class="s"><span class="lbl">Terpilih</span><span class="val"><span data-bulk-count>0</span> invoice</span></span>
    <div class="d-flex gap-2 align-items-center ms-auto flex-wrap">
      <select class="form-select form-select-sm" name="collection_officer_id" style="width:auto">
        <option value="">Pilih petugas penagihan…</option>
        <?php foreach (\App\Core\Database::i()->all('SELECT id,name FROM collection_officers WHERE deleted_at IS NULL AND is_active=1 ORDER BY name') as $o): ?>
          <option value="<?= (int)$o['id'] ?>"><?= e($o['name']) ?></option>
        <?php endforeach; ?>
      </select>
      <button class="btn btn-sm btn-outline-primary" name="aksi" value="tugaskan_penagih" type="submit">Tugaskan</button>
      <button class="btn btn-sm btn-outline-secondary" name="aksi" value="hitung_ulang" type="submit"
              data-konfirmasi="Hitung ulang seluruh nilai invoice yang dipilih?" data-konfirmasi-warna="primary">Hitung Ulang</button>
    </div>
  </div>
  <?php endif; ?>

  <div class="table-wrap">
    <table class="table table-hover table-sticky mb-0">
      <thead><tr>
        <?php if (can('invoice','edit')): ?><th style="width:32px"><input type="checkbox" class="form-check-input" data-check-all=".row-check"></th><?php endif; ?>
        <th><?= sortLink('invoice_no','No. Invoice',$sort,$dir) ?></th>
        <th>Faktur Pajak</th>
        <th class="ctr"><?= sortLink('invoice_date','Tgl Invoice',$sort,$dir) ?></th>
        <th class="ctr"><?= sortLink('due_date','Jatuh Tempo',$sort,$dir) ?></th>
        <th><?= sortLink('customer','Customer',$sort,$dir) ?></th>
        <th>Outlet</th><th>Area</th><th>Sales</th>
        <th class="num"><?= sortLink('gross_amount','Nominal',$sort,$dir) ?></th>
        <th class="num"><?= sortLink('net_amount','Tagihan',$sort,$dir) ?></th>
        <th class="num"><?= sortLink('outstanding_amount','Saldo',$sort,$dir) ?></th>
        <th class="ctr">Status</th><th class="ctr">Dokumen</th><th class="ctr">Aksi</th>
      </tr></thead>
      <tbody>
      <?php if (!$p['data']): ?><tr><td colspan="15"><div class="empty-state"><i class="bi bi-inbox"></i>Tidak ada invoice yang sesuai filter.</div></td></tr><?php endif; ?>
      <?php foreach ($p['data'] as $r): ?>
        <tr>
          <?php if (can('invoice','edit')): ?><td><input type="checkbox" class="form-check-input row-check" name="ids[]" value="<?= (int)$r['id'] ?>"></td><?php endif; ?>
          <td class="nowrap"><a class="text-decoration-none fw-semibold" href="<?= url('/invoice/'.(int)$r['id']) ?>"><?= e($r['invoice_no']) ?></a></td>
          <td class="small font-monospace nowrap"><?= e($r['tax_invoice_no'] ?: '-') ?></td>
          <td class="ctr"><?= tgl($r['invoice_date']) ?></td>
          <td class="ctr"><?= tgl($r['due_date']) ?></td>
          <td><a class="text-decoration-none" href="<?= url('/kartu-piutang/'.(int)$r['customer_id']) ?>"><?= e(truncate($r['customer_name'],28)) ?></a></td>
          <td><?= e(truncate($r['outlet_name'] ?: '-',22)) ?></td>
          <td><?= e($r['area_name'] ?: '-') ?></td>
          <td><?= e($r['salesperson_name'] ?: '-') ?></td>
          <td class="num"><?= rph($r['gross_amount']) ?></td>
          <td class="num"><?= rph($r['net_amount']) ?></td>
          <td class="num cell-strong"><?= rph($r['outstanding_amount']) ?></td>
          <td class="ctr"><?= statusBadge($r['payment_status_code']) ?></td>
          <td class="ctr"><?= docStatusBadge($r['doc_status']) ?></td>
          <td class="ctr nowrap">
            <a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/invoice/'.(int)$r['id']) ?>" title="Detail"><i class="bi bi-eye"></i></a>
            <?php if (can('invoice','edit')): ?><a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/invoice/'.(int)$r['id'].'/ubah') ?>" title="Ubah"><i class="bi bi-pencil"></i></a><?php endif; ?>
            <?php if (can('invoice','print')): ?><a class="btn btn-sm btn-light py-0 px-1" target="_blank" href="<?= url('/invoice/'.(int)$r['id'].'/cetak') ?>" title="Cetak"><i class="bi bi-printer"></i></a><?php endif; ?>
          </td>
        </tr>
      <?php endforeach; ?>
      </tbody>
      <?php if ($p['data']): ?>
      <tfoot><tr>
        <td colspan="<?= can('invoice','edit') ? 10 : 9 ?>">TOTAL SELURUH HASIL FILTER</td>
        <td class="num"><?= rph($tot['bruto']) ?></td><td class="num"><?= rph($tot['bersih']) ?></td>
        <td class="num"><?= rph($tot['saldo']) ?></td><td colspan="3"></td>
      </tr></tfoot>
      <?php endif; ?>
    </table>
  </div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
</form>
