<?php
$rank = ['kritis'=>['Kritis','danger'],'tinggi'=>['Tinggi','warning'],'sedang'=>['Sedang','info'],'rendah'=>['Rendah','secondary']];
$totalMasalah = array_sum(array_map(fn($c)=>$c['jumlah'], $checks));
?>
<div class="d-flex flex-wrap gap-2 mb-3">
  <?php if (can('kualitas_data','export')): ?>
    <a class="btn btn-sm btn-outline-primary" href="<?= url('/kualitas-data/ekspor',['format'=>'xlsx']) ?>"><i class="bi bi-file-earmark-excel me-1"></i>Ekspor Ringkasan</a>
    <a class="btn btn-sm btn-outline-danger" href="<?= url('/kualitas-data/ekspor',['format'=>'pdf']) ?>"><i class="bi bi-file-earmark-pdf me-1"></i>PDF</a>
  <?php endif; ?>
  <form method="post" action="<?= url('/kualitas-data/perbaiki') ?>" class="ms-auto"><?= csrf() ?>
    <button class="btn btn-sm btn-warning" data-konfirmasi="Jalankan perbaikan otomatis? Sistem akan menolkan saldo invoice non-aktif, memperbaiki revenue dobel hitung, mencocokkan ulang revisi, dan mengevaluasi janji bayar." data-konfirmasi-warna="warning">
      <i class="bi bi-tools me-1"></i>Jalankan Perbaikan Otomatis</button></form>
</div>

<?php if ($totalMasalah === 0): ?>
  <div class="alert alert-success"><i class="bi bi-check-circle-fill me-2"></i>Tidak ada masalah kualitas data yang terdeteksi. Seluruh pemeriksaan lolos.</div>
<?php endif; ?>

<div class="card mb-3">
  <div class="card-header"><i class="bi bi-clipboard-check me-1"></i>Hasil Pemeriksaan (<?= count($checks) ?> pemeriksaan)</div>
  <div class="table-wrap"><table class="table table-hover mb-0">
    <thead><tr><th class="ctr">Tingkat</th><th>Pemeriksaan</th><th class="num">Invoice</th><th class="num">Nilai Bruto</th><th class="num">Nilai Saldo</th><th>Saran Tindak Lanjut</th><th class="ctr">Aksi</th></tr></thead>
    <tbody>
    <?php foreach ($checks as $key => $c): [$lbl,$warna] = $rank[$c['tingkat']] ?? ['-','secondary']; ?>
      <tr class="<?= $c['jumlah'] === 0 ? 'text-body-tertiary' : '' ?>">
        <td class="ctr"><?= badge($lbl, $warna) ?></td>
        <td><?= e($c['label']) ?></td>
        <td class="num <?= $c['jumlah']>0?'fw-bold':'' ?>"><?= nf($c['jumlah']) ?></td>
        <td class="num"><?= $c['jumlah']>0 ? rph($c['nilai_bruto']) : '-' ?></td>
        <td class="num"><?= $c['jumlah']>0 ? rph($c['nilai_saldo']) : '-' ?></td>
        <td class="small text-body-secondary"><?= e($c['saran']) ?></td>
        <td class="ctr"><?php if ($c['jumlah'] > 0): ?>
          <a class="btn btn-sm btn-light py-0 px-2" href="<?= url('/kualitas-data/detail',['pemeriksaan'=>$key]) ?>">Lihat</a>
        <?php endif; ?></td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
</div>

<div class="row g-3">
  <div class="col-lg-6">
    <div class="card h-100"><div class="card-header"><i class="bi bi-tags me-1"></i>Tanda Masalah dari Proses Import</div>
      <div class="table-wrap"><table class="table table-sm mb-0"><thead><tr><th>Tanda</th><th class="num">Jumlah Invoice</th></tr></thead>
        <tbody><?php if (!$flags): ?><tr><td colspan="2" class="text-center text-body-secondary py-3">Tidak ada tanda masalah.</td></tr><?php endif; ?>
        <?php foreach ($flags as $f): ?><tr><td><?= e($f['label']) ?></td><td class="num"><?= nf($f['jumlah']) ?></td></tr><?php endforeach; ?>
        </tbody></table></div>
    </div>
  </div>
  <div class="col-lg-6">
    <div class="card mb-3"><div class="card-header"><i class="bi bi-files me-1"></i>Nomor Faktur Pajak Duplikat</div>
      <div class="table-wrap" style="max-height:250px"><table class="table table-sm mb-0"><thead><tr><th>No. Faktur Pajak</th><th class="num">Jumlah</th><th>Invoice</th></tr></thead>
        <tbody><?php if (!$duplicates['faktur_pajak']): ?><tr><td colspan="3" class="text-center text-body-secondary py-3">Tidak ada duplikat.</td></tr><?php endif; ?>
        <?php foreach ($duplicates['faktur_pajak'] as $d): ?>
          <tr><td class="font-monospace small"><?= e($d['tax_invoice_no']) ?></td><td class="num"><?= nf($d['jumlah']) ?></td>
            <td class="small"><?= e(truncate($d['daftar_invoice'],40)) ?></td></tr>
        <?php endforeach; ?></tbody></table></div>
    </div>
    <?php if ($rfGaps): ?>
    <div class="card"><div class="card-header"><i class="bi bi-exclamation-diamond me-1"></i>Celah Penomoran RF</div>
      <div class="card-body"><p class="small text-body-secondary mb-2">Nomor RF berikut tidak ditemukan dalam register — pastikan tidak ada dokumen revisi yang belum dicatat:</p>
        <?php foreach ($rfGaps as $g): ?><span class="badge bg-warning-subtle text-warning-emphasis me-1 mb-1"><?= e($g) ?></span><?php endforeach; ?>
      </div></div>
    <?php endif; ?>
  </div>
</div>

<?php if ($rekonsiliasi): ?>
<div class="card mt-3">
  <div class="card-header"><i class="bi bi-arrow-left-right me-1"></i>Rekonsiliasi Nilai Sistem vs Rumus Excel
    <span class="text-body-secondary fw-normal ms-1">(<?= count($rekonsiliasi) ?> selisih terdeteksi)</span></div>
  <div class="card-body pb-0"><p class="small text-body-secondary">
    Sistem menghitung ulang seluruh nilai dari data transaksi sumber. Selisih di bawah menunjukkan baris yang nilai
    rumus Excel-nya berbeda dari hasil perhitungan sistem — umumnya karena rumus Excel yang rusak, referensi baris salah,
    atau pembulatan.</p></div>
  <div class="table-wrap" style="max-height:340px"><table class="table table-sm table-sticky mb-0">
    <thead><tr><th>Sheet</th><th class="ctr">Baris</th><th>Invoice</th><th class="num">Tagihan Sistem</th><th class="num">Tagihan Excel</th>
      <th class="num">Selisih</th><th class="num">Saldo Sistem</th><th class="num">Saldo Excel</th><th class="num">Selisih</th></tr></thead>
    <tbody><?php foreach ($rekonsiliasi as $r): ?>
      <tr><td><?= e($r['sheet']) ?></td><td class="ctr"><?= (int)$r['baris'] ?></td>
        <td><a href="<?= url('/invoice') ?>?cari_teks=<?= urlencode($r['invoice_no']) ?>"><?= e($r['invoice_no']) ?></a></td>
        <td class="num"><?= rph($r['net_sistem']) ?></td><td class="num"><?= rph($r['net_excel']) ?></td>
        <td class="num <?= abs((float)$r['selisih_net'])>1?'text-danger fw-semibold':'' ?>"><?= rph($r['selisih_net']) ?></td>
        <td class="num"><?= rph($r['saldo_sistem']) ?></td><td class="num"><?= rph($r['saldo_excel']) ?></td>
        <td class="num <?= abs((float)$r['selisih_saldo'])>1?'text-danger fw-semibold':'' ?>"><?= rph($r['selisih_saldo']) ?></td></tr>
    <?php endforeach; ?></tbody></table></div>
</div>
<?php endif; ?>
