<?php

use App\Models\Page;

$page = Page::where('client_id', $patient->id)->where('category', 'CANVAS')->where('key', 'cc_hpi')->first();

// HUMONGOUS FIX! This CRITICAL "if" was never there till now!
if(!@$contentData) {

    /** @var \App\Models\Pro $hcpPro */
    /** @var \App\Models\Pro $pro */
    /** @var \App\Models\Note $note */

    if(!@$hcpPro) {
        if(@$note) {
            $hcpPro = $note->hcpPro;
        }
        else {
            $hcpPro = $pro; // should never get here
        }
    }

    $contentData = false;
    if($page){
        $contentData = json_decode($page->data, true);
        if(!isset($contentData[$hcpPro->id])) {
            $contentData[$hcpPro->id] = [
                'current_cc' => []
            ];
        }
        $contentData = $contentData[$hcpPro->id];
    }else{
        if ($patient->canvas_data) {
            $canvasData = json_decode($patient->canvas_data, true);
            if(isset($canvasData["cc_hpi"]) && isset($canvasData["cc_hpi"][$hcpPro->id])) {
                $contentData = $canvasData["cc_hpi"][$hcpPro->id];
            }
        }
    }
}

// $contentData (if not null) is already pro-scoped by this point!

if($contentData === false) {
    ?>
    <div class="text-secondary">No data in this section yet</div>
    <?php
}
else {
    $lines = $contentData['current_cch']['list'];
    ?>
    <p class="font-weight-bold mb-2">CC</p>
    <p class="p-2 bg-light border mb-2">
        Patient is a <?= $patient->age_in_years ?> y.o. <?= $patient->sex ?> presenting for the following reason<?= count($lines) > 1 ? 's' : '' ?>:
        <?php foreach($lines as $i => $line): ?>
            <?= $line['name'] ?>
            (<?= $line['category'] ?><?= $line['icd'] ? ' / ' . $line['icd'] : '' ?><?= $line['memo'] ? ' / ' . $line['memo'] : '' ?>)<?= count($lines) > 1 ? ($i === count($lines) - 2 ? ' and ' : ($i < count($lines) - 2 ? ', ' : ($i === count($lines) - 1 ? '.' : ''))) : '' ?>
        <?php endforeach; ?>
    </p>
    <p class="font-weight-bold mb-2">HPI</p>
    <table class="table table-sm table-bordered mb-2 table-edit-sheet">
        <thead>
        <tr class="bg-light">
            <th class="px-2 py-1 text-secondary border-bottom-0 width-30px text-center">#</th>
            <th class="px-2 py-1 text-secondary border-bottom-0 w-25">Issue</th>
            <th class="px-2 py-1 text-secondary border-bottom-0">HPI</th>
        </tr>
        </thead>
        <tbody class="bg-white">
        <?php foreach($lines as $i => $line): ?>
            <tr>
                <td class="px-2 bg-white py-1 text-center text-sm font-weight-bold"><?= ++$i ?></td>
                <td class="px-2 bg-white py-1"><?= ucwords($line['category']) ?><?= $line['name'] ? ' <span class="text-secondary on-hover-opaque">/</span> ' . $line['name'] : '' ?><?= $line['icd'] ? ' <span class="text-secondary on-hover-opaque">/</span> ' . $line['icd'] : ''?></td>
                <td class="px-2 bg-white py-1"><?= $line['hpi']['free_text'] ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php
}
?>
