<div class="card">
  <div class="row" style="justify-content:space-between;align-items:center">
    <h3 style="margin:0"><?= e($page_title) ?></h3>
    <a class="btn" href="<?= e(url('/index.php?page=inspector_tasks')) ?>">عودة</a>
  </div>

  <form method="post" action="<?= e(url('/index.php?page=inspector_tasks&action=' . e($action) . ($id ? '&id=' . intval($id) : ''))) ?>">
    <?= csrf_field() ?>

    <div class="grid" style="grid-template-columns:repeat(2,minmax(0,1fr));gap:12px">
      <div>
        <label class="label">المراقب *</label>
        <select class="input" name="inspector_id" required>
          <option value="0">اختر...</option>
          <?php foreach (($inspectors ?? []) as $i): ?>
            <?php $lbl = trim(($i['code'] ?? '') . ' ' . ($i['name'] ?? '')); ?>
            <option value="<?= e((string)$i['id']) ?>" <?= intval($row['inspector_id'] ?? 0)==intval($i['id']) ? 'selected':'' ?>><?= e($lbl) ?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <div>
        <label class="label">الحالة</label>
        <select class="input" name="status">
          <option value="open" <?= ($row['status'] ?? '')==='open'?'selected':'' ?>>مفتوحة</option>
          <option value="in_progress" <?= ($row['status'] ?? '')==='in_progress'?'selected':'' ?>>قيد التنفيذ</option>
          <option value="done" <?= ($row['status'] ?? '')==='done'?'selected':'' ?>>منجزة</option>
          <option value="cancelled" <?= ($row['status'] ?? '')==='cancelled'?'selected':'' ?>>ملغاة</option>
        </select>
      </div>

      <div>
        <label class="label">نوع الوجهة *</label>
        <select class="input" name="entity_type" required>
          <option value="oasis" <?= ($row['entity_type'] ?? '')==='oasis'?'selected':'' ?>>واحة</option>
          <option value="farm" <?= ($row['entity_type'] ?? '')==='farm'?'selected':'' ?>>مزرعة</option>
        </select>
      </div>

      <div>
        <label class="label">ID الوجهة *</label>
        <input class="input" name="entity_id" type="number" min="1" value="<?= e((string)($row['entity_id'] ?? '')) ?>" required>
      </div>

      <div style="grid-column:1/-1">
        <label class="label">عنوان المهمة *</label>
        <input class="input" name="task_title" value="<?= e((string)($row['task_title'] ?? '')) ?>" required>
      </div>

      <div style="grid-column:1/-1">
        <label class="label">الوصف</label>
        <textarea class="input" name="task_desc" rows="3"><?= e((string)($row['task_desc'] ?? '')) ?></textarea>
      </div>

      <div>
        <label class="label">تاريخ الاستحقاق</label>
        <input class="input date-field" name="due_date" value="<?= e((string)($row['due_date'] ?? '')) ?>" placeholder="YYYY-MM-DD">
      </div>
      <div class="muted small" style="align-self:end">يمكن أخذ ID من صفحة الواحات/المزارع.</div>
    </div>

    <div class="row" style="margin-top:12px;gap:10px">
      <?php if (!auth_can('manage_inspector_tasks')): ?>
        <div class="alert warn">ليس لديك صلاحية التعديل. يمكنك الاطلاع فقط.</div>
      <?php else: ?>
        <button class="btn primary" type="submit">حفظ</button>
      <?php endif; ?>
    </div>
  </form>
</div>
