<?php // vars: $rows, $q, $status ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">مجموعات المصروفات</h4>
  <?php if (can('expenses.categories.create')): ?>
    <a class="btn btn-primary" href="<?= e(url('/finance/expenses/categories/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-6">
      <label class="form-label">بحث</label>
      <input type="text" class="form-control" name="q" placeholder="اسم المجموعة أو الكود" value="<?= e($q) ?>">
    </div>
    <div class="col-md-3">
      <label class="form-label">الحالة</label>
      <select class="form-select" name="status">
        <option value="all" <?= $status==='all'?'selected':'' ?>>الكل</option>
        <option value="active" <?= $status==='active'?'selected':'' ?>>النشطة</option>
        <option value="inactive" <?= $status==='inactive'?'selected':'' ?>>غير النشطة</option>
      </select>
    </div>
    <div class="col-md-3 d-flex gap-2">
      <button class="btn btn-outline-primary w-100" type="submit">عرض</button>
      <a class="btn btn-outline-secondary" href="<?= e(url('/finance/expenses/categories')) ?>">مسح</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 class="text-center">عدد البنود</th>
          <th class="text-center">الترتيب</th>
          <th>الحالة</th>
          <th>ملاحظات</th>
          <th style="width:1%"></th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($rows)): ?>
          <tr><td colspan="7" class="text-center text-muted py-4">لا توجد مجموعات مصروفات.</td></tr>
        <?php endif; ?>
        <?php foreach ($rows as $row): ?>
          <tr>
            <td class="fw-bold"><?= e($row['name']) ?></td>
            <td><?= e($row['code'] ?: '-') ?></td>
            <td class="text-center"><?= (int)($row['items_count'] ?? 0) ?></td>
            <td class="text-center"><?= (int)$row['sort_order'] ?></td>
            <td><?= (int)$row['is_active']===1 ? '<span class="badge text-bg-success">نشطة</span>' : '<span class="badge text-bg-secondary">غير نشطة</span>' ?></td>
            <td><?= e($row['notes'] ?: '-') ?></td>
            <td class="text-nowrap">
              <?php if (can('expenses.categories.create')): ?>
                <a class="btn btn-sm btn-outline-primary" href="<?= e(url('/finance/expenses/categories/form?id='.(int)$row['id'])) ?>">تعديل</a>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
