<?php

use App\Models\Page;

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

$contentData = [];

if($page){
    $contentData = json_decode($page->data, true);
   
}else{
    if ($patient->canvas_data) {
        $canvasData = json_decode($patient->canvas_data, true);
        if (isset($canvasData["rx"])) {
            $contentData = $canvasData["rx"];
        }
    }
}

if(!isset($contentData['items'])) $contentData['items'] = [];

if(count($contentData['items'])) {
    for ($i = 0; $i < count($contentData['items']); $i++) {
        $item = $contentData['items'][$i];
?>
        <div class="mb-2 <?= @$item["type"] === 'removed' ? 'on-hover-opaque' : '' ?>">
            <div class="">
                <?php if(isset($item["title"]) && !empty($item["title"])): ?>
                    <b class="click-to-copy <?= isset($item["type"]) && $item["type"] === 'removed' ? 'text-secondary' : '' ?>">
                        <?= $item["title"] ?>
                    </b>
                <?php endif; ?>
                <?= !!$item["strength"] ? '/&nbsp;' . $item["strength"] : '' ?>
                <?= !!$item["frequency"] ? '/&nbsp;' . $item["frequency"] : '' ?>
                <?php if(isset($item["type"])): ?>
                    <?php if($item["type"] === 'removed'): ?>
                        <span class="pl-3 text-secondary text-sm mr-2">(CURRENTLY INACTIVE)</span>
                    <?php elseif($item["type"] === 'updated'): ?>
                        <span class="pl-3 text-warning-mellow text-sm mr-2 font-weight-bold">(* UPDATED)</span>
                    <?php elseif($item["type"] === 'prescribed today'): ?>
                        <span class="pl-3 text-success text-sm mr-2 font-weight-bold">(* PRESCRIBED TODAY)</span>
                    <?php elseif($item["type"] === 'existing'): ?>
                        <span class="pl-3 text-secondary text-sm mr-2 font-weight-bold">(EXISTING)</span>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
            <?php
            $detailPlain = isset($item["detail"]) ? $item["detail"] : '';
            $detailPlain = trim(strip_tags($detailPlain));
            if(!empty($detailPlain)):
            ?>
                <div class="text-secondary click-to-copy"><?= $detailPlain ?></div>
            <?php endif; ?>
            <?php if(isset($item["notes"]) && count($item["notes"])): ?>
                <div class="pl-3">
                <?php foreach($item["notes"] as $rxNote): ?>
                    <div>
                        <span class="text-secondary text-sm mr-2"><?= $rxNote["date"] ?></span>
                        <span class="text-secondary"><?= $rxNote["text"] ?></span>
                    </div>
                <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
<?php
    }
}
else {
    ?>
    <div class="text-secondary">Nothing here yet!</div>
    <?php
}
?>
