<?php
$typeLabels = [
  'deposit' => 'إيداع',
  'withdraw' => 'سحب',
  'cash_to_bank' => 'تحويل من خزنة إلى بنك',
  'bank_to_cash' => 'تحويل من بنك إلى خزنة',
];
?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">حركات البنوك</h4>
  <?php if (can('banks.txns.create')): ?>
    <a class="btn btn-primary" href="<?= e(url($bank_transaction_form_url ?? '/finance/bank-transactions/form')) ?>"><i class="fa-solid fa-plus"></i> إضافة حركة بنك</a>
  <?php endif; ?>
</div>

<form class="card p-3 mb-3" method="get">
  <div class="row g-2 align-items-end">
    <div class="col-md-4">
      <label class="form-label">الحساب البنكي</label>
      <select class="form-select" name="bank_account_id">
        <option value="0">كل الحسابات</option>
        <?php foreach ($bankAccounts as $a): ?>
          <option value="<?= (int)$a['id'] ?>" <?= ((int)$bank_id === (int)$a['id'] ? 'selected' : '') ?>><?= e($a['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-3">
      <label class="form-label">نوع الحركة</label>
      <select class="form-select" name="action_type">
        <option value="">الكل</option>
        <?php foreach ($typeLabels as $key => $label): ?>
          <option value="<?= e($key) ?>" <?= ($action_type === $key ? 'selected' : '') ?>><?= e($label) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-3">
      <label class="form-label">بحث</label>
      <input type="text" class="form-control" name="q" value="<?= e($q) ?>" placeholder="ملاحظات / مرجع / حساب">
    </div>
    <div class="col-md-2 d-flex gap-2">
      <button class="btn btn-outline-primary w-100" type="submit">عرض</button>
      <a class="btn btn-outline-secondary" href="<?= e(url($bank_transactions_url ?? '/finance/bank-transactions')) ?>">مسح</a>
    </div>
  </div>
</form>

<div class="card p-3">
  <div class="table-responsive">
    <table class="table table-sm table-bordered align-middle mb-0">
      <thead class="table-light">
        <tr>
          <th>التاريخ</th>
          <th>الحساب البنكي</th>
          <th>نوع الحركة</th>
          <th class="text-end">داخل</th>
          <th class="text-end">خارج</th>
          <th>الخزنة</th>
          <th>المرجع</th>
          <th>ملاحظات</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($rows)): ?>
          <tr><td colspan="8" class="text-center text-muted py-4">لا توجد حركات بنكية</td></tr>
        <?php endif; ?>
        <?php foreach ($rows as $r): ?>
          <tr>
            <td><?= e(date('Y-m-d H:i', strtotime((string)$r['trx_date']))) ?></td>
            <td><?= e($r['bank_name']) ?></td>
            <td><?= e($typeLabels[$r['source_type']] ?? ($r['trx_type']==='in' ? 'داخل' : 'خارج')) ?></td>
            <td class="text-end"><?= $r['trx_type']==='in' ? e(nf($r['amount'])) : '-' ?></td>
            <td class="text-end"><?= $r['trx_type']==='out' ? e(nf($r['amount'])) : '-' ?></td>
            <td><?= e($r['cashbox_name'] ?: '-') ?></td>
            <td><?= e($r['ref_no'] ?: '-') ?></td>
            <td><?= e($r['notes'] ?: '-') ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
