<form method="post" action="<?= url('/nota-kredit/tambah') ?>" data-loading>
<?= csrf() ?>
<div class="row g-3 justify-content-center">
  <div class="col-lg-7">
    <div class="card"><div class="card-header"><i class="bi bi-file-earmark-minus me-1"></i>Nota Kredit / Penyesuaian Saldo</div>
      <div class="card-body row g-3">
        <div class="col-md-6"><label class="form-label" for="cn_no">Nomor</label>
          <input type="text" class="form-control" id="cn_no" value="<?= attr($nomorSaran) ?>" readonly></div>
        <div class="col-md-6"><label class="form-label required" for="cn_date">Tanggal</label>
          <input type="date" class="form-control" id="cn_date" name="cn_date" required value="<?= attr(App\Core\Dates::today()) ?>"></div>
        <div class="col-12">
          <?php if ($inv): ?>
            <div class="alert alert-info py-2 mb-0"><strong>Invoice <?= e($inv['invoice_no']) ?></strong> — <?= e($inv['customer_name']) ?><br>
              <span class="small">Total tagihan <?= e(rp($inv['net_amount'])) ?> · Saldo <strong><?= e(rp($inv['outstanding_amount'])) ?></strong></span></div>
            <input type="hidden" name="invoice_id" value="<?= (int)$inv['id'] ?>">
          <?php else: ?>
            <label class="form-label required" for="cariInv">Pilih Invoice</label>
            <input type="text" class="form-control" id="cariInv" placeholder="Ketik nomor invoice atau nama customer…" autocomplete="off">
            <div id="invTerpilih" class="mt-2"></div>
          <?php endif; ?>
        </div>
        <div class="col-md-6"><label class="form-label required" for="cn_type">Jenis</label>
          <select class="form-select" id="cn_type" name="cn_type" required>
            <?php foreach ($types as $k=>$v): ?><option value="<?= $k ?>"><?= e($v) ?></option><?php endforeach; ?></select>
          <div class="form-text">"Hapus Buku" akan menandai invoice sebagai piutang tak tertagih.</div></div>
        <div class="col-md-6"><label class="form-label required" for="amount">Nilai</label>
          <div class="input-group"><span class="input-group-text">Rp</span>
            <input type="text" class="form-control text-end" id="amount" name="amount" data-uang required></div></div>
        <div class="col-12"><label class="form-label required" for="reason">Alasan</label>
          <textarea class="form-control" id="reason" name="reason" rows="3" required minlength="5" maxlength="500"></textarea></div>
        <div class="col-12"><div class="alert alert-warning mb-0 small">
          <i class="bi bi-shield-exclamation me-1"></i>Nota kredit memerlukan persetujuan sebelum mengurangi saldo piutang.
          Saldo invoice tidak berubah sampai permintaan disetujui.</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 &amp; Ajukan Persetujuan</button>
        <a class="btn btn-light" href="<?= url('/nota-kredit') ?>">Batal</a>
      </div>
    </div>
  </div>
</div>
</form>
<?php if (!$inv): viewStart('scripts'); ?>
<script>
SP.invoiceSearch(document.getElementById('cariInv'), function (row) {
  document.getElementById('invTerpilih').innerHTML =
    '<div class="alert alert-info py-2 mb-0"><strong>' + row.invoice_no + '</strong> — ' + (row.customer_name || '') +
    '<br><span class="small">Saldo ' + SP.rp(row.outstanding_amount) + '</span>' +
    '<input type="hidden" name="invoice_id" value="' + row.id + '"></div>';
  document.getElementById('amount').value = Number(row.outstanding_amount).toLocaleString('id-ID');
}, true);
</script>
<?php viewStop(); endif; ?>
