<?php

use App\Models\Point;

$category = 'DISCLAIMERS';
$endPoint = 'upsertNoteSingleton';

$point = Point::where('added_in_note_id', $note->id)->where('category', $category)->orderBy('id', 'DESC')->first();
$contentData = [
    "consent_for_telehealth_visit" => false,
    "eduction_provided_for_covid" => false,
    "communicated_dietary_ls_exercise_risks" => false,
    "importance_of_calling_911" => false,
    "assessment_is_virtual" => false,
    "assessment_based_on_patient_inputs" => false,
];
if (!!@$point->data) {
    $contentData = json_decode($point->data, true);
}
?>
<div visit-moe close-on-save close-on-cancel class="d-block p-3">
    <form show url="/api/visitPoint/<?= $endPoint ?>" class="mcp-theme-1">
        <input type="hidden" name="segmentUid" value="<?= $segment->uid ?>">
        <input type="hidden" name="category" value="<?= $category ?>">
        <input type="hidden" name="data" value="{{json_encode($contentData)}}">

        <label class="d-flex align-items-baseline mb-2">
            <div class="mr-2 align-self-stretch" style="padding-top: 2px;">
                <input type="checkbox" class="chk-disclaimers-check-all" onchange="$(this).closest('form').find(':checkbox[data-name]').prop('checked', this.checked)">
            </div>
            <span class="font-weight-bold">Check ALL</span>
        </label>

        <label class="d-flex align-items-baseline mb-2">
            <div class="mr-2 align-self-stretch" style="padding-top: 2px;">
                <input type="checkbox" data-name="consent_for_telehealth_visit" {{@($contentData["consent_for_telehealth_visit"] ? 'checked' : '')}}>
            </div>
            <span>Patient consent for telehealth visit obtained.</span>
        </label>

        <label class="d-flex align-items-baseline mb-2">
            <div class="mr-2 align-self-stretch" style="padding-top: 2px;">
                <input type="checkbox" data-name="eduction_provided_for_covid" {{@($contentData["eduction_provided_for_covid"] ? 'checked' : '')}}>
            </div>
            <span>Education provided for COVID risks, steps for prevention, and eliminating exposure.</span>
        </label>

        <label class="d-flex align-items-baseline mb-2">
            <div class="mr-2 align-self-stretch" style="padding-top: 2px;">
                <input type="checkbox" data-name="communicated_dietary_ls_exercise_risks" {{@($contentData["communicated_dietary_ls_exercise_risks"] ? 'checked' : '')}}>
            </div>
            <span>Communicated possible risks involved with dietary and lifestyle changes, and in particular with exercise prescription.</span>
        </label>

        <label class="d-flex align-items-baseline mb-2">
            <div class="mr-2 align-self-stretch" style="padding-top: 2px;">
                <input type="checkbox" data-name="importance_of_calling_911" {{@($contentData["importance_of_calling_911"] ? 'checked' : '')}}>
            </div>
            <span>Patient verbalizes the importance of calling 911 if experiencing chest pain, dizziness or lightheadedness while exercising.</span>
        </label>

        <label class="d-flex align-items-baseline mb-2">
            <div class="mr-2 align-self-stretch" style="padding-top: 2px;">
                <input type="checkbox" data-name="assessment_is_virtual" {{@($contentData["assessment_is_virtual"] ? 'checked' : '')}}>
            </div>
            <span>Patient understands that this assessment is done virtually and therefore a complete physical assessment was not completed, and is not a replacement for a visit with their primary care physician.</span>
        </label>

        <label class="d-flex align-items-baseline mb-2">
            <div class="mr-2 align-self-stretch" style="padding-top: 2px;">
                <input type="checkbox" data-name="assessment_based_on_patient_inputs" {{@($contentData["assessment_based_on_patient_inputs"] ? 'checked' : '')}}>
            </div>
            <span>Patient understands this assessment was completed using the information they provided only. All questions addressed.</span>
        </label>

        @if($note->hcp_pro_id == $patient->mcp_pro_id && $patient->is_enrolled_in_cm === 'YES')
            <p class="mb-2">Patient elects to participate in Chronic Care Management.</p>
        @endif

        @if($note->hcp_pro_id == $patient->mcp_pro_id && $patient->is_enrolled_in_rm === 'YES') {{-- correct --}}
            <p class="mb-2">Patient elects to participate in Remote Patient Monitoring.</p>
        @endif

        <?php
        $supplyOrders = \App\Models\SupplyOrder::where('client_id', $note->client->id)
            ->where('is_cancelled', false)
            ->where('note_id', $note->id)
            ->get();
        ?>
        @if($supplyOrders && count($supplyOrders))
            <p class="mb-2">The following supplies were ordered during this visit:</p>
            <ol class="mb-2">
                @foreach($supplyOrders as $so)
                    <li>{{$so->product->title}}</li>
                @endforeach
            </ol>
        @endif

        <?php
        $erxs = \App\Models\Erx::where('client_id', $note->client->id)
            ->where('note_id', $note->id)
            ->orderBy('created_at', 'desc')
            ->where(function ($q) {
                $q->whereNull('pro_declared_status')
                    ->orWhere('pro_declared_status', '<>', 'CANCELLED');
            })
            ->get();
        ?>
        @if($erxs && count($erxs))
            <p class="mb-2">The following e-prescriptions were ordered during this visit:</p>
            <ol class="mb-2">
                @foreach($erxs as $erx)
                    <?php $parsed = json_decode($erx->clinical_detail_json); ?>
                    @if($erx->erx_category === 'LAB' || $erx->erx_category === 'IMAGING' || $erx->erx_category === 'DRUG')
                        <li>
                            @if($erx->erx_category === 'LAB' || $erx->erx_category === 'IMAGING')
                                <div class="d-flex align-items-baseline">
                                    <span>{{$erx->erx_category}}:</span>
                                    <div class="pl-2 flex-grow-1">
                                        @if($parsed && @($parsed->items) && count($parsed->items))
                                            @foreach($parsed->items as $t)
                                                @if(@($t->tests) && count($t->tests))
                                                    @foreach($t->tests as $tx)
                                                        <div>{{$tx->desc}}</div>
                                                    @endforeach
                                                @endif
                                            @endforeach
                                        @endif
                                    </div>
                                </div>
                            @elseif($erx->erx_category === 'DRUG')
                                <div class="d-flex align-items-baseline">
                                    <span>{{$erx->erx_category}}:</span>
                                    <div class="pl-2 flex-grow-1">
                                        @if($parsed && @($parsed->items) && count($parsed->items))
                                            @foreach($parsed->items as $t)
                                                <div>{{$t->medication}}</div>
                                            @endforeach
                                        @endif
                                    </div>
                                </div>
                            @endif
                        </li>
                    @endif
                @endforeach
            </ol>
        @endif

        @if($note->method == 'AUDIO' || $note->method == 'VIDEO')
            <p class="mb-2">The visit was conducted with the use of an interactive <b>{{$note->method}}</b> telecommunications system that permits real time communication
                between the patient and the provider. Patient consent for telehealth visit was obtained.</p>
            <?php /*
            <p class="mb-2">Originating Site: <b>{{$note->hcpCompanyPro && $note->hcpCompanyPro->company ? $note->hcpCompanyPro->company->name : '-'}}</b></p>
            <p class="mb-2">Distant Site: <b>Patient Home</b></p>
            */ ?>
        @endif

        <div class="pt-2">
            <button submit class="btn btn-sm btn-primary mr-2"><i class="fa fa-save"></i></button>
            <div class="d-inline-flex align-self-stretch align-items-center">
                <span class="autosave-indicator saving text-sm text-secondary">Saving changes &hellip;</span>
                <span class="autosave-indicator saved text-sm text-secondary">
                    <i class="fa fa-check"></i>
                    Saved
                </span>
            </div>
        </div>
    </form>
</div>

<script>
    (function() {
        window.segmentInitializers.<?= !!@$segment ? $segment->segmentTemplate->internal_name : $segmentInternalName ?> = function () {
            let parentSegment = $('[data-segment-template-name="<?= $segment->segmentTemplate->internal_name ?>"] ');
            function __checkAll() {
                parentSegment.find('.chk-disclaimers-check-all').prop('checked',
                    parentSegment.find(':checkbox[data-name]').length === parentSegment.find(':checkbox[data-name]:checked').length);
            }
            parentSegment.find(':checkbox[data-name]')
                .off('change.check-all-toggle')
                .on('change.check-all-toggle', function() {
                    __checkAll();
                });
            __checkAll();
        };
    }).call(window);
</script>