<?php
$fields = [
    [
        "Head:::Trauma",
        "Eyes:::Blindness|Cataracts|Glaucoma|Wears glasses/contacts",
        "Ears:::Hearing aids",
        "Nose/Sinuses:::Allergic Rhinitis|Sinus infections",
        "Mouth/Throat/Teeth:::Dentures",
        "Cardiovascular:::Aneurysm|Angina|DVT|Dysrhythmia|HTN|Murmur|Myocardial infarction|Other heart disease",
    ],
    [
        "Respiratory:::Asthma|Bronchitis|COPD - Bronchitis/Emphysema|Pleuritis|Pneumonia",
        "Gastrointestinal:::Cirrhosis|GERD|Gallbladder disease|Heartburn|Hemorrhoids|Hepatitis|Hiatal hernia|Jaundice|Ulcer",
        "Genitourinary:::Hernia|Incontinence|Nephrolithiasis|Other kidney disease|STDs|UTI(s)",
    ],
    [
        "Musculoskeletal:::Arthritis|Gout|M/S injury",
        "Skin:::Dermatitis|Mole(s)|Other skin condition(s)|Psoriasis",
        "Neurological:::Epilepsy|Seizures|Severe headaches, migraines|Stroke|TIA",
        "Psychiatric:::Bipolar disorder|Depression|Hallucinations, delusions|Suicidal ideation|Suicide attempts",
    ],
    [
        "Endocrine:::Goiter|Hyperlipidemia|Hypothyroidism|Thyroid disease|Thyroiditis|Type I DM|Type II DM",
        "Heme/Onc:::Anemia|Cancer",
        "Infectious:::HIV|STDs|Tuberculosis (dz)|Tuberculosis (exposure)",
    ]
];

$customFields = [];

if($section) {
    $customItems = $section->sectionTemplate->customItems;
    $perColumn = floor(count($customItems) / 4);
    if($perColumn < 5) $perColumn = 5;
    $inColumn = 0;
    foreach ($customItems as $customItem) {
        if(!isset($customFields[$inColumn])) $customFields[$inColumn] = [];
        $customFields[$inColumn][] = $customItem->label;
        if(count($customFields[$inColumn]) >= $perColumn) {
            $inColumn++;
        }
    }
    for ($i=0; $i<count($customFields); $i++) {
        $customFields[$i] = [':::' . implode("|", $customFields[$i])];
    }
}

//$customFields = [
//    [
//        ":::Anxiety disorder|BPH|carpal tunnel|CHF|Degernative Disc Disorder|Fibromyalgia",
//    ],
//    [
//        ":::Hard of Hearing|Hearing Impairment|Lupus|Multiple Falls|Multiple kidney stones|Multiple Rib Fractures",
//    ],
//    [
//        ":::Neuropathy|Obesity|Obstructive Sleep Apnea|Primary immunodeficiency diso|pulmonary hypertension|Restless Leg Syndrome",
//    ],
//    [
//        ":::Rhinitis|Russell Silver Syndrome|transverse myelitis|Vertigo",
//    ]
//];

$blood = [];
if(isset($newContentData['blood_type'])) {
    $blood[] = $newContentData['blood_type'];
}
if(isset($newContentData['blood_rh'])) {
    $blood[] = $newContentData['blood_rh'];
}
$blood = implode(" ", $blood);
if(!empty($blood)) { ?>
    <div class="my-2">
        <div>Blood Type &amp; RH: <b><?= $blood ?></b></div>
    </div>
<?php }

for ($i = 0; $i < count($fields); $i++) {
    for($j = 0; $j < count($fields[$i]); $j++) {
        $parts = explode(":::", $fields[$i][$j]);
        $head = 'custom';
        if(!empty($parts[0])) $head = $parts[0];
        $values = explode("|", $parts[1]);
        for($k = 0; $k < count($values); $k++) {
            $fName = $head . '_' . sanitize_field_name($values[$k]);
            if(isset($newContentData[$fName])) { ?>
                <div>
                    <?= ucwords($head) ?>
                    <i class="fa fa-arrow-right text-sm text-secondary"></i>
                    <span class="font-weight-bold"><?= $values[$k] ?></span>
                    <?php if(@$newContentData[$fName . '__comments']): ?>
                        <span class="text-sm ml-1 text-secondary">(<?= $newContentData[$fName . '__comments'] ?>)</span>
                    <?php endif; ?>
                </div>
            <?php }
        }
    }
}

for ($i = 0; $i < count($customFields); $i++) {
    for($j = 0; $j < count($customFields[$i]); $j++) {
        $parts = explode(":::", $customFields[$i][$j]);
        $head = 'custom';
        if(!empty($parts[0])) $head = $parts[0];
        $values = explode("|", $parts[1]);
        for($k = 0; $k < count($values); $k++) {
            $fName = $head . '_' . sanitize_field_name($values[$k]);
            if(isset($newContentData[$fName])) { ?>
                <div>
                    <?= ucwords($head) ?>
                    <i class="fa fa-arrow-right text-sm text-secondary"></i>
                    <span class="font-weight-bold"><?= $values[$k] ?></span>
                    <?php if(@$newContentData[$fName . '__comments']): ?>
                        <span class="text-sm ml-1 text-secondary">(<?= $newContentData[$fName . '__comments'] ?>)</span>
                    <?php endif; ?>
                </div>
            <?php }
        }
    }
}

if(isset($newContentData['comments'])) { ?>
    <div class="mt-2 mb-1">
        <div class="font-weight-bold">Comments:</div>
        <div><?= $newContentData['comments'] ?></div>
    </div>
<?php }
