<?php

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

if(!@$note) {
    $note = $patient->coreNote;
}
if(!@$sessionKey) {
    $sessionKey = request()->cookie('sessionKey');
}

$fields = [
    [
        "" => ["Aneurysm repair", "Appendectomy", "Back surgery", "Bariatric surgery/gastric bypass", "Bilateral tubal ligation", "Breast resection/mastectomy", "CABG", "Carotid endarterectomy/stent", "Carpal tunnel release surgery",]
    ],
    [
        "" => ["Cataract/lens surgery", "Cesarean section", "Cholecystectomy/bile duct surgery", "Dilation and curettage", "Hemorrhoid surgery", "Hip arthroplasty", "Hip replacement", "Hysterectomy", "Inguinal hernia repair",]
    ],
    [
        "" => ["Knee arthroplasty", "LASIK", "Laminectomy", "Nasal surgery", "PTCA/PCI", "Pacemaker/defibrillator", "Prostate surgery", "Prostatectomy", "Rotator cuff surgery",]
    ],
    [
        "" => ["Sinus surgery", "Skin cancer excision", "Spinal fusion", "TAH-BSO", "TURP", "Tonsillectomy/Adenoidectomy", "Vasectomy",]
    ]
];

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

$contentData = $parsed = false;

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

if ($point->lastChildReview && $point->lastChildReview->data) {
    $point->lastChildReview->data = json_decode($point->lastChildReview->data, true);
    $contentData = $parsed = $point->lastChildReview->data;

    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]): ?>
                    <div>
                        <!--Common
                        <i class="fa fa-arrow-right text-sm text-secondary"></i>-->
                        <span class="font-weight-normal"><?= $values[$k] ?></span>
                        <?php if(@$contentData['common'][$fName . '__comments']): ?>
                            <span class="text-sm ml-1 text-secondary">(<?= $contentData['common'][$fName . '__comments'] ?>)</span>
                        <?php endif; ?>
                    </div>
                <?php
                endif;
            endfor;
        endforeach;
    endfor;

    // custom fields

    if(isset($contentData['customFields']) && count($contentData['customFields'])):
        ?> <div class=""> <?php
        for ($i = 0; $i < count($contentData['customFields']); $i++):
            $item = $contentData['customFields'][$i];
            if($item['value']): ?>
                <div>
                    <!--Custom
                    <i class="fa fa-arrow-right text-sm text-secondary"></i>-->
                    <span class="font-weight-bold"><?= $item['label'] ?></span>
                    <?php if($item['comments']): ?>
                        <span class="text-sm ml-1 text-secondary">(<?= $item['comments'] ?>)</span>
                    <?php endif; ?>
                </div>
            <?php
            endif;
        endfor;
        ?> </div> <?php
    endif;

    if(isset($contentData['comments']) && !empty(trim($contentData['comments']))) { ?>
        <div class="mt-2 mb-1">
            <b>Comments: </b><?= $contentData['comments'] ?>
        </div>
    <?php }
}
else {
    echo '<span class="text-secondary">-</span>';
}
?>
