<?php $isEdit = !empty($rev); ?>
<form method="post" action="<?= $isEdit ? url('/revisi/'.(int)$rev['id'].'/ubah') : url('/revisi/tambah') ?>" data-loading>
<?= csrf() ?>
<div class="row g-3 justify-content-center">
  <div class="col-lg-8">
    <div class="card"><div class="card-header"><i class="bi bi-arrow-repeat me-1"></i>Data Revisi Invoice (RF)</div>
      <div class="card-body row g-3">
        <div class="col-md-4"><label class="form-label required" for="rf_no">Nomor RF</label>
          <input type="text" class="form-control" id="rf_no" name="rf_no" required maxlength="40"
                 value="<?= attr($isEdit ? $rev['rf_no'] : ($nomorSaran ?? '')) ?>"></div>
        <div class="col-md-4"><label class="form-label" for="rf_date">Tanggal RF</label>
          <input type="date" class="form-control" id="rf_date" name="rf_date" value="<?= attr($isEdit ? $rev['rf_date'] : App\Core\Dates::today()) ?>"></div>
        <div class="col-md-4"><label class="form-label required" for="revision_type_id">Jenis Revisi</label>
          <select class="form-select" id="revision_type_id" name="revision_type_id" required>
            <option value="">— Pilih jenis —</option>
            <?php foreach ($types as $t): ?>
              <option value="<?= (int)$t['id'] ?>" data-action="<?= e($t['default_action']) ?>"
                <?= ($isEdit && (int)$rev['revision_type_id']===(int)$t['id'])?'selected':'' ?>><?= e($t['name']) ?></option>
            <?php endforeach; ?></select></div>

        <div class="col-md-6"><label class="form-label required" for="old_invoice_no">Nomor Invoice Lama</label>
          <input type="text" class="form-control" id="old_invoice_no" name="old_invoice_no" required maxlength="60"
                 value="<?= attr($isEdit ? $rev['old_invoice_no'] : '') ?>" placeholder="mis. FA/26/0002">
          <div class="form-text">Invoice yang akan diganti / dibatalkan / dihapuskan.</div></div>
        <div class="col-md-6"><label class="form-label" for="new_invoice_no">Nomor Invoice Baru (Pengganti)</label>
          <input type="text" class="form-control" id="new_invoice_no" name="new_invoice_no" maxlength="60"
                 value="<?= attr($isEdit ? $rev['new_invoice_no'] : '') ?>" placeholder="Kosongkan bila tidak ada pengganti">
          <div class="form-text">Kosongkan untuk pembatalan atau penghapusan tanpa invoice pengganti.</div></div>

        <div class="col-md-4"><label class="form-label" for="old_invoice_date">Tanggal Invoice Lama</label>
          <input type="date" class="form-control" id="old_invoice_date" name="old_invoice_date" value="<?= attr($isEdit ? $rev['old_invoice_date'] : '') ?>"></div>
        <div class="col-md-4"><label class="form-label" for="original_amount">Nominal Invoice Lama</label>
          <div class="input-group"><span class="input-group-text">Rp</span>
            <input type="text" class="form-control text-end" id="original_amount" name="original_amount" data-uang
                   value="<?= attr($isEdit ? nf($rev['original_amount'],0) : '') ?>"></div>
          <div class="form-text">Kosongkan untuk mengambil dari data invoice.</div></div>
        <div class="col-md-4"><label class="form-label required" for="old_invoice_action">Tindakan Invoice Lama</label>
          <select class="form-select" id="old_invoice_action" name="old_invoice_action" required>
            <?php foreach ($actions as $k=>$v): ?>
              <option value="<?= $k ?>" <?= ($isEdit && $rev['old_invoice_action']===$k)?'selected':'' ?>><?= e($v) ?></option>
            <?php endforeach; ?></select></div>

        <div class="col-12"><label class="form-label" for="reason">Alasan / Keterangan Revisi</label>
          <textarea class="form-control" id="reason" name="reason" rows="3" maxlength="2000"><?= e($isEdit ? $rev['reason'] : '') ?></textarea></div>

        <div class="col-12"><div class="alert alert-info mb-0 small">
          <i class="bi bi-shield-check me-1"></i><strong>Pencegahan dobel hitung.</strong>
          Setelah revisi disetujui, invoice lama otomatis dikeluarkan dari piutang aktif dan pendapatannya
          tidak dihitung, sementara invoice pengganti tetap dihitung penuh. Riwayat invoice lama tidak pernah dihapus.
        </div></div>
      </div>
      <div class="card-footer d-flex gap-2">
        <button class="btn btn-primary" type="submit"><i class="bi bi-check-lg me-1"></i>Simpan Revisi</button>
        <a class="btn btn-light" href="<?= $isEdit ? url('/revisi/'.(int)$rev['id']) : url('/revisi') ?>">Batal</a>
      </div>
    </div>
  </div>
</div>
</form>
<?php viewStart('scripts'); ?>
<script>
document.getElementById('revision_type_id').addEventListener('change', function () {
  const a = this.options[this.selectedIndex].dataset.action;
  if (a) document.getElementById('old_invoice_action').value = a;
});
</script>
<?php viewStop(); ?>
