<div class="kpi-grid mb-3">
  <div class="kpi kpi-primary"><span class="kpi-label">Total Batch</span><span class="kpi-value"><?= nf($stat['total']) ?></span></div>
  <div class="kpi kpi-info"><span class="kpi-label">Total Baris Diproses</span><span class="kpi-value"><?= nf($stat['baris']) ?></span></div>
  <div class="kpi kpi-success"><span class="kpi-label">Baris Tersimpan</span><span class="kpi-value"><?= nf($stat['sukses']) ?></span></div>
  <div class="kpi kpi-danger"><span class="kpi-label">Baris Gagal</span><span class="kpi-value"><?= nf($stat['gagal']) ?></span></div>
</div>
<div class="card">
  <div class="card-header"><i class="bi bi-clock-history me-1"></i>Riwayat Import
    <div class="card-actions">
      <?php if (can('import','create')): ?><a class="btn btn-sm btn-primary" href="<?= url('/import/wizard') ?>"><i class="bi bi-plus-lg me-1"></i>Import Baru</a><?php endif; ?>
    </div>
  </div>
  <div class="table-wrap"><table class="table table-hover mb-0">
    <thead><tr><th>No. Batch</th><th>Berkas / Sheet</th><th>Jenis</th><th class="ctr">Waktu</th><th>Oleh</th>
      <th class="num">Total</th><th class="num">Sukses</th><th class="num">Gagal</th><th class="ctr">Status</th><th class="ctr">Aksi</th></tr></thead>
    <tbody>
    <?php if (!$p['data']): ?><tr><td colspan="10"><div class="empty-state"><i class="bi bi-file-earmark-excel"></i>Belum ada riwayat import.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $b):
      $warna = ['completed'=>'success','completed_with_errors'=>'warning','failed'=>'danger','rolled_back'=>'dark','processing'=>'info','draft'=>'secondary','validated'=>'info'][$b['status']] ?? 'secondary';
      $label = ['completed'=>'Selesai','completed_with_errors'=>'Selesai dengan Kesalahan','failed'=>'Gagal','rolled_back'=>'Dibatalkan','processing'=>'Diproses','draft'=>'Draf','validated'=>'Tervalidasi'][$b['status']] ?? $b['status']; ?>
      <tr>
        <td><a class="text-decoration-none fw-semibold" href="<?= url('/import/'.(int)$b['id']) ?>"><?= e($b['batch_no']) ?></a></td>
        <td><?= e(truncate($b['file_name'],32)) ?><div class="small text-body-secondary">sheet: <?= e($b['sheet_name'] ?: '-') ?></div></td>
        <td><?= badge($b['import_type']==='revision'?'Revisi (RF)':'Invoice', $b['import_type']==='revision'?'warning':'primary') ?></td>
        <td class="ctr small"><?= tglWaktu($b['uploaded_at']) ?></td>
        <td class="small"><?= e($b['uploaded_by_name'] ?: '-') ?></td>
        <td class="num"><?= nf($b['total_rows']) ?></td>
        <td class="num text-success"><?= nf($b['success_rows']) ?></td>
        <td class="num <?= (int)$b['failed_rows']>0?'text-danger':'' ?>"><?= nf($b['failed_rows']) ?></td>
        <td class="ctr"><?= badge($label, $warna) ?></td>
        <td class="ctr nowrap">
          <a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/import/'.(int)$b['id']) ?>" title="Detail"><i class="bi bi-eye"></i></a>
          <?php if ((int)$b['failed_rows'] > 0 || (int)$b['warning_rows'] > 0): ?>
            <a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/import/'.(int)$b['id'].'/baris-gagal') ?>" title="Unduh baris bermasalah"><i class="bi bi-download"></i></a>
          <?php endif; ?>
          <?php if (can('import','delete') && $b['status'] !== 'rolled_back' && (int)$b['success_rows'] > 0): ?>
            <form method="post" action="<?= url('/import/'.(int)$b['id'].'/rollback') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1 text-danger" title="Batalkan import"
                data-konfirmasi="Batalkan seluruh dampak import <?= attr($b['batch_no']) ?>? Invoice, pembayaran, dan revisi dari batch ini akan dikeluarkan dari sistem."><i class="bi bi-arrow-counterclockwise"></i></button></form>
          <?php endif; ?>
        </td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
