<?php $check=$check??[]; $form=$form??[]; $errors=$errors??[]; ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <div><h4 class="mb-1">تظهير شيك لمورد</h4><div class="text-muted small">سيتم تخفيض استحقاق المورد بقيمة الشيك</div></div>
  <a class="btn btn-outline-secondary" href="<?= e((url($check_view_base_url ?? '/finance/checks/view')) . '?id='.(int)$check['id']) ?>">رجوع للشيك</a>
</div>
<?php if ($errors): ?><div class="alert alert-danger"><ul class="mb-0"><?php foreach($errors as $er): ?><li><?= e($er) ?></li><?php endforeach; ?></ul></div><?php endif; ?>
<div class="card mb-3"><div class="card-body row g-3">
  <div class="col-md-3"><div class="text-muted small">رقم الشيك</div><div class="fw-bold"><?= e((string)$check['check_no']) ?></div></div>
  <div class="col-md-3"><div class="text-muted small">العميل الأصلي</div><div class="fw-bold"><?= e((string)$check['drawer_name']) ?></div></div>
  <div class="col-md-3"><div class="text-muted small">القيمة</div><div class="fw-bold"><?= e(format_money((float)$check['amount'])) ?></div></div>
  <div class="col-md-3"><div class="text-muted small">الاستحقاق</div><div class="fw-bold"><?= e((string)($check['due_date'] ?: '-')) ?></div></div>
</div></div>
<div class="card"><div class="card-body">
<form method="post" class="row g-3">
  <?= csrf_field() ?>
  <div class="col-md-6 position-relative">
    <label class="form-label">المورد</label>
    <input type="hidden" name="supplier_party_id" id="supplier_party_id" value="<?= e((string)($form['supplier_party_id']??'')) ?>">
    <input type="text" class="form-control" name="supplier_name" id="supplier_name" value="<?= e((string)($form['supplier_name']??'')) ?>" placeholder="ابحث باسم المورد">
    <div id="supplierSuggestions" class="list-group position-absolute w-100 d-none" style="z-index:1200;max-height:220px;overflow:auto;"></div>
  </div>
  <div class="col-md-6"><label class="form-label">تاريخ التظهير</label><input type="datetime-local" class="form-control" name="movement_date" value="<?= e((string)($form['movement_date']??'')) ?>"></div>
  <div class="col-12"><label class="form-label">ملاحظات</label><input class="form-control" name="notes" value="<?= e((string)($form['notes']??'')) ?>"></div>
  <div class="col-12 d-flex justify-content-end"><button class="btn btn-primary">تأكيد التظهير</button></div>
</form></div></div>
<script>
(function(){
  const q = document.getElementById('supplier_name');
  const id = document.getElementById('supplier_party_id');
  const list = document.getElementById('supplierSuggestions');
  let timer = null;
  function clearList(){ list.innerHTML=''; list.classList.add('d-none'); }
  q?.addEventListener('input', function(){
    id.value='';
    const v = this.value.trim();
    clearTimeout(timer);
    if (v.length < 1) { clearList(); return; }
    timer = setTimeout(async ()=>{
      const res = await fetch(<?= json_encode(url('/ajax/parties/search?type=supplier')) ?> + '&q=' + encodeURIComponent(v));
      const rows = await res.json();
      list.innerHTML='';
      if (!rows.length) { clearList(); return; }
      rows.forEach(r=>{
        const btn = document.createElement('button');
        btn.type='button'; btn.className='list-group-item list-group-item-action';
        btn.textContent = `${r.name}${r.code ? ' - ' + r.code : ''}`;
        btn.onclick = ()=>{ q.value = r.name; id.value = r.id; clearList(); };
        list.appendChild(btn);
      });
      list.classList.remove('d-none');
    }, 180);
  });
  document.addEventListener('click', (e)=>{ if (!list.contains(e.target) && e.target !== q) clearList(); });
})();
</script>
