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

$contentData = $parsed = false;

$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 = $parsed = $point->lastChildReview->data;
}

$labels = [
    'PE_general' => 'General',
    'PE_General_Depression' => 'Depression',
    'PE_Eyes' => 'Eyes',
    'PE_Ears' => 'Ears',
    'PE_Nose' => 'Nose',
    'PE_Mouth_Throat' => 'Mouth/Throat',
    'PE_Neck' => 'Neck',
    'PE_Lymph' => 'Lymphatic',
    'PE_Resp' => 'Respiratory',
    'PE_Cardio' => 'Cardiovascular',
    'PE_Skin' => 'Skin',
    'PE_Extrimities' => 'Extrimities',
    'PE_Neuro' => 'Neurological',
    'PE_Psych' => 'Psychiatric',
];

if($contentData && !@$contentData['unknown']) {
    foreach ($labels as $k => $v) {
?>
@if(isset($contentData[$k]))
  @if($k == 'PE_general')
    <div class="mb-1">
      <b>{{$v}}:</b> <span><?= implode(", ", $contentData[$k]) ?></span>
    </div>
  @elseif($k == 'PE_General_Depression')
    <div class="mb-1">
      <b>{{$v}}:</b> <span>{{ $contentData[$k] }}</span>
    </div>
  @else
    <div class="mb-2">
      <div class="">
        <b>{{$v}}:</b> <span>{{$contentData[$k]}}</span>
      </div>
      <span><em>Note:</em> {{@$contentData["{$k}_$contentData[$k]"]}}</span>
    </div>
  @endif
@endif

<?php
    }
}
else if(@$contentData['unknown']) {
    ?>
    <div class="text-secondary">Physical Exam is unavailable</div>
    <?php
}
else {
?>
<div class="text-secondary">-</div>
<?php
}
?>
