<?php // vars: $rows, $categories, $q, $category_id, $status ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">بنود المصروفات</h4>
  <?php if (can('expenses.items.create')): ?>
    <a class="btn btn-primary" href="<?= e(url('/finance/expenses/items/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-5">
      <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="category_id">
        <option value="0">كل المجموعات</option>
        <?php foreach ($categories as $cat): ?>
          <option value="<?= (int)$cat['id'] ?>" <?= ((int)$category_id === (int)$cat['id'] ? 'selected' : '') ?>><?= e($cat['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-2">
      <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-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('/finance/expenses/items')) ?>">مسح</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-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['category_name']) ?></td>
            <td><?= e($row['code'] ?: '-') ?></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.items.create')): ?>
                <a class="btn btn-sm btn-outline-primary" href="<?= e(url('/finance/expenses/items/form?id='.(int)$row['id'])) ?>">تعديل</a>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
