<?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, 'PSYCH_RISK_ASSESSMENT', $sessionKey, true);

$contentData = $parsed = false;

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

if($contentData && !$contentData['no_risks'] && !!$contentData['count']) {
    for ($i = 0; $i < $contentData['count']; $i++) {
?>
<div class="<?= $i > 0 ? 'mt-1' : '' ?>">
    @if(@($contentData['items'][$i]['area']))
    <div class="d-flex align-items-baseline mb-1">
        <b class="mr-3"><?= @($contentData['items'][$i]['area']) ?></b>
        <div class="mr-2"><span class="text-secondary text-sm">Level:</span> <?= @($contentData['items'][$i]['level']) ?></div>
        <div class="mr-2"><span class="text-secondary text-sm">Intent:</span> <?= @($contentData['items'][$i]['intent']) ?></div>
        <div class="mr-2"><span class="text-secondary text-sm">Plan:</span> <?= @($contentData['items'][$i]['plan']) ?></div>
        <div class="mr-2"><span class="text-secondary text-sm">Means:</span> <?= @($contentData['items'][$i]['means']) ?></div>
    </div>
    <div class="ml-3">
        <div class="">Risk Factors: <?= @($contentData['items'][$i]['risk_factors']) ?></div>
        <div class="">Protective Factors: <?= @($contentData['items'][$i]['protective_factors']) ?></div>
        <div class="">Additional Details: <?= @($contentData['items'][$i]['additional_details']) ?></div>
    </div>
    @endif
</div>
<?php
    }
}
else if(@$contentData['no_risks']) {
?>
<div class="text-secondary">Patient denies all areas of risk. No contrary clinical indications present.</div>
<?php
}
else {
?>
<div class="text-secondary">-</div>
<?php
}
?>
