@extends('layouts.app') @push('styles') @endpush @section('content') @auth @php $editingId = request()->query('edit'); $editingRecord = $editingId ? \App\Models\PreTbRecord::find($editingId) : null; $isEditingExistingRecord = (bool) $editingRecord; $defaultClinic = config('tb_entry.default_clinic', 'Myitkyina'); $currentSessionClinic = session('tb.clinic', $defaultClinic); $clinicOptions = array_values(array_filter((array) ($clinics ?? config('tb_entry.clinics', [])))); $clinicPrefixes = (array) config('tb_entry.general_id_prefixes', []); $recordClinicValue = old('clinic_name', $editingRecord->clinic_name ?? $currentSessionClinic); if ($recordClinicValue && !in_array($recordClinicValue, $clinicOptions, true)) { $clinicOptions[] = $recordClinicValue; } $riskOptionsCurrentExNoUk = [ ['1', 'Current'], ['2', 'Ex'], ['3', 'No'], ['4', 'UK'], ]; $riskOptionsYesNo = [ ['1', 'Yes'], ['2', 'No'], ]; $riskOptionsYesNoUk = [ ['1', 'Yes'], ['2', 'No'], ['3', 'UK'], ]; $symptomRiskRows = [ [ 'symptom' => ['slug' => 'fever', 'label' => 'Fever'], 'risk' => ['id' => 'alcohol', 'field' => 'alcohol', 'label' => 'Alcohol', 'options' => $riskOptionsCurrentExNoUk], ], [ 'symptom' => ['slug' => 'cough', 'label' => 'Cough'], 'risk' => ['id' => 'smoking', 'field' => 'smoking', 'label' => 'Smoking', 'options' => $riskOptionsCurrentExNoUk], ], [ 'symptom' => ['slug' => 'hemoptysis', 'label' => 'Hemoptysis'], 'risk' => ['id' => 'malnutrition', 'field' => 'malnutrition', 'label' => 'Malnutrition', 'options' => $riskOptionsYesNo], ], [ 'symptom' => ['slug' => 'weight_loss', 'label' => 'Weight loss'], 'risk' => ['id' => 'PWID', 'field' => 'PWID', 'label' => 'PWID', 'options' => $riskOptionsCurrentExNoUk], ], [ 'symptom' => ['slug' => 'appetite_loss', 'label' => 'Loss of appetite'], 'risk' => ['id' => 'PWUD', 'field' => 'PWUD', 'label' => 'PWUD', 'options' => $riskOptionsCurrentExNoUk], ], [ 'symptom' => ['slug' => 'chest_pain', 'label' => 'Chest pain'], 'risk' => [ 'id' => 'DM', 'field' => 'DM', 'label' => 'DM', 'options' => $riskOptionsYesNoUk, 'tx' => [ 'id' => 'dm_tx_status', 'field' => 'dm_tx_status', 'wrap_id' => 'dm_tx_status_wrap', 'options' => [ ['1', 'on Tx'], ['2', 'not Tx'], ], ], ], ], [ 'symptom' => ['slug' => 'night_sweats', 'label' => 'Night sweats'], 'risk' => [ 'id' => 'hiv_status', 'field' => 'hiv_status', 'label' => 'HIV', 'options' => $riskOptionsYesNoUk, 'tx' => [ 'id' => 'hiv_tx', 'field' => 'hiv_tx', 'wrap_id' => 'hiv_tx_wrap', 'options' => [ ['1', 'on Tx'], ['2', 'not Tx'], ], ], ], ], [ 'symptom' => ['slug' => 'neck_glands', 'label' => 'Neck glands'], 'risk' => ['id' => 'his_tb_self', 'field' => 'his_tb_self', 'label' => 'Hist. of TB (self)', 'options' => $riskOptionsYesNoUk], ], [ 'symptom' => ['slug' => 'fatigue', 'label' => 'Fatigue'], 'risk' => ['id' => 'his_tb_family', 'field' => 'his_tb_family', 'label' => 'Hist. of TB (family)', 'options' => $riskOptionsYesNoUk], ], ]; $selectedSpecimens = static function ($value): array { $modeAliasToCode = [ '1' => '1', 'general' => '1', '2' => '2', 'plhiv' => '2', '3' => '3', 'diabetes' => '3', 'ncd' => '3', '7' => '3', '4' => '4', 'remote (icmv)' => '4', 'remote icmv' => '4', '5' => '5', 'remote (tb mobile)' => '5', 'remote tb mobile' => '5', '6' => '6', 'contact tracing' => '6', ]; $toModeCode = static function (string $raw) use ($modeAliasToCode): ?string { $v = trim($raw); if ($v === '') { return null; } $lower = mb_strtolower($v); if (array_key_exists($lower, $modeAliasToCode)) { return $modeAliasToCode[$lower]; } if (is_numeric($v)) { $num = (int) $v; if ($num >= 1 && $num <= 6) { return (string) $num; } } return $v; }; $normalize = static function ($item) use ($toModeCode): ?string { if ($item === null) { return null; } $v = trim((string) $item); if ($v === '') { return null; } return $toModeCode($v); }; if (is_array($value)) { $out = []; foreach ($value as $item) { $v = $normalize($item); if ($v !== null) { $out[] = $v; } } return array_values(array_unique($out)); } $raw = trim((string) $value); if ($raw === '') { return []; } // Handle JSON-like arrays stored in old payloads, e.g. ["4","5"]. if (str_starts_with($raw, '[') && str_ends_with($raw, ']')) { $decoded = json_decode($raw, true); if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) { $out = []; foreach ($decoded as $item) { $v = $normalize($item); if ($v !== null) { $out[] = $v; } } return array_values(array_unique($out)); } } $out = []; foreach (preg_split('/\s*,\s*/', $raw) ?: [] as $item) { $v = $normalize($item); if ($v !== null) { $out[] = $v; } } return array_values(array_unique($out)); }; $modeOfEntryOptions = [ '1' => 'General', '2' => 'PLHIV', '3' => 'Diabetes', '4' => 'Remote (ICMV)', '5' => 'Remote (TB mobile)', '6' => 'Contact tracing', ]; $modeOfEntryValue = old('mode_of_entry', $editingRecord->mode_of_entry ?? null); $selectedModeOfEntry = $selectedSpecimens($modeOfEntryValue); $selectedModeOfEntry = array_values(array_unique(array_map(fn($v) => (string) $v, $selectedModeOfEntry))); @endphp