<?php

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

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

$point = Point::getOrCreateOnlyTopLevelPointOfCategory($note, 'FAMILY_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;
}

$labels = [
    'general_no_health_concern' => 'No health concern',
    'general_arthritis' => 'Arthritis',
    'general_asthma' => 'Asthma',
    'general_bleeding_disorder' => 'Bleeding disorder',
    'general_cad_lt_age_55' => 'CAD &gt; age 55',
    'general_copd' => 'COPD',
    'general_diabetes' => 'Diabetes',
    'general_heart_attack' => 'Heart attack',
    'general_heart_disease' => 'Heart disease',
    'general_high_cholesterol' => 'High cholesterol',
    'general_hypertension' => 'Hypertension',
    'general_mental_illness' => 'Mental illness',
    'general_osteoporosis' => 'Osteoporosis',
    'general_stroke' => 'Stroke',
    'cancer_breast_ca' => 'Breast cancer',
    'cancer_colon_ca' => 'Colon cancer',
    'cancer_other_ca' => 'Other cancer',
    'cancer_ovarian_ca' => 'Ovarian cancer',
    'cancer_uterine_ca' => 'Uterine cancer',
];

if($contentData && !@$contentData['unknown'] && !!$contentData['count']) {
    for ($i = 0; $i < $contentData['count']; $i++) {
?>
<div class="<?= $i > 0 ? 'mt-2' : '' ?>">
    <div class="">
        <b><?= isset($contentData['items'][$i]['relationship']) ? $contentData['items'][$i]['relationship'] : '--' ?></b>
        <?php if(isset($contentData['items'][$i]['status']) && !empty($contentData['items'][$i]['status'])): ?>
        <span class="ml-1 text-secondary">(<?= $contentData['items'][$i]['status'] ?>)</span>
        <?php endif; ?>
    </div>
    <div class="ml-3">
        <?php
        $positives = [];
        $negatives = [];
        foreach ($labels as $k => $v) {
            if(isset($contentData['items'][$i][$k])) {
                if(strtolower($contentData['items'][$i][$k]) === 'yes') {
                    $positives[] = $v;
                }
                else {
                    $negatives[] = $v;
                }
            }
        }
        ?>
        @if(count($positives))
            <div class=""><b>Positive for</b>: {!!  implode(', ', $positives) !!}</div>
        @endif
        @if(count($negatives))
            <div class=""><b>Negative for</b>: {!!  implode(', ', $negatives) !!}</div>
        @endif
    </div>
    <?php if(isset($contentData['items'][$i]['comments']) && !empty($contentData['items'][$i]['comments'])): ?>
    <div class="ml-3 pt-1 client-rs-contents">
        <b>Comments: </b><?= $contentData['items'][$i]['comments'] ?>
    </div>
    <?php endif; ?>
</div>
<?php
    }
}
else if(@$contentData['unknown']) {
    ?>
    <div class="text-secondary">Family history is unknown/unavailable</div>
    <?php
}
else {
?>
<div class="text-secondary">-</div>
<?php
}
?>
