<?php ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">حركة بنك</h4>
  <a class="btn btn-outline-secondary" href="<?= e(url($bank_transactions_url ?? '/finance/bank-transactions')) ?>">رجوع</a>
</div>

<?php if (!empty($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 class="card p-3" method="post">
  <?= csrf_field() ?>
  <div class="row g-3">
    <div class="col-md-4">
      <label class="form-label">الحساب البنكي</label>
      <select class="form-select" name="bank_account_id" required>
        <option value="">اختر الحساب</option>
        <?php foreach ($bankAccounts as $a): ?>
          <option value="<?= (int)$a['id'] ?>" <?= ((int)$form['bank_account_id'] === (int)$a['id'] ? 'selected' : '') ?>><?= e($a['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-4">
      <label class="form-label">نوع الحركة</label>
      <select class="form-select" name="action_type" id="bank-action-type" required>
        <option value="deposit" <?= ($form['action_type']==='deposit'?'selected':'') ?>>إيداع</option>
        <option value="withdraw" <?= ($form['action_type']==='withdraw'?'selected':'') ?>>سحب</option>
        <option value="cash_to_bank" <?= ($form['action_type']==='cash_to_bank'?'selected':'') ?>>تحويل من خزنة إلى بنك</option>
        <option value="bank_to_cash" <?= ($form['action_type']==='bank_to_cash'?'selected':'') ?>>تحويل من بنك إلى خزنة</option>
      </select>
    </div>
    <div class="col-md-4" id="cashbox-wrap" style="display:none">
      <label class="form-label">الخزنة</label>
      <select class="form-select" name="cashbox_id">
        <option value="">اختر الخزنة</option>
        <?php foreach ($cashboxes as $c): ?>
          <option value="<?= (int)$c['id'] ?>" <?= ((int)$form['cashbox_id'] === (int)$c['id'] ? 'selected' : '') ?>><?= e($c['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-3">
      <label class="form-label">التاريخ</label>
      <input type="date" class="form-control" name="trx_date" value="<?= e($form['trx_date']) ?>" required>
    </div>
    <div class="col-md-3">
      <label class="form-label">الوقت</label>
      <input type="time" class="form-control" name="trx_time" value="<?= e($form['trx_time']) ?>">
    </div>
    <div class="col-md-3">
      <label class="form-label">المبلغ</label>
      <input type="number" step="0.01" min="0.01" class="form-control" name="amount" value="<?= e($form['amount']) ?>" required>
    </div>
    <div class="col-md-3">
      <label class="form-label">مرجع</label>
      <input type="text" class="form-control" name="ref_no" value="<?= e($form['ref_no']) ?>">
    </div>
    <div class="col-md-12">
      <label class="form-label">ملاحظات</label>
      <input type="text" class="form-control" name="notes" value="<?= e($form['notes']) ?>">
    </div>
  </div>
  <div class="mt-3 d-flex gap-2">
    <button class="btn btn-primary" type="submit"><i class="fa-solid fa-floppy-disk"></i> حفظ</button>
    <a class="btn btn-outline-secondary" href="<?= e(url($bank_transactions_url ?? '/finance/bank-transactions')) ?>">إلغاء</a>
  </div>
</form>

<script>
(function(){
  const typeSel = document.getElementById('bank-action-type');
  const cashboxWrap = document.getElementById('cashbox-wrap');
  function sync(){
    const show = typeSel && ['cash_to_bank','bank_to_cash'].includes(typeSel.value);
    if (cashboxWrap) cashboxWrap.style.display = show ? '' : 'none';
  }
  if (typeSel) {
    typeSel.addEventListener('change', sync);
    sync();
  }
})();
</script>
