<div class="d-flex flex-wrap gap-2 mb-3">
  <a class="btn btn-sm btn-light" href="<?= url('/revisi') ?>"><i class="bi bi-arrow-left me-1"></i>Kembali</a>
  <?php if (can('revisi','edit') && $rev['status'] !== 'approved'): ?>
    <a class="btn btn-sm btn-outline-primary" href="<?= url('/revisi/'.(int)$rev['id'].'/ubah') ?>"><i class="bi bi-pencil me-1"></i>Ubah</a><?php endif; ?>
  <?php if (can('revisi','approve') && empty($rev['applied_at'])): ?>
    <form method="post" action="<?= url('/revisi/'.(int)$rev['id'].'/setujui') ?>"><?= csrf() ?>
      <button class="btn btn-sm btn-success" data-konfirmasi="Setujui dan terapkan revisi ini?" data-konfirmasi-warna="success"><i class="bi bi-check-lg me-1"></i>Setujui &amp; Terapkan</button></form>
  <?php endif; ?>
  <?php if (can('revisi','approve')): ?>
    <button class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#modalTolak">
      <i class="bi bi-x-lg me-1"></i><?= empty($rev['applied_at']) ? 'Tolak' : 'Batalkan Penerapan' ?></button>
  <?php endif; ?>
</div>

<?php if ($rev['status'] === 'menunggu_pencocokan'): ?>
  <div class="alert alert-warning"><i class="bi bi-link-45deg me-1"></i><strong>Menunggu pencocokan.</strong>
    <?= e($rev['match_notes'] ?: 'Invoice terkait belum ditemukan di database.') ?>
    Impor invoice tahun sebelumnya lalu jalankan <em>Cocokkan Ulang</em> di halaman daftar revisi.</div>
<?php endif; ?>
<?php if (!empty($rev['applied_at'])): ?>
  <div class="alert alert-success"><i class="bi bi-check-circle-fill me-1"></i>Revisi telah diterapkan pada <?= tglWaktu($rev['applied_at']) ?>.
    Invoice lama sudah dikeluarkan dari piutang aktif dan pendapatannya tidak lagi dihitung.</div>
<?php endif; ?>

<div class="row g-3">
  <div class="col-lg-5">
    <div class="card"><div class="card-header"><i class="bi bi-file-earmark-text me-1"></i>Informasi Revisi</div>
      <div class="card-body"><dl class="dl-grid">
        <dt>Nomor RF</dt><dd class="fw-bold"><?= e($rev['rf_no']) ?></dd>
        <dt>Tanggal RF</dt><dd><?= tgl($rev['rf_date']) ?><?php if (!$rev['rf_date'] && $rev['rf_date_raw']): ?>
          <span class="text-danger small">(nilai asli: <?= e($rev['rf_date_raw']) ?>)</span><?php endif; ?></dd>
        <dt>Jenis Revisi</dt><dd><?= e($rev['jenis_revisi'] ?: '-') ?></dd>
        <dt>Tindakan Invoice Lama</dt><dd><?= badge(\App\Services\RevisionService::ACTIONS[$rev['old_invoice_action']] ?? $rev['old_invoice_action'],
            ['replaced'=>'secondary','cancelled'=>'dark','revised'=>'info','written_off'=>'danger'][$rev['old_invoice_action']] ?? 'secondary') ?></dd>
        <dt>Nominal Lama</dt><dd><?= rph($rev['original_amount']) ?></dd>
        <dt>Nominal Baru</dt><dd><?= rph($rev['revised_amount']) ?></dd>
        <dt>Selisih</dt><dd><?= rph((float)$rev['revised_amount'] - (float)$rev['original_amount']) ?></dd>
        <dt>Status</dt><dd><?= badge(['pending'=>'Menunggu','approved'=>'Disetujui','rejected'=>'Ditolak','menunggu_pencocokan'=>'Belum Cocok','draft'=>'Draf'][$rev['status']] ?? $rev['status'],
            ['pending'=>'warning','approved'=>'success','rejected'=>'danger','menunggu_pencocokan'=>'secondary'][$rev['status']] ?? 'secondary') ?></dd>
        <dt>Diajukan Oleh</dt><dd><?= e($rev['requested_by_name'] ?: 'Import') ?> · <?= tglWaktu($rev['created_at']) ?></dd>
        <?php if ($rev['approved_by_name']): ?><dt>Disetujui Oleh</dt><dd><?= e($rev['approved_by_name']) ?> · <?= tglWaktu($rev['approved_at']) ?></dd><?php endif; ?>
        <dt>Sumber</dt><dd><?= e($rev['source']==='import'?'Import Excel':'Input Manual') ?></dd>
      </dl>
      <?php if ($rev['reason']): ?><hr><div class="small"><strong>Alasan revisi:</strong><br><?= nlbr($rev['reason']) ?></div><?php endif; ?>
      </div></div>
  </div>
  <div class="col-lg-7">
    <div class="row g-3">
      <?php foreach ([['Invoice Lama', $old, 'danger'], ['Invoice Pengganti', $new, 'success']] as [$lbl, $iv, $warna]): ?>
      <div class="col-md-6"><div class="card h-100">
        <div class="card-header text-<?= $warna ?>"><i class="bi bi-receipt me-1"></i><?= e($lbl) ?></div>
        <div class="card-body">
          <?php if (!$iv): ?>
            <p class="text-body-secondary small mb-0">
              <?= $lbl === 'Invoice Pengganti' ? 'Tidak ada invoice pengganti (invoice lama dibatalkan/dihapuskan).' : 'Invoice tidak ditemukan di database.' ?></p>
          <?php else: ?>
            <dl class="dl-grid" style="grid-template-columns:minmax(90px,120px) 1fr">
              <dt>Nomor</dt><dd><a class="fw-bold" href="<?= url('/invoice/'.(int)$iv['id']) ?>"><?= e($iv['invoice_no']) ?></a></dd>
              <dt>Tanggal</dt><dd><?= tgl($iv['invoice_date']) ?></dd>
              <dt>Faktur Pajak</dt><dd class="small font-monospace"><?= e($iv['tax_invoice_no'] ?: '-') ?></dd>
              <dt>Customer</dt><dd><?= e($iv['customer_name'] ?? '-') ?></dd>
              <dt>Nominal</dt><dd><?= rph($iv['gross_amount']) ?></dd>
              <dt>Total Tagihan</dt><dd><?= rph($iv['net_amount']) ?></dd>
              <dt>Dibayar</dt><dd><?= rph($iv['paid_amount']) ?></dd>
              <dt>Saldo</dt><dd class="fw-bold"><?= rph($iv['outstanding_amount']) ?></dd>
              <dt>Status Bayar</dt><dd><?= statusBadge($iv['payment_status_code']) ?></dd>
              <dt>Status Dokumen</dt><dd><?= docStatusBadge($iv['doc_status']) ?></dd>
              <dt>Revenue Dihitung</dt><dd><?= (int)$iv['is_revenue_counted'] === 1 ? badge('Ya','success') : badge('Tidak','secondary') ?></dd>
            </dl>
          <?php endif; ?>
        </div>
      </div></div>
      <?php endforeach; ?>
    </div>
    <?php if ($audit): ?>
    <div class="card mt-3"><div class="card-header"><i class="bi bi-clock-history me-1"></i>Riwayat</div>
      <div class="card-body"><div class="timeline">
        <?php foreach ($audit as $a): ?>
          <div class="timeline-item"><div class="timeline-time"><?= tglWaktu($a['created_at']) ?></div>
            <div class="small"><strong><?= e(\App\Core\Audit::actionLabel($a['action'])) ?></strong> — <?= e($a['user_name'] ?: 'Sistem') ?></div>
            <div class="small text-body-secondary"><?= e($a['description']) ?></div></div>
        <?php endforeach; ?>
      </div></div></div>
    <?php endif; ?>
  </div>
</div>

<div class="modal fade" id="modalTolak" tabindex="-1"><div class="modal-dialog"><div class="modal-content">
  <form method="post" action="<?= url('/revisi/'.(int)$rev['id'].'/tolak') ?>"><?= csrf() ?>
    <div class="modal-header"><h5 class="modal-title"><?= empty($rev['applied_at']) ? 'Tolak Revisi' : 'Batalkan Penerapan Revisi' ?></h5>
      <button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
    <div class="modal-body">
      <?php if (!empty($rev['applied_at'])): ?>
        <p class="small">Invoice lama akan diaktifkan kembali dan pendapatannya dihitung ulang.</p><?php endif; ?>
      <label class="form-label required" for="alasanTolak">Alasan</label>
      <textarea class="form-control" id="alasanTolak" name="alasan" rows="3" required maxlength="400"></textarea>
    </div>
    <div class="modal-footer"><button type="button" class="btn btn-light" data-bs-dismiss="modal">Batal</button>
      <button class="btn btn-danger" type="submit">Konfirmasi</button></div>
  </form>
</div></div></div>
