<?php


use App\Models\Page;

$page = Page::where('client_id', $patient->id)->where('category', 'CANVAS')->where('key', 'lifestyle_rx_update')->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);
    }else{
        if ($patient->canvas_data) {
            $canvasData = json_decode($patient->canvas_data, true);
            if(!isset($canvasData["lifestyle_rx_update"])) $canvasData["lifestyle_rx_update"] = [];
            $contentData = $canvasData["lifestyle_rx_update"];
        }
    }
    if(!isset($contentData[$hcpPro->id])) $contentData[$hcpPro->id] = [];
    $contentData = $contentData[$hcpPro->id];    

    if(!isset($contentData['prescription'])) $contentData['prescription'] = [];
    if(!isset($contentData['prescription']['seated'])) $contentData['prescription']['seated'] = [];
    if(!isset($contentData['prescription']['seated']['memo'])) $contentData['prescription']['seated']['memo'] = "";
    if(!isset($contentData['prescription']['seated']['active'])) $contentData['prescription']['seated']['active'] = false;

    if(!isset($contentData['prescription']['arm'])) $contentData['prescription']['arm'] = [];
    if(!isset($contentData['prescription']['arm']['active'])) $contentData['prescription']['arm']['active'] = [];
    if(!isset($contentData['prescription']['arm']['memo'])) $contentData['prescription']['arm']['memo'] = [];
    if(!isset($contentData['prescription']['aquatic'])) $contentData['prescription']['aquatic'] = [];
    if(!isset($contentData['prescription']['aquatic']['active'])) $contentData['prescription']['aquatic']['active'] = [];
    if(!isset($contentData['prescription']['aquatic']['memo'])) $contentData['prescription']['aquatic']['memo'] = [];
    if(!isset($contentData['prescription']['aerobic'])) $contentData['prescription']['aerobic'] = [];
    if(!isset($contentData['prescription']['aerobic']['active'])) $contentData['prescription']['aerobic']['active'] = [];
    if(!isset($contentData['prescription']['aerobic']['walk']['active'])) $contentData['prescription']['aerobic']['walk']['active'] = [];
    if(!isset($contentData['prescription']['aerobic']['walk']['days_per_week'])) $contentData['prescription']['aerobic']['walk']['days_per_week'] = [];
    if(!isset($contentData['prescription']['aerobic']['walk']['minutes_per_day'])) $contentData['prescription']['aerobic']['walk']['minutes_per_day'] = [];
    if(!isset($contentData['prescription']['aerobic']['walk']['intensity'])) $contentData['prescription']['aerobic']['walk']['intensity'] = [];
    if(!isset($contentData['prescription']['aerobic']['walk']['memo'])) $contentData['prescription']['aerobic']['walk']['memo'] = [];
    if(!isset($contentData['prescription']['aerobic']['jog'])) $contentData['prescription']['aerobic']['jog'] = [];
    if(!isset($contentData['prescription']['aerobic']['jog']['active'])) $contentData['prescription']['aerobic']['jog']['active'] = [];
    if(!isset($contentData['prescription']['aerobic']['jog']['days_per_week'])) $contentData['prescription']['aerobic']['jog']['days_per_week'] = [];
    if(!isset($contentData['prescription']['aerobic']['jog']['minutes_per_day'])) $contentData['prescription']['aerobic']['jog']['minutes_per_day'] = [];
    if(!isset($contentData['prescription']['aerobic']['jog']['intensity'])) $contentData['prescription']['aerobic']['jog']['intensity'] = [];
    if(!isset($contentData['prescription']['aerobic']['jog']['memo'])) $contentData['prescription']['aerobic']['jog']['memo'] = [];
    if(!isset($contentData['prescription']['aerobic']['elliptical'])) $contentData['prescription']['aerobic']['elliptical'] = [];
    if(!isset($contentData['prescription']['aerobic']['elliptical']['active'])) $contentData['prescription']['aerobic']['elliptical']['active'] = [];
    if(!isset($contentData['prescription']['aerobic']['elliptical']['days_per_week'])) $contentData['prescription']['aerobic']['elliptical']['days_per_week'] = [];
    if(!isset($contentData['prescription']['aerobic']['elliptical']['minutes_per_day'])) $contentData['prescription']['aerobic']['elliptical']['minutes_per_day'] = [];
    if(!isset($contentData['prescription']['aerobic']['elliptical']['intensity'])) $contentData['prescription']['aerobic']['elliptical']['intensity'] = [];
    if(!isset($contentData['prescription']['aerobic']['elliptical']['memo'])) $contentData['prescription']['aerobic']['elliptical']['memo'] = [];
    if(!isset($contentData['prescription']['aerobic']['swim'])) $contentData['prescription']['aerobic']['swim'] = [];
    if(!isset($contentData['prescription']['aerobic']['swim']['active'])) $contentData['prescription']['aerobic']['swim']['active'] = [];
    if(!isset($contentData['prescription']['aerobic']['swim']['days_per_week'])) $contentData['prescription']['aerobic']['swim']['days_per_week'] = [];
    if(!isset($contentData['prescription']['aerobic']['swim']['minutes_per_day'])) $contentData['prescription']['aerobic']['swim']['minutes_per_day'] = [];
    if(!isset($contentData['prescription']['aerobic']['swim']['intensity'])) $contentData['prescription']['aerobic']['swim']['intensity'] = [];
    if(!isset($contentData['prescription']['aerobic']['swim']['memo'])) $contentData['prescription']['aerobic']['swim']['memo'] = [];
    if(!isset($contentData['prescription']['aerobic']['bike'])) $contentData['prescription']['aerobic']['bike'] = [];
    if(!isset($contentData['prescription']['aerobic']['bike']['active'])) $contentData['prescription']['aerobic']['bike']['active'] = [];
    if(!isset($contentData['prescription']['aerobic']['bike']['days_per_week'])) $contentData['prescription']['aerobic']['bike']['days_per_week'] = [];
    if(!isset($contentData['prescription']['aerobic']['bike']['minutes_per_day'])) $contentData['prescription']['aerobic']['bike']['minutes_per_day'] = [];
    if(!isset($contentData['prescription']['aerobic']['bike']['intensity'])) $contentData['prescription']['aerobic']['bike']['intensity'] = [];
    if(!isset($contentData['prescription']['aerobic']['bike']['memo'])) $contentData['prescription']['aerobic']['bike']['memo'] = [];
    if(!isset($contentData['prescription']['strength'])) $contentData['prescription']['strength'] = [];
    if(!isset($contentData['prescription']['strength']['active'])) $contentData['prescription']['strength']['active'] = [];
    if(!isset($contentData['prescription']['strength']['exercises'])) $contentData['prescription']['strength']['exercises'] = [];
    if(!isset($contentData['prescription']['neat'])) $contentData['prescription']['neat'] = [];
    if(!isset($contentData['prescription']['neat']['active'])) $contentData['prescription']['neat']['active'] = [];
    if(!isset($contentData['prescription']['neat']['recommendations'])) $contentData['prescription']['neat']['recommendations'] = [];
    if(!isset($contentData['prescription']['neat']['recommendations']['park_farther'])) $contentData['prescription']['neat']['recommendations']['park_farther'] = [];
    if(!isset($contentData['prescription']['neat']['recommendations']['steps_instead_of_elevator'])) $contentData['prescription']['neat']['recommendations']['steps_instead_of_elevator'] = [];
    if(!isset($contentData['prescription']['neat']['recommendations']['stand_instead_of_sit'])) $contentData['prescription']['neat']['recommendations']['stand_instead_of_sit'] = [];
    if(!isset($contentData['prescription']['neat']['recommendations']['steps_goal_per_day'])) $contentData['prescription']['neat']['recommendations']['steps_goal_per_day'] = [];
    if(!isset($contentData['prescription']['neat']['recommendations']['other'])) $contentData['prescription']['neat']['recommendations']['other'] = [];

}

if($contentData === false) {
    ?>
    <div class="text-secondary">No data in this section yet</div>
    <?php
}
else {
    ?>

    <?php if($contentData['prescription']['seated']['active']): ?>
    <div class="mb-1 d-flex align-items-center">
        <i class="fa fa-check mr-2 text-secondary"></i>
        <span>Seated Exercises</span>
        <?php if($contentData['prescription']['seated']['memo']): ?>
            <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['seated']['memo'] ?></span>
        <?php endif; ?>
    </div>
    <?php endif; ?>

    <?php if($contentData['prescription']['arm']['active']): ?>
        <div class="mb-1 d-flex align-items-center">
            <i class="fa fa-check mr-2 text-secondary"></i>
            <span>Arm Exercises</span>
            <?php if($contentData['prescription']['arm']['memo']): ?>
                <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['arm']['memo'] ?></span>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <?php if($contentData['prescription']['aquatic']['active']): ?>
        <div class="mb-1 d-flex align-items-center">
            <i class="fa fa-check mr-2 text-secondary"></i>
            <span>Aquatic Exercises</span>
            <?php if($contentData['prescription']['aquatic']['memo']): ?>
                <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aquatic']['memo'] ?></span>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <?php if($contentData['prescription']['aerobic']['active']): ?>
        <p class="font-weight-bold my-2">Aerobic Activity</p>
        <?php if($contentData['prescription']['aerobic']['walk']['active']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Walk</span>
                <?php if($contentData['prescription']['aerobic']['walk']['days_per_week']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['walk']['days_per_week'] ?> days/wk</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['walk']['minutes_per_day']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['walk']['minutes_per_day'] ?> mins/day</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['walk']['intensity']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['walk']['intensity'] ?></span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['walk']['memo']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['walk']['memo'] ?></span>
                <?php endif; ?>
            </div>
        <?php endif; ?>
        <?php if($contentData['prescription']['aerobic']['jog']['active']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Jog</span>
                <?php if($contentData['prescription']['aerobic']['jog']['days_per_week']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['jog']['days_per_week'] ?> days/wk</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['jog']['minutes_per_day']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['jog']['minutes_per_day'] ?> mins/day</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['jog']['intensity']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['jog']['intensity'] ?></span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['jog']['memo']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['jog']['memo'] ?></span>
                <?php endif; ?>
            </div>
        <?php endif; ?>
        <?php if($contentData['prescription']['aerobic']['elliptical']['active']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Elliptical</span>
                <?php if($contentData['prescription']['aerobic']['elliptical']['days_per_week']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['elliptical']['days_per_week'] ?> days/wk</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['elliptical']['minutes_per_day']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['elliptical']['minutes_per_day'] ?> mins/day</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['elliptical']['intensity']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['elliptical']['intensity'] ?></span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['elliptical']['memo']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['elliptical']['memo'] ?></span>
                <?php endif; ?>
            </div>
        <?php endif; ?>
        <?php if($contentData['prescription']['aerobic']['swim']['active']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Swim</span>
                <?php if($contentData['prescription']['aerobic']['swim']['days_per_week']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['swim']['days_per_week'] ?> days/wk</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['swim']['minutes_per_day']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['swim']['minutes_per_day'] ?> mins/day</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['swim']['intensity']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['swim']['intensity'] ?></span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['swim']['memo']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['swim']['memo'] ?></span>
                <?php endif; ?>
            </div>
        <?php endif; ?>
        <?php if($contentData['prescription']['aerobic']['bike']['active']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Bike</span>
                <?php if($contentData['prescription']['aerobic']['bike']['days_per_week']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['bike']['days_per_week'] ?> days/wk</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['bike']['minutes_per_day']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['bike']['minutes_per_day'] ?> mins/day</span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['bike']['intensity']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['bike']['intensity'] ?></span>
                <?php endif; ?>
                <?php if($contentData['prescription']['aerobic']['bike']['memo']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $contentData['prescription']['aerobic']['bike']['memo'] ?></span>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>

    <?php if($contentData['prescription']['strength']['active']): ?>
        <p class="font-weight-bold my-2">Strength Training</p>
        <?php foreach($contentData['prescription']['strength']['exercises'] as $exercise): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span><?= $exercise['name'] ?></span>
                <?php if($exercise['days_per_week']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $exercise['days_per_week'] ?> days/wk</span>
                <?php endif; ?>
                <?php if($exercise['reps']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $exercise['reps'] ?> reps</span>
                <?php endif; ?>
                <?php if($exercise['intensity']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $exercise['intensity'] ?></span>
                <?php endif; ?>
                <?php if($exercise['memo']): ?>
                    <span class="text-secondary">&nbsp;/&nbsp;<?= $exercise['memo'] ?></span>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>

    <?php if($contentData['prescription']['neat']['active']): ?>
        <p class="font-weight-bold my-2">Non-Exercise Activity Time (NEAT)</p>
        <?php if($contentData['prescription']['neat']['recommendations']['park_farther']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Park farther away from entrances</span>
            </div>
        <?php endif; ?>
        <?php if($contentData['prescription']['neat']['recommendations']['steps_instead_of_elevator']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Take the stairs instead of escalator/elevator</span>
            </div>
        <?php endif; ?>
        <?php if($contentData['prescription']['neat']['recommendations']['stand_instead_of_sit']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Stand instead of sit</span>
            </div>
        <?php endif; ?>
        <?php if($contentData['prescription']['neat']['recommendations']['steps_goal_per_day']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Steps goal / day: <b><?= $contentData['prescription']['neat']['recommendations']['steps_goal_per_day'] ?></b></span>
            </div>
        <?php endif; ?>
        <?php if($contentData['prescription']['neat']['recommendations']['other']): ?>
            <div class="mb-1 d-flex align-items-center">
                <i class="fa fa-check mr-2 text-secondary"></i>
                <span>Other: <b><?= $contentData['prescription']['neat']['recommendations']['other'] ?></b></span>
            </div>
        <?php endif; ?>

    <?php endif; ?>
    <?php
}
?>
