<?php
// vars: $cashboxes, $balances, $q
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">الخزن</h4>
  <?php if (can('cashboxes.create')): ?>
    <a class="btn btn-primary" href="<?= e(url($cashbox_form_url ?? '/finance/cashboxes/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($cashboxes_url ?? '/finance/cashboxes')) ?>">مسح</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 class="text-end">رصيد افتتاحي</th>
          <th class="text-end">الرصيد الحالي</th>
          <th>الحالة</th>
          <th style="width:1%"></th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($cashboxes)): ?>
          <tr><td colspan="5" class="text-center text-muted py-4">لا توجد بيانات</td></tr>
        <?php endif; ?>
        <?php foreach ($cashboxes as $cb): ?>
          <tr>
            <td class="fw-bold"><?= e($cb['name']) ?></td>
            <td class="text-end"><?= e(nf($cb['opening_balance'])) ?></td>
            <td class="text-end"><?= e(nf($balances[(int)$cb['id']] ?? 0)) ?></td>
            <td>
              <?php if ((int)$cb['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('cashboxes.report.view')): ?>
                <a class="btn btn-sm btn-outline-secondary" href="<?= e((url($cashbox_ledger_url ?? '/finance/cashboxes/ledger')) . '?cashbox_id='.(int)$cb['id']) ?>">كشف الحركة</a>
              <?php endif; ?>
              <?php if (can('cashboxes.create')): ?>
                <a class="btn btn-sm btn-outline-primary" href="<?= e((url($cashbox_form_url ?? '/finance/cashboxes/form')) . '?id='.(int)$cb['id']) ?>">تعديل</a>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
