<?php

use App\Models\Client;
use App\Models\Point;
use App\Models\Note;
/** @var Client $patient */
/** @var Note $note */

$fields = [
    [
        "Constitutional" => [
            "Fatigue",
            "Fever",
            "Weight gain",
            "Weight loss"],

        "Eyes" => [
            "Blurry vision",
            "Double vision",
            "Dry eyes",
            "Floaters",
            "Halos",
            "Headache",
            "Irritation",
            "Loss of peripheral vision",
            "Pain",
            "Pinpoint pupils",
            "Teary",
            "Visual changes"],

        "ENMT" => [
            "Altered sense of smell",
            "Dental caries",
            "Dentures",
            "Dizziness",
            "Dry mouth",
            "Ear pain/pressure",
            "Hearing loss",
            "Mouth pain",
            "Nasal congestion",
            "Neck pain/stiffness",
            "Post nasal drainage",
            "Sinus pain / pressure",
            "Sore throat",
            "Teeth missing",
            "Tinnitus",
            "Trouble swallowing"],
        "Allergy / Immune" => [
            "Congestion",
            "Difficulty healing",
            "Frequent infections",
            "Headache",
            "Hives",
            "Itching",
            "Running nose",
            "Sneezing",
            "Watery eyes"]
    ],
    [
        "Cardiovascular" => [
            "Chest pain / pressure at rest",
            "Chest pain / pressure with excursion",
            "Cold hands / feet",
            "Dizziness",
            "Exercise intolerance",
            "Fainting",
            "Feeling faint",
            "Left arm pain",
            "Light headed",
            "Lower leg cramps",
            "Muscle weakness",
            "Palpitations",
            "Shortness of breath",
            "Sweating",
            "Swelling"],

        "Respiratory" => [
            "Cough – dry",
            "Cough – wet",
            "Coughing up blood",
            "Pain with breathing",
            "Productive cough",
            "Shortness of breath at rest",
            "Shortness of breath on excursion",
            "Wheezing"],

        "Gastrointestinal" => [
            "Abdominal pain",
            "Constipation",
            "Coughing up blood",
            "Diarrhea",
            "Difficulty swallowing",
            "Fecal incontinence",
            "Heartburn",
            "Loose stool",
            "Nausea",
            "Painful swallowing",
            "Rectal pain",
            "Stool – black tarry",
            "Stool – bloody"],
    ],
    [
        "Genitourinary" => [
            "Blood in urine",
            "Discharge",
            "Foley",
            "Incontinence",
            "Lesions",
            "Sexual dysfunction",
            "Tenderness",
            "Urinary retention",
            "Urination - frequent",
            "Urination - painful",
            "Urination wakes you up at night"],

        "Musculoskeletal" => [
            "Decreased range of motion",
            "Fracture",
            "Gait - unstable",
            "Joint instability",
            "Joint pain",
            "Joint stiffness",
            "Joint swelling",
            "Muscle pain",
            "Muscle weakness",
            "Sprain"],

        "Skin" => [
            "Bruises",
            "Color changes",
            "Dry skin",
            "Goosebumps",
            "Itchy",
            "IVDU marks",
            "Lesions",
            "Nodules",
            "Rash",
            "Scars",
            "Swelling",
            "Track marks",
            "Ulcer",
            "Wound"],

    ],
    [
        "Neurological" => [
            "Coordination problem",
            "Dizziness",
            "Double vision",
            "Fainting",
            "Head trauma",
            "Headache",
            "Imbalance",
            "Muscle twitching",
            "Numbness",
            "Problems walking",
            "Restlessness",
            "Seizures",
            "Speech disturbance",
            "TBI",
            "Tingling",
            "Tinnitus",
            "Tremor",
            "Vertigo",
            "Weakness"],

        "Endocrine" => [
            "Change in hair growth",
            "Cold intolerance",
            "Diabetes",
            "Excessive hunger",
            "Excessive thirst",
            "Excessive urination",
            "Exhausted",
            "Goiter",
            "Heat intolerance",
            "Hot and cold flashes",
            "Sleeplessness",
            "Weight gain",
            "Weight loss"],

        "Lymphatic" => [
            "Anemia",
            "Bleeding tendency",
            "Bruises",
            "Lymph node pain enlargement",
            "Pain",
            "Swelling",
            "Transfusions"],
    ]
];

$point = Point::getOrCreateOnlyTopLevelPointOfCategory($note, 'ROS', $sessionKey, true);

$rel = !!$point->relevanceToNote($note);

if ($point->lastChildReview && $point->lastChildReview->data && ($rel || $point->lastChildReview->added_in_note_id === $note->id)) {
    $point->lastChildReview->data = json_decode($point->lastChildReview->data, true);
    $contentData = $point->lastChildReview->data;

    if(!!@$contentData['declaration']) {
        echo '<div class="mb-1">';
        echo '<i class="fa fa-check"></i>&nbsp;';
        echo "Except for the systems documented in the HPI or detailed below, all other systems were reviewed and negative.";
        echo '</div>';
    }

    // group by "head" and separate fields with values
    $grouped = [];
    for ($i = 0; $i < count($fields); $i++) {
        foreach ($fields[$i] as $head => $values) {
            for ($k = 0; $k < count($values); $k++) {
                $fName = $head . '_' . sanitize_field_name($values[$k]);
                if (@$contentData['common'][$fName]) {
                    if(!isset($grouped[$head])) $grouped[$head] = [];
                    $grouped[$head][] = [
                        "label" => $values[$k],
                        "value" => @$contentData['common'][$fName],
                        "comments" => @$contentData['common'][$fName . '__comments']
                    ];
                }
            }
        }
    }

    $display = [];
    foreach($grouped as $head => $items) {
        $combined = [];
        for ($i = 0; $i < count($items); $i++) {
            $combined[] = $items[$i]['label'] . ' (' . $items[$i]['value'] . ')' .
                (@$items[$i]['comments'] ? '<span class="text-secondary font-italic text-sm">&nbsp;' . @$items[$i]['comments'] . '</span>' : '');
        }
        $display[$head] = implode(' • ', $combined);
    }

    foreach($display as $head => $line) {
        ?>
        <div class="">
            <span class="font-weight-bold mr-1">{{$head}}:</span>
            <span class="">{!! $line !!}</span>
        </div>
        <?php
    }
    ?>
    @if(isset($contentData['comments']) && !empty(trim($contentData['comments'])))
        <div class="mt-1">
            <b>Comments: </b><?= $contentData['comments'] ?>
        </div>
    @endif
    <?php
} else {
    echo '<div class="text-secondary">-</div>';
}
?>