<?php $form=$form??[]; $errors=$errors??[]; ?>
<div class="page-actions-bar">
  <a class="btn btn-outline-secondary btn-back" data-shortcut="back" href="<?= e(url($checks_url ?? '/finance/checks')) ?>">رجوع</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; ?>
<form method="post" class="form-shell">
  <div class="card content-card"><div class="card-body"><div class="row g-3">
  <?= csrf_field() ?>
  <div class="col-md-4 position-relative">
    <label class="form-label">العميل</label>
    <input type="hidden" name="drawer_party_id" id="drawer_party_id" value="<?= e((string)($form['drawer_party_id']??'')) ?>">
    <input type="text" class="form-control" name="drawer_name" id="drawer_name" value="<?= e((string)($form['drawer_name']??'')) ?>" placeholder="ابحث عن العميل">
    <div id="drawerSuggestions" class="list-group position-absolute w-100 d-none" style="z-index:1200;max-height:220px;overflow:auto;"></div>
  </div>
  <div class="col-md-4"><label class="form-label">رقم الشيك</label><input class="form-control" name="check_no" value="<?= e((string)($form['check_no']??'')) ?>"></div>
  <div class="col-md-4"><label class="form-label">قيمة الشيك</label><input type="number" step="0.01" min="0" class="form-control" name="amount" value="<?= e((string)($form['amount']??'')) ?>"></div>
  <div class="col-md-4"><label class="form-label">تاريخ الاستلام</label><input type="datetime-local" class="form-control" name="receive_date" value="<?= e((string)($form['receive_date']??'')) ?>"></div>
  <div class="col-md-4"><label class="form-label">تاريخ الإصدار</label><input type="date" class="form-control" name="issue_date" value="<?= e((string)($form['issue_date']??'')) ?>"></div>
  <div class="col-md-4"><label class="form-label">تاريخ الاستحقاق</label><input type="date" class="form-control" name="due_date" value="<?= e((string)($form['due_date']??'')) ?>"></div>
  <div class="col-md-6"><label class="form-label">البنك المسحوب عليه</label><input class="form-control" name="bank_name" value="<?= e((string)($form['bank_name']??'')) ?>"></div>
  <div class="col-md-6"><label class="form-label">ملاحظات</label><input class="form-control" name="notes" value="<?= e((string)($form['notes']??'')) ?>"></div>
  </div></div></div>
  <div class="card content-card sticky-actions"><div class="card-body d-flex justify-content-end gap-2 flex-wrap"><button class="btn btn-primary btn-save" data-shortcut="save">حفظ الشيك <span class="shortcut-hint">F7</span></button><a class="btn btn-outline-secondary btn-back" data-shortcut="back" href="<?= e(url($checks_url ?? '/finance/checks')) ?>">إلغاء</a></div></div>
</form>
<script>
(function(){
  const q = document.getElementById('drawer_name');
  const id = document.getElementById('drawer_party_id');
  const list = document.getElementById('drawerSuggestions');
  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=customer')) ?> + '&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>
