{
  "$schema": "https://schemas.rasd.dev/form/v1.json",
  "rasd": "1.0",
  "id": "facility-monitoring-visit",
  "version": "2026.08.1",
  "meta": {
    "title": { "en": "Facility Monitoring Visit – Schools & Health Facilities", "ar": "زيارة رصد المرافق – المدارس والمرافق الصحية" },
    "description": {
      "en": "Third-party monitoring checklist for a school, health centre, hospital or WASH facility: facility lookup from the offline facility register, infrastructure and staffing checklists with cross-field constraints, an issues log with photos and severity, an optional perimeter trace, and an automatic follow-up flag when a critical issue is recorded.",
      "ar": "قائمة تحقق لرصد الطرف الثالث لمدرسة أو مركز صحي أو مستشفى أو مرفق مياه وصرف صحي: البحث عن المرفق في سجل المرافق غير المتصل، قوائم تحقق للبنية التحتية والكوادر مع قيود بين الحقول، سجل للمشكلات مع صور ودرجة الخطورة، تتبّع اختياري لمحيط المرفق، وعلامة متابعة تلقائية عند تسجيل مشكلة حرجة."
    },
    "tags": ["tpm", "facility", "education", "health", "wash"],
    "author": "TPM Consortium – Field Quality Team",
    "createdAt": "2026-05-20T06:30:00Z",
    "updatedAt": "2026-08-14T15:12:00Z",
    "changelog": { "en": "2026.08.1: facility register lookup replaces free-text facility name; issues repeat gains photo requirement for high/critical severity." },
    "ext": { "org.example.tpm": { "contract": "TPM-2026-04", "donor": "ECHO" } }
  },
  "settings": {
    "defaultLocale": "en",
    "locales": ["en", "ar"],
    "navigation": "paged",
    "showProgress": true,
    "allowDrafts": true,
    "autosaveMs": 1500,
    "instanceName": "concat(coalesce(${facility_name}, 'Unknown facility'), ' – ', coalesce(${visit_date}, ''))",
    "submissionIdPrefix": "FMV",
    "numbering": "latn",
    "localeMeta": { "ar": { "dir": "rtl", "numbering": "native" } },
    "audit": {
      "enabled": true,
      "trackChanges": true,
      "identifyUser": true,
      "location": { "enabled": true, "priority": "high-accuracy", "minSeconds": 30, "minMeters": 20 }
    },
    "encryption": { "mode": "none" },
    "theme": { "themeId": "rasd-field" }
  },
  "choiceLists": {
    "yes_no": {
      "choices": [
        { "value": "yes", "label": { "en": "Yes", "ar": "نعم" } },
        { "value": "no", "label": { "en": "No", "ar": "لا" } }
      ]
    },
    "yes_partial_no": {
      "choices": [
        { "value": "yes", "label": { "en": "Yes, fully", "ar": "نعم، بالكامل" } },
        { "value": "partial", "label": { "en": "Partially", "ar": "جزئياً" } },
        { "value": "no", "label": { "en": "No", "ar": "لا" } }
      ]
    },
    "facility_type": {
      "choices": [
        { "value": "school", "label": { "en": "School", "ar": "مدرسة" }, "attrs": { "sector": "education" }, "media": { "image": "assets/icons/school.svg" } },
        { "value": "health_centre", "label": { "en": "Primary health centre", "ar": "مركز رعاية صحية أولية" }, "attrs": { "sector": "health" } },
        { "value": "hospital", "label": { "en": "Hospital", "ar": "مستشفى" }, "attrs": { "sector": "health" } },
        { "value": "wash", "label": { "en": "WASH facility (water point / sanitation block)", "ar": "مرفق مياه وصرف صحي" }, "attrs": { "sector": "wash" } }
      ]
    },
    "condition": {
      "choices": [
        { "value": "good", "label": { "en": "Good", "ar": "جيدة" } },
        { "value": "fair", "label": { "en": "Fair – minor repairs needed", "ar": "مقبولة – تحتاج إصلاحات بسيطة" } },
        { "value": "poor", "label": { "en": "Poor – major repairs needed", "ar": "سيئة – تحتاج إصلاحات كبيرة" } },
        { "value": "unsafe", "label": { "en": "Unsafe / not usable", "ar": "غير آمنة / غير صالحة للاستخدام" } }
      ]
    },
    "services": {
      "choices": [
        { "value": "classes_running", "label": { "en": "Classes running as scheduled", "ar": "الحصص الدراسية تُعقد حسب الجدول" }, "attrs": { "sector": "education" } },
        { "value": "school_feeding", "label": { "en": "School feeding provided", "ar": "تُقدَّم التغذية المدرسية" }, "attrs": { "sector": "education" } },
        { "value": "psychosocial", "label": { "en": "Psychosocial support available", "ar": "الدعم النفسي الاجتماعي متاح" }, "attrs": { "sector": "education" } },
        { "value": "outpatient", "label": { "en": "Outpatient consultations", "ar": "عيادات خارجية" }, "attrs": { "sector": "health" } },
        { "value": "vaccination", "label": { "en": "Routine vaccination", "ar": "التطعيم الروتيني" }, "attrs": { "sector": "health" } },
        { "value": "maternal", "label": { "en": "Antenatal / maternal care", "ar": "رعاية الحوامل والأمهات" }, "attrs": { "sector": "health" } },
        { "value": "essential_drugs", "label": { "en": "Essential drugs in stock", "ar": "الأدوية الأساسية متوفرة" }, "attrs": { "sector": "health" } },
        { "value": "water_supply", "label": { "en": "Water supply functional", "ar": "إمدادات المياه تعمل" }, "attrs": { "sector": "wash" } },
        { "value": "latrines_open", "label": { "en": "Latrines open and cleaned", "ar": "المراحيض مفتوحة ونظيفة" }, "attrs": { "sector": "wash" } },
        { "value": "handwashing", "label": { "en": "Handwashing stations with soap", "ar": "محطات غسل اليدين مع صابون" }, "attrs": { "sector": "wash" } }
      ]
    },
    "issue_category": {
      "choices": [
        { "value": "structural", "label": { "en": "Structural damage", "ar": "أضرار إنشائية" } },
        { "value": "wash", "label": { "en": "Water / sanitation", "ar": "المياه / الصرف الصحي" } },
        { "value": "supplies", "label": { "en": "Missing supplies or equipment", "ar": "نقص في المستلزمات أو المعدات" } },
        { "value": "staffing", "label": { "en": "Staffing gap", "ar": "نقص في الكوادر" } },
        { "value": "safety", "label": { "en": "Safety / protection concern", "ar": "مخاوف تتعلق بالسلامة / الحماية" } },
        { "value": "access", "label": { "en": "Access barrier (physical or financial)", "ar": "عائق في الوصول (مادي أو مالي)" } }
      ]
    },
    "severity": {
      "choices": [
        { "value": "low", "label": { "en": "Low", "ar": "منخفضة" }, "attrs": { "rank": 1 } },
        { "value": "medium", "label": { "en": "Medium", "ar": "متوسطة" }, "attrs": { "rank": 2 } },
        { "value": "high", "label": { "en": "High", "ar": "عالية" }, "attrs": { "rank": 3 } },
        { "value": "critical", "label": { "en": "Critical – immediate action", "ar": "حرجة – تتطلب إجراءً فورياً" }, "attrs": { "rank": 4 } }
      ]
    },
    "governorate": {
      "source": { "type": "dataset", "dataset": "geo_gov" },
      "valueKey": "code",
      "labelKey": "name"
    },
    "district": {
      "source": { "type": "dataset", "dataset": "geo_dist" },
      "valueKey": "code",
      "labelKey": "name",
      "filterKeys": ["gov_code"]
    },
    "facility": {
      "source": { "type": "dataset", "dataset": "facilities" },
      "valueKey": "facility_id",
      "labelKey": "name",
      "filterKeys": ["dist_code", "type"],
      "ext": { "org.example.tpm": { "register": "national-facility-register", "refresh": "weekly" } }
    }
  },
  "datasets": [
    {
      "name": "geo_gov",
      "source": "server",
      "keyField": "code",
      "columns": [ { "name": "code" }, { "name": "name" } ]
    },
    {
      "name": "geo_dist",
      "source": "server",
      "keyField": "code",
      "columns": [ { "name": "code" }, { "name": "name" }, { "name": "gov_code" } ]
    },
    {
      "name": "facilities",
      "source": "server",
      "keyField": "facility_id",
      "minVersion": "2026-07",
      "columns": [
        { "name": "facility_id", "type": "string", "label": { "en": "Facility ID" } },
        { "name": "name", "type": "string", "label": { "en": "Facility name" } },
        { "name": "type", "type": "string", "label": { "en": "Facility type (school | health_centre | hospital | wash)" } },
        { "name": "gov_code", "type": "string" },
        { "name": "dist_code", "type": "string" },
        { "name": "partner", "type": "string", "label": { "en": "Implementing partner" } },
        { "name": "capacity", "type": "number", "label": { "en": "Design capacity (students / beds / users)" } }
      ],
      "ext": { "org.example.tpm": { "owner": "MoE/MoH registers", "rowsApprox": 4200 } }
    }
  ],
  "pages": [
    {
      "id": "visit",
      "title": { "en": "Visit details", "ar": "بيانات الزيارة" },
      "elements": [
        { "type": "hidden", "name": "enumerator", "default": { "expr": "${meta.username}" } },
        { "type": "hidden", "name": "device_id", "default": { "expr": "${meta.deviceId}" } },
        { "type": "hidden", "name": "started_at", "default": { "expr": "${meta.startedAt}" } },
        {
          "type": "date",
          "name": "visit_date",
          "label": { "en": "Visit date", "ar": "تاريخ الزيارة" },
          "required": true,
          "default": { "expr": "today()" },
          "constraint": ". <= today()",
          "constraintMessage": { "en": "The visit date cannot be in the future.", "ar": "لا يمكن أن يكون تاريخ الزيارة في المستقبل." },
          "validators": [
            { "type": "expr", "expr": "dateDiff(., today(), 'days') <= 3", "message": { "en": "Visits should be entered within 3 days.", "ar": "يجب إدخال الزيارات خلال 3 أيام." }, "severity": "warning" }
          ]
        },
        {
          "type": "time",
          "name": "arrival_time",
          "label": { "en": "Arrival time at the facility", "ar": "وقت الوصول إلى المرفق" },
          "required": true
        },
        {
          "type": "select_one",
          "name": "facility_type",
          "label": { "en": "Facility type", "ar": "نوع المرفق" },
          "required": true,
          "props": { "list": "facility_type" },
          "appearance": { "variant": "buttons", "columns": 2, "size": "lg" }
        },
        { "type": "calculate", "name": "sector", "calculate": "if(${facility_type} = 'school', 'education', if(${facility_type} = 'wash', 'wash', 'health'))" },
        {
          "type": "select_one",
          "name": "governorate",
          "label": { "en": "Governorate", "ar": "المحافظة" },
          "required": true,
          "props": { "list": "governorate" },
          "appearance": { "variant": "dropdown" }
        },
        {
          "type": "select_one",
          "name": "district",
          "label": { "en": "District", "ar": "المديرية" },
          "required": true,
          "props": { "list": "district", "choiceFilter": "gov_code = ${governorate}", "search": true },
          "appearance": { "variant": "dropdown" }
        },
        {
          "type": "select_one",
          "name": "facility",
          "label": { "en": "Facility", "ar": "المرفق" },
          "hint": { "en": "Type part of the name to search the offline register.", "ar": "اكتب جزءاً من الاسم للبحث في السجل غير المتصل." },
          "required": true,
          "props": { "list": "facility", "choiceFilter": "dist_code = ${district} and type = ${facility_type}", "search": true },
          "appearance": { "variant": "dropdown" },
          "bind": { "index": true }
        },
        { "type": "calculate", "name": "facility_name", "calculate": "pulldata('facilities', 'name', 'facility_id', ${facility})" },
        { "type": "calculate", "name": "facility_partner", "calculate": "pulldata('facilities', 'partner', 'facility_id', ${facility})" },
        { "type": "calculate", "name": "facility_capacity", "calculate": "number(pulldata('facilities', 'capacity', 'facility_id', ${facility}))" },
        {
          "type": "note",
          "name": "n_facility",
          "label": { "en": "Selected: **{facility_name}** (partner: {facility_partner}, capacity {facility_capacity}).", "ar": "المرفق المختار: **{facility_name}** (الشريك: {facility_partner}، السعة {facility_capacity})." },
          "relevant": "notEmpty(${facility})",
          "props": { "style": "success" }
        },
        {
          "type": "select_one",
          "name": "facility_found",
          "label": { "en": "Was the facility found and operating at the registered location?", "ar": "هل تم العثور على المرفق وهو يعمل في الموقع المسجّل؟" },
          "required": true,
          "props": { "list": "yes_no" },
          "appearance": { "variant": "buttons" }
        },
        {
          "type": "text",
          "name": "not_found_reason",
          "label": { "en": "Describe what you found instead", "ar": "صف ما وجدته بدلاً من ذلك" },
          "relevant": "${facility_found} = 'no'",
          "required": "${facility_found} = 'no'",
          "props": { "multiline": true, "maxLength": 1000 }
        },
        {
          "type": "geopoint",
          "name": "visit_location",
          "label": { "en": "Location at the facility entrance", "ar": "الموقع عند مدخل المرفق" },
          "required": true,
          "props": { "accuracyThreshold": 10, "warningThreshold": 30, "autoCapture": true, "allowManual": true, "map": true }
        },
        {
          "type": "image",
          "name": "facility_photo",
          "label": { "en": "Photo of the facility entrance / signboard", "ar": "صورة مدخل المرفق / اللافتة" },
          "required": true,
          "props": { "source": "camera", "maxPixels": 1600, "quality": 0.75, "geotag": true }
        }
      ]
    },
    {
      "id": "infrastructure",
      "title": { "en": "Infrastructure checklist", "ar": "قائمة تحقق البنية التحتية" },
      "relevant": "${facility_found} = 'yes'",
      "elements": [
        {
          "type": "group",
          "name": "infra",
          "label": { "en": "Building and utilities", "ar": "المبنى والمرافق" },
          "appearance": { "variant": "section" },
          "elements": [
            {
              "type": "select_one",
              "name": "building_condition",
              "label": { "en": "Overall condition of the building", "ar": "الحالة العامة للمبنى" },
              "required": true,
              "props": { "list": "condition" },
              "appearance": { "variant": "radio" }
            },
            {
              "type": "select_one",
              "name": "water_available",
              "label": { "en": "Is safe water available on site today?", "ar": "هل تتوفر مياه آمنة في الموقع اليوم؟" },
              "required": true,
              "props": { "list": "yes_partial_no" },
              "appearance": { "variant": "buttons" }
            },
            {
              "type": "select_one",
              "name": "electricity",
              "label": { "en": "Is electricity available (grid, generator or solar)?", "ar": "هل تتوفر الكهرباء (شبكة أو مولد أو طاقة شمسية)؟" },
              "required": true,
              "props": { "list": "yes_partial_no" },
              "appearance": { "variant": "buttons" }
            },
            {
              "type": "number",
              "name": "latrines_total",
              "label": { "en": "Total number of latrines / toilets", "ar": "العدد الإجمالي للمراحيض" },
              "required": true,
              "props": { "kind": "integer", "min": 0, "max": 200 }
            },
            {
              "type": "number",
              "name": "latrines_functional",
              "label": { "en": "Number of functional latrines", "ar": "عدد المراحيض العاملة" },
              "required": true,
              "relevant": "${latrines_total} > 0",
              "props": { "kind": "integer", "min": 0, "max": 200 },
              "constraint": ". <= ${latrines_total}",
              "constraintMessage": { "en": "Functional latrines cannot exceed the total ({latrines_total}).", "ar": "لا يمكن أن يتجاوز عدد المراحيض العاملة العدد الإجمالي ({latrines_total})." }
            },
            {
              "type": "select_one",
              "name": "latrines_separate",
              "label": { "en": "Are there separate latrines for women/girls and men/boys?", "ar": "هل توجد مراحيض منفصلة للنساء/الفتيات والرجال/الفتيان؟" },
              "relevant": "${latrines_total} > 0",
              "props": { "list": "yes_no" },
              "appearance": { "variant": "buttons" }
            },
            {
              "type": "select_one",
              "name": "accessible_entrance",
              "label": { "en": "Is the entrance accessible for wheelchair users (ramp, no steps)?", "ar": "هل المدخل مهيأ لمستخدمي الكراسي المتحركة (منحدر، بدون درجات)؟" },
              "required": true,
              "props": { "list": "yes_partial_no" },
              "appearance": { "variant": "buttons" }
            },
            {
              "type": "image",
              "name": "infra_photos",
              "label": { "en": "Photos of the building and utilities", "ar": "صور المبنى والمرافق" },
              "props": { "source": "both", "maxPixels": 1280, "quality": 0.7, "multiple": true, "maxCount": 4, "geotag": true }
            }
          ],
          "ext": { "org.example.tpm": { "checklist": "INFRA-v2" } }
        },
        {
          "type": "select_multiple",
          "name": "services",
          "label": { "en": "Which services were observed to be functioning during the visit?", "ar": "ما هي الخدمات التي لوحظ أنها تعمل أثناء الزيارة؟" },
          "hint": { "en": "Only options relevant to this facility type are listed.", "ar": "تُعرض فقط الخيارات المتعلقة بنوع هذا المرفق." },
          "required": true,
          "props": { "list": "services", "choiceFilter": "sector = ${sector}", "minSelected": 1 },
          "appearance": { "variant": "chips" }
        }
      ]
    },
    {
      "id": "staffing",
      "title": { "en": "Staffing and attendance", "ar": "الكوادر والحضور" },
      "relevant": "${facility_found} = 'yes'",
      "elements": [
        {
          "type": "group",
          "name": "staff",
          "label": { "en": "Staff", "ar": "الكوادر" },
          "appearance": { "variant": "field-list", "columns": 2 },
          "elements": [
            {
              "type": "number",
              "name": "staff_expected",
              "label": { "en": "Staff on the duty roster today", "ar": "عدد الموظفين في جدول المناوبة اليوم" },
              "required": true,
              "props": { "kind": "integer", "min": 1, "max": 500 }
            },
            {
              "type": "number",
              "name": "staff_present",
              "label": { "en": "Staff physically present", "ar": "عدد الموظفين الحاضرين فعلياً" },
              "required": true,
              "props": { "kind": "integer", "min": 0, "max": 500 },
              "constraint": ". <= ${staff_expected}",
              "constraintMessage": { "en": "Present staff cannot exceed the roster ({staff_expected}).", "ar": "لا يمكن أن يتجاوز عدد الحاضرين عدد الموظفين في الجدول ({staff_expected})." },
              "validators": [
                { "type": "expr", "expr": ". >= ${staff_expected} * 0.5", "message": { "en": "Less than half of the roster is present – record a staffing issue on the next page.", "ar": "أقل من نصف الموظفين حاضرون – سجّل مشكلة كوادر في الصفحة التالية." }, "severity": "warning" }
              ]
            },
            {
              "type": "number",
              "name": "staff_female",
              "label": { "en": "Of those present, how many are women?", "ar": "من الحاضرين، كم عدد النساء؟" },
              "relevant": "${staff_present} > 0",
              "props": { "kind": "integer", "min": 0, "max": 500 },
              "constraint": ". <= ${staff_present}",
              "constraintMessage": { "en": "Cannot exceed the number of staff present.", "ar": "لا يمكن أن يتجاوز عدد الموظفين الحاضرين." }
            },
            { "type": "calculate", "name": "staff_ratio", "calculate": "if(${staff_expected} > 0, round(${staff_present} / ${staff_expected} * 100), 0)" },
            {
              "type": "note",
              "name": "n_staff_ratio",
              "label": { "en": "Attendance today: **{staff_ratio}%** of the roster.", "ar": "الحضور اليوم: **{staff_ratio}%** من الجدول." },
              "relevant": "notEmpty(${staff_present})",
              "props": { "style": "info" }
            }
          ]
        },
        {
          "type": "group",
          "name": "school_attendance",
          "label": { "en": "Students present today", "ar": "الطلاب الحاضرون اليوم" },
          "relevant": "${facility_type} = 'school'",
          "appearance": { "variant": "card" },
          "elements": [
            {
              "type": "number",
              "name": "students_girls",
              "label": { "en": "Girls", "ar": "الفتيات" },
              "required": true,
              "props": { "kind": "integer", "min": 0, "max": 5000 }
            },
            {
              "type": "number",
              "name": "students_boys",
              "label": { "en": "Boys", "ar": "الفتيان" },
              "required": true,
              "props": { "kind": "integer", "min": 0, "max": 5000 }
            },
            { "type": "calculate", "name": "students_total", "calculate": "coalesce(${students_girls}, 0) + coalesce(${students_boys}, 0)" },
            {
              "type": "note",
              "name": "n_students",
              "label": { "en": "Total present: **{students_total}** (design capacity {facility_capacity}).", "ar": "إجمالي الحاضرين: **{students_total}** (السعة التصميمية {facility_capacity})." },
              "relevant": "${students_total} > 0",
              "props": { "style": "info" }
            },
            {
              "type": "select_one",
              "name": "over_capacity_ack",
              "label": { "en": "Attendance exceeds the registered capacity – confirm the count is correct", "ar": "الحضور يتجاوز السعة المسجّلة – أكّد صحة العدد" },
              "relevant": "${facility_capacity} > 0 and ${students_total} > ${facility_capacity}",
              "required": "${facility_capacity} > 0 and ${students_total} > ${facility_capacity}",
              "props": { "list": "yes_no" },
              "appearance": { "variant": "buttons" }
            }
          ]
        },
        {
          "type": "group",
          "name": "health_load",
          "label": { "en": "Patient load", "ar": "عبء المرضى" },
          "relevant": "${sector} = 'health'",
          "appearance": { "variant": "card" },
          "elements": [
            {
              "type": "number",
              "name": "patients_yesterday",
              "label": { "en": "Outpatient consultations yesterday (from the register)", "ar": "عدد استشارات العيادات الخارجية أمس (من السجل)" },
              "required": true,
              "props": { "kind": "integer", "min": 0, "max": 5000 }
            },
            {
              "type": "select_one",
              "name": "stockout_7d",
              "label": { "en": "Any stock-out of essential drugs in the past 7 days?", "ar": "هل حدث نفاد لأي من الأدوية الأساسية خلال الأيام السبعة الماضية؟" },
              "required": true,
              "props": { "list": "yes_no" },
              "appearance": { "variant": "buttons" }
            }
          ]
        }
      ]
    },
    {
      "id": "issues",
      "title": { "en": "Issues observed", "ar": "المشكلات الملاحظة" },
      "relevant": "${facility_found} = 'yes'",
      "elements": [
        {
          "type": "note",
          "name": "n_issues",
          "label": { "en": "Log every problem that needs action. Take a photo for **high** and **critical** issues.", "ar": "سجّل كل مشكلة تتطلب إجراءً. التقط صورة للمشكلات **العالية** و**الحرجة**." },
          "props": { "style": "warning" }
        },
        {
          "type": "repeat",
          "name": "issues",
          "label": { "en": "Issues", "ar": "المشكلات" },
          "props": {
            "min": 0,
            "max": 20,
            "itemLabel": "concat(position(), '. ', coalesce(${issue_title}, '…'), if(notEmpty(${severity}), concat(' [', upper(${severity}), ']'), ''))",
            "addLabel": { "en": "Add issue", "ar": "إضافة مشكلة" },
            "removeLabel": { "en": "Remove issue", "ar": "إزالة المشكلة" },
            "keyField": "issue_title",
            "confirmDelete": true,
            "allowReorder": true
          },
          "elements": [
            {
              "type": "select_one",
              "name": "issue_category",
              "label": { "en": "Category", "ar": "الفئة" },
              "required": true,
              "props": { "list": "issue_category" },
              "appearance": { "variant": "dropdown" }
            },
            {
              "type": "text",
              "name": "issue_title",
              "label": { "en": "Short title", "ar": "عنوان مختصر" },
              "required": true,
              "props": { "maxLength": 80, "placeholder": { "en": "e.g. Roof leaking in classroom 3", "ar": "مثال: تسرّب في سقف الصف 3" } },
              "validators": [
                { "type": "length", "min": 5, "message": { "en": "Give at least 5 characters so the title is meaningful.", "ar": "أدخل 5 أحرف على الأقل ليكون العنوان ذا معنى." } }
              ]
            },
            {
              "type": "text",
              "name": "issue_desc",
              "label": { "en": "Description and location within the facility", "ar": "الوصف والموقع داخل المرفق" },
              "props": { "multiline": true, "maxLength": 2000 }
            },
            {
              "type": "select_one",
              "name": "severity",
              "label": { "en": "Severity", "ar": "درجة الخطورة" },
              "required": true,
              "props": { "list": "severity" },
              "appearance": { "variant": "buttons", "columns": 4 }
            },
            { "type": "calculate", "name": "is_critical", "calculate": "if(${severity} = 'critical', 1, 0)" },
            {
              "type": "image",
              "name": "issue_photo",
              "label": { "en": "Photo of the issue", "ar": "صورة المشكلة" },
              "required": "${severity} = 'high' or ${severity} = 'critical'",
              "requiredMessage": { "en": "A photo is mandatory for high and critical issues.", "ar": "الصورة إلزامية للمشكلات العالية والحرجة." },
              "props": { "source": "camera", "maxPixels": 1600, "quality": 0.75, "annotate": true, "geotag": true, "multiple": true, "maxCount": 3 }
            },
            {
              "type": "select_one",
              "name": "reported_before",
              "label": { "en": "Was this issue already reported to the partner?", "ar": "هل تم إبلاغ الشريك بهذه المشكلة سابقاً؟" },
              "props": { "list": "yes_no" },
              "appearance": { "variant": "buttons" }
            },
            {
              "type": "date",
              "name": "reported_date",
              "label": { "en": "When was it reported?", "ar": "متى تم الإبلاغ عنها؟" },
              "relevant": "${reported_before} = 'yes'",
              "constraint": ". <= ${../visit_date}",
              "constraintMessage": { "en": "Cannot be after the visit date.", "ar": "لا يمكن أن يكون بعد تاريخ الزيارة." },
              "appearance": { "variant": "no-calendar" }
            }
          ],
          "ext": { "org.example.tpm": { "table": "issues", "exportSheet": "Issues" } }
        }
      ]
    },
    {
      "id": "perimeter",
      "title": { "en": "Site perimeter (optional)", "ar": "محيط الموقع (اختياري)" },
      "relevant": "${facility_found} = 'yes' and (${facility_type} = 'school' or ${facility_type} = 'wash')",
      "elements": [
        {
          "type": "note",
          "name": "n_perimeter",
          "label": { "en": "If time allows, walk the fence line and record the perimeter. Skip when the site is unsafe to walk.", "ar": "إذا سمح الوقت، امشِ على طول السياج وسجّل المحيط. تخطَّ هذه الخطوة إذا كان الموقع غير آمن للمشي." },
          "props": { "style": "info", "collapsible": true }
        },
        {
          "type": "geotrace",
          "name": "site_perimeter",
          "label": { "en": "Perimeter walk", "ar": "مسار محيط الموقع" },
          "props": { "mode": "auto", "intervalSeconds": 5, "accuracyThreshold": 8, "warningThreshold": 25, "allowManual": true, "map": true, "minPoints": 4 }
        },
        {
          "type": "select_one",
          "name": "fence_intact",
          "label": { "en": "Is the perimeter fence intact?", "ar": "هل سياج المحيط سليم؟" },
          "relevant": "notEmpty(${site_perimeter})",
          "props": { "list": "yes_partial_no" },
          "appearance": { "variant": "buttons" }
        }
      ]
    },
    {
      "id": "wrapup",
      "title": { "en": "Follow-up and sign-off", "ar": "المتابعة والتوقيع" },
      "elements": [
        {
          "type": "select_one",
          "name": "follow_up_required",
          "label": { "en": "Is a follow-up visit required?", "ar": "هل تلزم زيارة متابعة؟" },
          "hint": { "en": "Set to Yes automatically when a critical issue is logged.", "ar": "يُضبط على نعم تلقائياً عند تسجيل مشكلة حرجة." },
          "required": true,
          "default": { "value": "no" },
          "props": { "list": "yes_no" },
          "appearance": { "variant": "buttons" }
        },
        {
          "type": "date",
          "name": "follow_up_date",
          "label": { "en": "Proposed follow-up date", "ar": "تاريخ المتابعة المقترح" },
          "relevant": "${follow_up_required} = 'yes'",
          "required": "${follow_up_required} = 'yes'",
          "constraint": ". > today()",
          "constraintMessage": { "en": "The follow-up date must be in the future.", "ar": "يجب أن يكون تاريخ المتابعة في المستقبل." },
          "validators": [
            { "type": "expr", "expr": "dateDiff(today(), ., 'days') <= 30", "message": { "en": "Follow-ups are normally scheduled within 30 days.", "ar": "تُجدول المتابعات عادةً خلال 30 يوماً." }, "severity": "warning" }
          ]
        },
        {
          "type": "text",
          "name": "focal_name",
          "label": { "en": "Name of the facility focal point met", "ar": "اسم مسؤول الاتصال في المرفق الذي تمت مقابلته" },
          "relevant": "${facility_found} = 'yes'",
          "props": { "maxLength": 120 },
          "bind": { "sensitive": true }
        },
        {
          "type": "text",
          "name": "focal_phone",
          "label": { "en": "Focal point phone", "ar": "هاتف مسؤول الاتصال" },
          "relevant": "${facility_found} = 'yes'",
          "props": { "format": "phone", "maxLength": 15 },
          "validators": [
            { "type": "regex", "pattern": "^\\+?[0-9]{9,15}$", "message": { "en": "Digits only, optionally starting with +.", "ar": "أرقام فقط، ويمكن أن يبدأ بـ +." } }
          ],
          "bind": { "sensitive": true, "saveIncomplete": false }
        },
        {
          "type": "signature",
          "name": "focal_signature",
          "label": { "en": "Focal point signature (confirms the visit took place)", "ar": "توقيع مسؤول الاتصال (يؤكد حدوث الزيارة)" },
          "relevant": "${facility_found} = 'yes'",
          "required": "${facility_found} = 'yes'",
          "props": { "penColor": "#111111" }
        },
        {
          "type": "text",
          "name": "monitor_comments",
          "label": { "en": "Monitor's overall assessment", "ar": "التقييم العام للمراقب" },
          "props": { "multiline": true, "maxLength": 3000 }
        },
        {
          "type": "checkbox",
          "name": "ack_accuracy",
          "label": { "en": "I confirm that all observations were made in person during this visit.", "ar": "أؤكد أن جميع الملاحظات تمت شخصياً أثناء هذه الزيارة." },
          "required": true
        }
      ]
    }
  ],
  "logic": {
    "calculated": [
      { "name": "issue_count", "calculate": "count(${issues})", "includeInData": true },
      { "name": "critical_count", "calculate": "sum(${issues[].is_critical})", "includeInData": true },
      { "name": "visit_score", "calculate": "if(${facility_found} = 'yes', round((if(${water_available} = 'yes', 1, 0) + if(${electricity} = 'yes', 1, 0) + if(${accessible_entrance} = 'yes', 1, 0) + if(${building_condition} = 'good', 1, 0)) / 4 * 100), null)", "includeInData": true }
    ],
    "triggers": [
      {
        "id": "t-not-found",
        "when": "${facility_found} = 'no'",
        "on": "pageLeave",
        "actions": [
          { "type": "showMessage", "message": { "en": "Facility not found – the checklist pages are skipped. Complete the sign-off page.", "ar": "لم يتم العثور على المرفق – سيتم تخطي صفحات قائمة التحقق. أكمل صفحة التوقيع." }, "severity": "warning" },
          { "type": "setValue", "target": "follow_up_required", "value": "yes" },
          { "type": "skipTo", "page": "wrapup" }
        ]
      },
      {
        "id": "t-critical",
        "when": "${critical_count} > 0",
        "once": true,
        "actions": [
          { "type": "setValue", "target": "follow_up_required", "value": "yes" },
          { "type": "showMessage", "message": { "en": "A critical issue was logged. Follow-up is now required and the supervisor will be notified on sync.", "ar": "تم تسجيل مشكلة حرجة. أصبحت المتابعة مطلوبة وسيتم إشعار المشرف عند المزامنة." }, "severity": "error", "blocking": true }
        ],
        "ext": { "org.example.tpm": { "sla": "48h" } }
      },
      {
        "id": "t-notify-supervisor",
        "when": "${critical_count} > 0",
        "on": "finalize",
        "actions": [
          { "type": "custom", "id": "tpm.notifySupervisor", "payload": { "channel": "email", "template": "critical-issue", "includeFields": ["facility_name", "critical_count", "visit_location"] } }
        ]
      },
      {
        "id": "t-low-attendance",
        "when": "${staff_expected} > 0 and ${staff_present} < ${staff_expected} * 0.5",
        "on": "pageLeave",
        "once": true,
        "actions": [
          { "type": "showMessage", "message": { "en": "Staff attendance is below 50 % – remember to log a staffing issue.", "ar": "حضور الموظفين أقل من 50٪ – تذكّر تسجيل مشكلة كوادر." }, "severity": "warning" }
        ]
      }
    ]
  },
  "ext": {
    "org.example.tpm": { "formCode": "FMV-02", "reportingLine": "TPM Consortium – Field Quality Team", "powerBiDataset": "tpm_facility_visits" }
  }
}
