<?php // vars: $accounts, $balances, $q ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">الحسابات البنكية</h4>
  <?php if (can('banks.accounts.create')): ?>
    <a class="btn btn-primary" href="<?= e(url($bank_account_form_url ?? '/finance/bank-accounts/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-8">
      <label class="form-label">بحث</label>
      <input type="text" class="form-control" name="q" placeholder="اسم الحساب / البنك / رقم الحساب" value="<?= e($q) ?>">
    </div>
    <div class="col-md-4 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_accounts_url ?? '/finance/bank-accounts')) ?>">مسح</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>العملة</th>
          <th class="text-end">رصيد افتتاحي</th>
          <th class="text-end">الرصيد الحالي</th>
          <th>الحالة</th>
          <th style="width:1%"></th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($accounts)): ?>
          <tr><td colspan="8" class="text-center text-muted py-4">لا توجد حسابات بنكية</td></tr>
        <?php endif; ?>
        <?php foreach ($accounts as $acc): ?>
          <tr>
            <td>
              <div class="fw-bold"><?= e($acc['name']) ?></div>
              <?php if (!empty($acc['iban'])): ?><small class="text-muted">IBAN: <?= e($acc['iban']) ?></small><?php endif; ?>
            </td>
            <td><?= e($acc['bank_name'] ?: '-') ?></td>
            <td><?= e($acc['account_no'] ?: '-') ?></td>
            <td><?= e($acc['currency_code'] ?: 'EGP') ?></td>
            <td class="text-end"><?= e(nf($acc['opening_balance'])) ?></td>
            <td class="text-end"><?= e(nf($balances[(int)$acc['id']] ?? 0)) ?></td>
            <td>
              <?php if ((int)$acc['is_active'] === 1): ?>
                <span class="badge text-bg-success">نشط</span>
              <?php else: ?>
                <span class="badge text-bg-secondary">غير نشط</span>
              <?php endif; ?>
            </td>
            <td class="text-nowrap">
              <?php if (can('banks.accounts.create')): ?>
                <a class="btn btn-sm btn-outline-primary" href="<?= e((url($bank_account_form_url ?? '/finance/bank-accounts/form')) . '?id='.(int)$acc['id']) ?>">تعديل</a>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
