<?php

use App\Models\Page;

$page = Page::where('client_id', $patient->id)->where('category', 'CANVAS')->where('key', 'lifestyle_rx_review')->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);
            $contentData = $canvasData["lifestyle_rx_review"];
        }    
    }

    
    if(isset($contentData) && isset($contentData[$hcpPro->id])) {
        $contentData = $contentData[$hcpPro->id];
    }else { // default data structure
        $contentData = [
            'physical_activity' => [
                'type' => '',
                'frequency' => '',
                'time' => '',
                'intensity' => '',
                'enjoyment' => '',
            ],
            'barriers' => '',   // free text
            'assessment' => '', // Inactive, Insufficiently Active, Active, Highly Active
            'adherence' => [
                'seated' => [
                    'active' => false,
                    'memo' => '',
                    'adherence' => '',
                    'adherence_memo' => '',
                ],
                'arm' => [
                    'active' => false,
                    'memo' => '',
                    'adherence' => '',
                    'adherence_memo' => '',
                ],
                'aquatic' => [
                    'active' => false,
                    'memo' => '',
                    'adherence' => '',
                    'adherence_memo' => '',
                ],
                'aerobic' => [
                    'active' => false,
                    'walk' => [
                        'active' => false,
                        'days_per_week' => '',
                        'minutes_per_day' => '',
                        'intensity' => '', // Light, Moderate, Vigorous
                        'memo' => '',
                        'adherence' => '',
                        'adherence_memo' => '',
                    ],
                    'jog' => [
                        'active' => false,
                        'days_per_week' => '',
                        'minutes_per_day' => '',
                        'intensity' => '', // Light, Moderate, Vigorous
                        'memo' => '',
                        'adherence' => '',
                        'adherence_memo' => '',
                    ],
                    'elliptical' => [
                        'active' => false,
                        'days_per_week' => '',
                        'minutes_per_day' => '',
                        'intensity' => '', // Light, Moderate, Vigorous
                        'memo' => '',
                        'adherence' => '',
                        'adherence_memo' => '',
                    ],
                    'swim' => [
                        'active' => false,
                        'days_per_week' => '',
                        'minutes_per_day' => '',
                        'intensity' => '', // Light, Moderate, Vigorous
                        'memo' => '',
                        'adherence' => '',
                        'adherence_memo' => '',
                    ],
                    'bike' => [
                        'active' => false,
                        'days_per_week' => '',
                        'minutes_per_day' => '',
                        'intensity' => '', // Light, Moderate, Vigorous
                        'memo' => '',
                        'adherence' => '',
                        'adherence_memo' => '',
                    ],
                ],
                'strength' => [
                    'active' => false,
                    'exercises' => [
                        [
                            'name' => '',
                            'days_per_week' => '',
                            'reps' => '',
                            'intensity' => '', // Light, Moderate, Vigorous
                            'memo' => '',
                            'adherence' => '',
                            'adherence_memo' => '',
                        ]
                    ]
                ],
                'neat' => [
                    'active' => false,
                    'recommendations' => [
                        'park_farther' => false,
                        'park_farther_adherence' => '',
                        'park_farther_adherence_memo' => '',
                        'steps_instead_of_elevator' => false,
                        'steps_instead_of_elevator_adherence' => '',
                        'steps_instead_of_elevator_adherence_memo' => '',
                        'stand_instead_of_sit' => false,
                        'stand_instead_of_sit_adherence' => '',
                        'stand_instead_of_sit_adherence_memo' => '',
                        'steps_goal_per_day' => '',
                        'steps_goal_per_day_adherence' => '',
                        'steps_goal_per_day_adherence_memo' => '',
                        'other' => '',
                        'other_adherence' => '',
                        'other_adherence_memo' => '',
                    ]
                ]
            ]
        ];
    }
}

if($contentData === false) {
    ?>
    <div class="text-secondary">No data in this section yet</div>
    <?php
}
else {
    ?>
    <p class="font-weight-bold text-secondary my-2">Current Physical Activity</p>
    <p class="my-2 text-dark">
        <b><?= $contentData['physical_activity']['type'] ?></b>
        <?php if($contentData['physical_activity']['frequency']): ?>
            <span class="text-secondary">&nbsp;/&nbsp;<?= @$contentData['physical_activity']['frequency'] ?></span>
        <?php endif; ?>
        <?php if($contentData['physical_activity']['time']): ?>
            <span class="text-secondary">&nbsp;/&nbsp;<?= @$contentData['physical_activity']['time'] ?></span>
        <?php endif; ?>
        <?php if($contentData['physical_activity']['intensity']): ?>
            <span class="text-secondary">&nbsp;/&nbsp;<?= @$contentData['physical_activity']['intensity'] ?></span>
        <?php endif; ?>
        <?php if($contentData['physical_activity']['enjoyment']): ?>
            <span class="text-secondary">&nbsp;/&nbsp;<?= @$contentData['physical_activity']['enjoyment'] ?></span>
        <?php endif; ?>
    </p>
    <p class="font-weight-bold text-secondary my-2">Barriers To Physical Activity: <span class="text-dark"><?= @$contentData['physical_activity']['barriers'] ?></span></p>
    <p class="font-weight-bold text-secondary my-2">Assessment of Physical Activity Level: <span class="text-dark"><?= @$contentData['physical_activity']['assessment'] ?></span></p>

    <?php if ($contentData['adherence']['seated']['active'] ||
        $contentData['adherence']['arm']['active'] ||
        $contentData['adherence']['aquatic']['active'] ||
        $contentData['adherence']['aerobic']['active'] ||
        $contentData['adherence']['strength']['active'] ||
        $contentData['adherence']['neat']['active']): ?>

        <p class="font-weight-bold text-secondary my-2">Lifestyle Rx Adherence</p>

        <div class="pl-3 border-left mb-3">
            <table class="table table-sm table-bordered mb-2 table-edit-sheet bg-white">
                <thead>
                <tr class="bg-light">
                    <th class="px-2 text-secondary border-bottom-0 width-100px text-nowrap">Lifestyle Rx</th>
                    <th class="px-2 text-secondary border-bottom-0 width-100px text-nowrap">Rx Memo</th>
                    <th class="px-2 text-secondary border-bottom-0 width-100px text-nowrap">Adherence</th>
                    <th class="px-2 text-secondary border-bottom-0 width-100px text-nowrap">Adherence Memo</th>
                </tr>
                </thead>

                <?php if ($contentData['adherence']['seated']['active'] || $contentData['adherence']['arm']['active'] || $contentData['adherence']['aquatic']['active']): ?>
                    <tbody class="border-0">
                    <?php if ($contentData['adherence']['seated']['active']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white">Seated Exercises</td>
                            <td class="px-2 align-middle py-1 bg-white"><?= $contentData['adherence']['seated']['memo'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= $contentData['adherence']['seated']['adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= $contentData['adherence']['seated']['adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if ($contentData['adherence']['arm']['active']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white">Arm Exercises</td>
                            <td class="px-2 align-middle py-1 bg-white"><?= $contentData['adherence']['arm']['memo'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= $contentData['adherence']['arm']['adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= $contentData['adherence']['arm']['adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if ($contentData['adherence']['aquatic']['active']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white">Aquatic Exercises</td>
                            <td class="px-2 align-middle py-1 bg-white"><?= $contentData['adherence']['aquatic']['memo'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= $contentData['adherence']['aquatic']['adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= $contentData['adherence']['aquatic']['adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                <?php endif; ?>

                <?php if ($contentData['adherence']['aerobic']['active']): ?>
                    <tbody class="border-0">
                    <?php if ($contentData['adherence']['aerobic']['walk']['active']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white">
                                Aerobic → Walk
                                <?= @$contentData['adherence']['aerobic']['walk']['intensity'] ? ' / ' . $contentData['adherence']['aerobic']['walk']['intensity'] : '' ?>
                                <?= @$contentData['adherence']['aerobic']['walk']['days_per_week'] ? ' / ' . $contentData['adherence']['aerobic']['walk']['days_per_week'] . ' days/wk' : '' ?>
                                <?= @$contentData['adherence']['aerobic']['walk']['minutes_per_day'] ? ' / ' . $contentData['adherence']['aerobic']['walk']['minutes_per_day'] . ' mins/day' : '' ?>
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['walk']['memo'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['walk']['adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['walk']['adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if ($contentData['adherence']['aerobic']['jog']['active']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white">
                                Aerobic → Jog
                                <?= @$contentData['adherence']['aerobic']['jog']['intensity'] ? ' / ' . $contentData['adherence']['aerobic']['jog']['intensity'] : '' ?>
                                <?= @$contentData['adherence']['aerobic']['jog']['days_per_week'] ? ' / ' . $contentData['adherence']['aerobic']['jog']['days_per_week'] . ' days/wk' : '' ?>
                                <?= @$contentData['adherence']['aerobic']['jog']['minutes_per_day'] ? ' / ' . $contentData['adherence']['aerobic']['jog']['minutes_per_day'] . ' mins/day' : '' ?>
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['jog']['memo'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['jog']['adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['jog']['adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if ($contentData['adherence']['aerobic']['elliptical']['active']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white">
                                Aerobic → Elliptical
                                <?= @$contentData['adherence']['aerobic']['elliptical']['intensity'] ? ' / ' . $contentData['adherence']['aerobic']['elliptical']['intensity'] : '' ?>
                                <?= @$contentData['adherence']['aerobic']['elliptical']['days_per_week'] ? ' / ' . $contentData['adherence']['aerobic']['elliptical']['days_per_week'] . ' days/wk' : '' ?>
                                <?= @$contentData['adherence']['aerobic']['elliptical']['minutes_per_day'] ? ' / ' . $contentData['adherence']['aerobic']['elliptical']['minutes_per_day'] . ' mins/day' : '' ?>
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['elliptical']['memo'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['elliptical']['adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['elliptical']['adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if ($contentData['adherence']['aerobic']['swim']['active']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white">
                                Aerobic → Swim
                                <?= @$contentData['adherence']['aerobic']['swim']['intensity'] ? ' / ' . $contentData['adherence']['aerobic']['swim']['intensity'] : '' ?>
                                <?= @$contentData['adherence']['aerobic']['swim']['days_per_week'] ? ' / ' . $contentData['adherence']['aerobic']['swim']['days_per_week'] . ' days/wk' : '' ?>
                                <?= @$contentData['adherence']['aerobic']['swim']['minutes_per_day'] ? ' / ' . $contentData['adherence']['aerobic']['swim']['minutes_per_day'] . ' mins/day' : '' ?>
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['swim']['memo'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['swim']['adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['swim']['adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if ($contentData['adherence']['aerobic']['bike']['active']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white">
                                Aerobic → Bike
                                <?= @$contentData['adherence']['aerobic']['bike']['intensity'] ? ' / ' . $contentData['adherence']['aerobic']['bike']['intensity'] : '' ?>
                                <?= @$contentData['adherence']['aerobic']['bike']['days_per_week'] ? ' / ' . $contentData['adherence']['aerobic']['bike']['days_per_week'] . ' days/wk' : '' ?>
                                <?= @$contentData['adherence']['aerobic']['bike']['minutes_per_day'] ? ' / ' . $contentData['adherence']['aerobic']['bike']['minutes_per_day'] . ' mins/day' : '' ?>
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['bike']['memo'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['bike']['adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['aerobic']['bike']['adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                <?php endif; ?>

                <?php if ($contentData['adherence']['strength']['active']): ?>
                    <tbody class="border-0">
                    <?php foreach ($contentData['adherence']['strength']['exercises'] as $exercise): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white">
                                Strength → <?= @$exercise['name'] ?>
                                <?= @$exercise['intensity'] ? ' / ' . $exercise['intensity'] : '' ?>
                                <?= @$exercise['reps'] ? ' / ' . $exercise['reps'] . ' reps' : '' ?>
                                <?= @$exercise['days_per_week'] ? ' / ' . $exercise['days_per_week'] . ' days/wk' : '' ?>
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$exercise['memo'] ? $exercise['memo'] : '-' ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$exercise['adherence'] ? $exercise['adherence'] : '-' ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$exercise['adherence_memo'] ? $exercise['adherence_memo'] : '-' ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                <?php endif; ?>

                <?php if (@$contentData['adherence']['neat']['active']): ?>
                    <tbody class="border-0">
                    <?php if (@$contentData['adherence']['neat']['recommendations']['park_farther']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white" colspan="2">
                                NEAT → Park farther away from entrances
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['park_farther_adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['park_farther_adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if (@$contentData['adherence']['neat']['recommendations']['steps_instead_of_elevator']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white" colspan="2">
                                NEAT → Take the stairs instead of escalator/elevator
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['steps_instead_of_elevator_adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['steps_instead_of_elevator_adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if (@$contentData['adherence']['neat']['recommendations']['stand_instead_of_sit']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white" colspan="2">
                                NEAT → Stand instead of sit
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['stand_instead_of_sit_adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['stand_instead_of_sit_adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if (@$contentData['adherence']['neat']['recommendations']['steps_goal_per_day']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white" colspan="2">
                                NEAT → Steps goal / day:
                                <b><?= @$contentData['adherence']['neat']['recommendations']['steps_goal_per_day'] ?></b>
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['steps_goal_per_day_adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['steps_goal_per_day_adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    <?php if (@$contentData['adherence']['neat']['recommendations']['other']): ?>
                        <tr>
                            <td class="px-2 align-middle py-1 bg-white" colspan="2">
                                NEAT → Other: <b><?= @$contentData['adherence']['neat']['recommendations']['other'] ?></b>
                            </td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['other_adherence'] ?></td>
                            <td class="px-2 align-middle py-1 bg-white"><?= @$contentData['adherence']['neat']['recommendations']['other_adherence_memo'] ?></td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                <?php endif; ?>

            </table>
        </div>

    <?php endif; ?>

    <?php
}
?>
