<div class="d-flex gap-2 mb-3">
  <a class="btn btn-sm btn-light" href="<?= url('/import') ?>"><i class="bi bi-arrow-left me-1"></i>Riwayat Import</a>
  <?php if ((int)$b['failed_rows'] > 0 || (int)$b['warning_rows'] > 0): ?>
    <a class="btn btn-sm btn-outline-primary" href="<?= url('/import/'.(int)$b['id'].'/baris-gagal') ?>"><i class="bi bi-download me-1"></i>Unduh Baris Bermasalah (Excel)</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') ?>"><?= csrf() ?>
      <button class="btn btn-sm btn-outline-danger" data-konfirmasi="Batalkan seluruh dampak import ini?"><i class="bi bi-arrow-counterclockwise me-1"></i>Batalkan Import (Rollback)</button></form>
  <?php endif; ?>
</div>

<div class="kpi-grid mb-3">
  <div class="kpi kpi-primary"><span class="kpi-label">Total Baris</span><span class="kpi-value"><?= nf($b['total_rows']) ?></span></div>
  <div class="kpi kpi-success"><span class="kpi-label">Tersimpan</span><span class="kpi-value"><?= nf($b['success_rows']) ?></span></div>
  <div class="kpi kpi-danger"><span class="kpi-label">Gagal</span><span class="kpi-value"><?= nf($b['failed_rows']) ?></span></div>
  <div class="kpi kpi-warning"><span class="kpi-label">Duplikat</span><span class="kpi-value"><?= nf($b['duplicate_rows']) ?></span></div>
  <div class="kpi kpi-info"><span class="kpi-label">Peringatan</span><span class="kpi-value"><?= nf($b['warning_rows']) ?></span></div>
  <div class="kpi kpi-secondary"><span class="kpi-label">Baris Kosong</span><span class="kpi-value"><?= nf($b['skipped_rows']) ?></span></div>
</div>

<div class="row g-3">
  <div class="col-lg-4">
    <div class="card mb-3"><div class="card-header"><i class="bi bi-info-circle me-1"></i>Informasi Batch</div>
      <div class="card-body"><dl class="dl-grid">
        <dt>Nomor Batch</dt><dd><?= e($b['batch_no']) ?></dd>
        <dt>Berkas</dt><dd><?= e($b['file_name']) ?></dd>
        <dt>Ukuran</dt><dd><?= ukuranBerkas($b['file_size']) ?></dd>
        <dt>Worksheet</dt><dd><?= e($b['sheet_name'] ?: '-') ?></dd>
        <dt>Jenis Import</dt><dd><?= e($b['import_type']==='revision'?'Revisi Invoice (RF)':'Invoice / Piutang') ?></dd>
        <dt>Diunggah</dt><dd><?= tglWaktu($b['uploaded_at']) ?></dd>
        <dt>Oleh</dt><dd><?= e($b['uploaded_by_name'] ?: '-') ?></dd>
        <dt>Selesai</dt><dd><?= tglWaktu($b['finished_at']) ?></dd>
        <?php if ($b['rolled_back_at']): ?><dt>Dibatalkan</dt><dd class="text-danger"><?= tglWaktu($b['rolled_back_at']) ?></dd><?php endif; ?>
      </dl></div>
    </div>
    <?php if ($mapping): ?>
    <div class="card mb-3"><div class="card-header"><i class="bi bi-diagram-2 me-1"></i>Pemetaan Kolom</div>
      <div class="table-wrap"><table class="table table-sm mb-0"><thead><tr><th>Kolom Excel</th><th>Field Sistem</th></tr></thead>
        <tbody><?php foreach ($mapping as $col => $field): ?><tr><td class="fw-bold"><?= e($col) ?></td><td><?= e($field) ?></td></tr><?php endforeach; ?></tbody>
      </table></div></div>
    <?php endif; ?>
    <?php if (!empty($ringkasan['master_baru'])): ?>
    <div class="card"><div class="card-header"><i class="bi bi-database-add me-1"></i>Master Data Baru</div>
      <div class="card-body"><ul class="small mb-0">
        <?php foreach ($ringkasan['master_baru'] as $k => $v): ?>
          <li><?= e(str_replace('_',' ',$k)) ?>: <strong><?= nf($v['jumlah']) ?></strong>
            <div class="text-body-tertiary" style="font-size:11px"><?= e(implode(', ', array_slice($v['contoh'],0,4))) ?>…</div></li>
        <?php endforeach; ?>
      </ul></div></div>
    <?php endif; ?>
  </div>

  <div class="col-lg-8">
    <?php if (!empty($ringkasan['ringkasan_error'])): ?>
    <div class="alert alert-warning"><strong>Ringkasan kesalahan:</strong>
      <ul class="mb-0 mt-1 small"><?php foreach ($ringkasan['ringkasan_error'] as $k => $v): ?><li>(<?= (int)$v ?>×) <?= e($k) ?></li><?php endforeach; ?></ul></div>
    <?php endif; ?>
    <div class="card">
      <div class="card-header"><i class="bi bi-list-ul me-1"></i>Rincian Baris
        <div class="card-actions">
          <select class="form-select form-select-sm" style="width:auto" onchange="window.location='<?= attr(urlCurrent([], ['status','hal'])) ?>' + (this.value ? (window.location.search.includes('?')?'&':'?') + 'status=' + this.value : '')">
            <option value="">Semua status</option>
            <?php foreach (['ok'=>'Berhasil','warning'=>'Peringatan','failed'=>'Gagal','duplicate'=>'Duplikat','skipped'=>'Dilewati'] as $k=>$v): ?>
              <option value="<?= $k ?>" <?= $statusFilter===$k?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?>
          </select>
        </div>
      </div>
      <div class="table-wrap" style="max-height:640px"><table class="table table-sm table-hover table-sticky mb-0">
        <thead><tr><th class="ctr">Baris</th><th class="ctr">Status</th><th>Invoice</th><th>Pesan</th></tr></thead>
        <tbody>
        <?php foreach ($p['data'] as $r):
          $w = ['ok'=>'success','warning'=>'info','failed'=>'danger','duplicate'=>'warning','skipped'=>'secondary'][$r['status']] ?? 'secondary';
          $lbl = ['ok'=>'Berhasil','warning'=>'Peringatan','failed'=>'Gagal','duplicate'=>'Duplikat','skipped'=>'Dilewati'][$r['status']] ?? $r['status'];
          $msgs = array_merge(arr($r['errors_json']), arr($r['warnings_json'])); ?>
          <tr>
            <td class="ctr"><?= (int)$r['row_number'] ?></td>
            <td class="ctr"><?= badge($lbl, $w) ?></td>
            <td class="small"><?= $r['invoice_id'] ? '<a href="'.attr(url('/invoice/'.(int)$r['invoice_id'])).'">'.e($r['invoice_no']).'</a>' : e($r['invoice_no'] ?: '-') ?></td>
            <td class="small"><?= $msgs ? e(implode(' · ', $msgs)) : '<span class="text-body-tertiary">—</span>' ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody></table></div>
      <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
    </div>
  </div>
</div>
