<?php $isEdit = !empty($fu); ?>
<form method="post" action="<?= $isEdit ? url('/penagihan/'.(int)$fu['id'].'/ubah') : url('/penagihan/tambah') ?>" data-loading>
<?= csrf() ?>
<div class="row g-3">
  <div class="col-lg-7">
    <div class="card mb-3"><div class="card-header"><i class="bi bi-telephone-outbound me-1"></i>Aktivitas Penagihan</div>
      <div class="card-body row g-3">
        <?php if ($inv): ?>
          <div class="col-12"><div class="alert alert-info mb-0 py-2">
            <strong>Invoice <?= e($inv['invoice_no']) ?></strong> — <?= e($inv['customer_name'] ?? '') ?><br>
            <span class="small">Jatuh tempo <?= tgl($inv['due_date']) ?> · Saldo <?= e(rp($inv['outstanding_amount'])) ?></span>
          </div>
          <input type="hidden" name="invoice_id" value="<?= (int)$inv['id'] ?>">
          <input type="hidden" name="customer_id" value="<?= (int)$inv['customer_id'] ?>"></div>
        <?php elseif (!$isEdit): ?>
          <div class="col-12"><label class="form-label" for="cariInv">Cari 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></div>
        <?php else: ?>
          <div class="col-12"><div class="alert alert-secondary mb-0 py-2 small">
            Invoice: <strong><?= e($fu['invoice_no'] ?: '-') ?></strong> · Customer: <?= e($fu['customer_name'] ?: '-') ?></div>
          <input type="hidden" name="invoice_id" value="<?= (int)$fu['invoice_id'] ?>"></div>
        <?php endif; ?>

        <div class="col-md-4"><label class="form-label required" for="followup_date">Tanggal Follow-up</label>
          <input type="date" class="form-control" id="followup_date" name="followup_date" required
                 value="<?= attr($isEdit ? $fu['followup_date'] : App\Core\Dates::today()) ?>"></div>
        <div class="col-md-4"><label class="form-label" for="method">Metode</label>
          <select class="form-select" id="method" name="method">
            <?php foreach ($methods as $k=>$v): ?><option value="<?= $k ?>" <?= ($isEdit && $fu['method']===$k)?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-4"><label class="form-label" for="collection_officer_id">Petugas Penagihan</label>
          <select class="form-select" id="collection_officer_id" name="collection_officer_id"><option value="">—</option>
            <?php foreach ($officers as $o): ?><option value="<?= (int)$o['id'] ?>" <?= ($isEdit && (int)$fu['collection_officer_id']===(int)$o['id'])?'selected':'' ?>><?= e($o['name']) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-6"><label class="form-label" for="contact_person">Narahubung Customer</label>
          <input type="text" class="form-control" id="contact_person" name="contact_person" maxlength="120"
                 value="<?= attr($isEdit ? $fu['contact_person'] : ($inv['contact_person'] ?? '')) ?>"></div>
        <div class="col-md-6"><label class="form-label" for="contact_phone">Nomor Telepon</label>
          <input type="text" class="form-control" id="contact_phone" name="contact_phone" maxlength="60"
                 value="<?= attr($isEdit ? $fu['contact_phone'] : ($inv['customer_phone'] ?? '')) ?>"></div>
        <div class="col-md-6"><label class="form-label" for="collection_status_id">Status Penagihan</label>
          <select class="form-select" id="collection_status_id" name="collection_status_id"><option value="">—</option>
            <?php foreach ($statuses as $s): ?><option value="<?= (int)$s['id'] ?>" <?= ($isEdit && (int)$fu['collection_status_id']===(int)$s['id'])?'selected':'' ?>><?= e($s['name']) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-3"><label class="form-label" for="priority">Prioritas</label>
          <select class="form-select" id="priority" name="priority">
            <?php foreach ($priorities as $k=>$v): ?><option value="<?= $k ?>" <?= ($isEdit && $fu['priority']===$k)?'selected':'' ?>><?= e($v) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-3"><label class="form-label" for="next_followup_date">Follow-up Berikutnya</label>
          <input type="date" class="form-control" id="next_followup_date" name="next_followup_date" value="<?= attr($isEdit ? $fu['next_followup_date'] : '') ?>"></div>
        <div class="col-12"><label class="form-label" for="result">Hasil Follow-up</label>
          <textarea class="form-control" id="result" name="result" rows="3" maxlength="2000" placeholder="Ringkasan pembicaraan / tanggapan customer…"><?= e($isEdit ? $fu['result'] : '') ?></textarea></div>
        <div class="col-12"><label class="form-label" for="notes">Catatan Tambahan</label>
          <textarea class="form-control" id="notes" name="notes" rows="2" maxlength="2000"><?= e($isEdit ? $fu['notes'] : '') ?></textarea></div>
      </div>
    </div>
  </div>
  <div class="col-lg-5">
    <?php if (!$isEdit): ?>
    <div class="card mb-3"><div class="card-header"><i class="bi bi-hand-thumbs-up me-1"></i>Janji Bayar (Promise to Pay)</div>
      <div class="card-body row g-3">
        <div class="col-12"><p class="small text-body-secondary mb-0">Isi bila customer memberi komitmen pembayaran.
          Sistem akan mengevaluasi realisasinya secara otomatis setelah tanggal janji terlewati.</p></div>
        <div class="col-6"><label class="form-label" for="promise_date">Tanggal Janji Bayar</label>
          <input type="date" class="form-control" id="promise_date" name="promise_date"></div>
        <div class="col-6"><label class="form-label" for="promise_amount">Nilai Dijanjikan</label>
          <div class="input-group"><span class="input-group-text">Rp</span>
            <input type="text" class="form-control text-end" id="promise_amount" name="promise_amount" data-uang></div></div>
        <div class="col-12"><label class="form-label" for="promise_notes">Catatan Janji Bayar</label>
          <input type="text" class="form-control" id="promise_notes" name="promise_notes" maxlength="500"></div>
      </div>
    </div>
    <?php endif; ?>
    <div class="card"><div class="card-body d-flex gap-2">
      <button class="btn btn-primary flex-fill" type="submit"><i class="bi bi-check-lg me-1"></i>Simpan Aktivitas</button>
      <a class="btn btn-light" href="<?= url('/penagihan') ?>">Batal</a>
    </div></div>
  </div>
</div>
</form>
<?php if (!$isEdit && !$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) + ' · Jatuh tempo ' + (row.due_date_fmt || '-') + '</span>' +
    '<input type="hidden" name="invoice_id" value="' + row.id + '">' +
    '<input type="hidden" name="customer_id" value="' + (row.customer_id || '') + '"></div>';
}, true);
</script>
<?php viewStop(); endif; ?>
