<div class="card">
  <div style="display:flex;justify-content:space-between;align-items:flex-start;gap:10px;flex-wrap:wrap">
    <div>
      <h3 style="margin:0">مهام المراقبين</h3>
      <div class="muted small">إدارة المهام الموكلّة للمراقبين وربطها بالواحات/المزارع.</div>
    </div>
    <?php if (auth_can('manage_inspector_tasks')): ?>
      <a class="btn primary" style="align-self:flex-start" href="<?= e(url('/index.php?page=inspector_tasks&action=create')) ?>">إضافة مهمة</a>
    <?php endif; ?>
  </div>

  <form method="get" style="display:flex;gap:10px;flex-wrap:wrap;margin-top:12px;align-items:center">
    <input type="hidden" name="page" value="inspector_tasks">

    <select class="input" name="filter_inspector">
      <option value="0">كل المراقبين</option>
      <?php foreach (($inspectors ?? []) as $i): ?>
        <?php $lbl = trim(($i['code'] ?? '') . ' ' . ($i['name'] ?? '')); ?>
        <option value="<?= e((string)$i['id']) ?>" <?= intval($filter_inspector)==intval($i['id']) ? 'selected':'' ?>><?= e($lbl) ?></option>
      <?php endforeach; ?>
    </select>

    <select class="input" name="filter_status">
      <option value="">كل الحالات</option>
      <option value="open" <?= $filter_status==='open'?'selected':'' ?>>مفتوحة</option>
      <option value="in_progress" <?= $filter_status==='in_progress'?'selected':'' ?>>قيد التنفيذ</option>
      <option value="done" <?= $filter_status==='done'?'selected':'' ?>>منجزة</option>
      <option value="cancelled" <?= $filter_status==='cancelled'?'selected':'' ?>>ملغاة</option>
    </select>

    <input class="input" type="text" name="q" value="<?= e($q) ?>" placeholder="بحث في العنوان/الوصف">
    <button class="btn" type="submit">تطبيق</button>
    <a class="btn" href="<?= e(url('/index.php?page=inspector_tasks')) ?>">إعادة</a>
  </form>
</div>

<div class="card" style="margin-top:16px">
  <div class="table-wrap">
    <table class="table">
      <thead>
        <tr>
          <th>#</th>
          <th>المراقب</th>
          <th>الوجهة</th>
          <th>العنوان</th>
          <th>الحالة</th>
          <th>تاريخ الاستحقاق</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach (($rows ?? []) as $r): ?>
          <?php
            $st = (string)$r['status'];
            $badge = '<span class="tag">'.e($st).'</span>';
            if ($st==='open') $badge = '<span class="tag warn">مفتوحة</span>';
            if ($st==='in_progress') $badge = '<span class="tag">قيد التنفيذ</span>';
            if ($st==='done') $badge = '<span class="tag ok">منجزة</span>';
            if ($st==='cancelled') $badge = '<span class="tag danger">ملغاة</span>';
          ?>
          <tr>
            <td><?= e((string)$r['id']) ?></td>
            <td><?= e(trim((string)($r['inspector_code'] ?? '') . ' ' . (string)($r['inspector_name'] ?? ''))) ?></td>
            <td><?= e(owner_label((string)$r['entity_type'], intval($r['entity_id']))) ?></td>
            <td><?= e((string)$r['task_title']) ?></td>
            <td><?= $badge ?></td>
            <td><?= e((string)($r['due_date'] ?? '')) ?></td>
            <td style="text-align:left;white-space:nowrap">
              <a class="btn" href="<?= e(url('/index.php?page=inspector_tasks&action=edit&id=' . intval($r['id']))) ?>">فتح</a>
              <?php if (auth_can('manage_inspector_tasks')): ?>
                <a class="btn danger" href="<?= e(url('/index.php?page=inspector_tasks&action=delete&id=' . intval($r['id']))) ?>">حذف</a>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($rows)): ?>
          <tr><td colspan="7" class="muted">لا توجد مهام.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
