@extends ('layouts.patient')

@section('inner-content')
<?php
$shortCutsObject = [];
foreach ($pro->allShortcuts() as $shortcut) {

    // %replaceables%
    $shortcut->text = str_replace("%AGE%", $patient->age_in_years, $shortcut->text);
    $shortcut->text = str_replace("%GENDER%", $patient->sex, $shortcut->text);
    $shortcut->text = str_replace("%NAME%", $patient->displayName(), $shortcut->text);

    $shortCutsObject[] = [
        "name" => $shortcut->shortcut,
        "value" => $shortcut->text
    ];
}
?>
<script>
    window.userShortcuts = <?= json_encode($shortCutsObject); ?>;
</script>
<link href="/select2/select2.min.css" rel="stylesheet" />
<script src="/select2/select2.min.js"></script>
<div>

    {{-- CARE PLAN START --}}
    <?php $infoLines = json_decode($patient->info_lines); ?>
    <?php $infoLines = !$infoLines ? [] : $infoLines; ?>
    <?php $vitalLabels = ['Ht. (in.)', 'Wt. (lbs.)', 'Temp. (F)', 'Pulse', 'Resp.', 'Pulse Ox.', 'SBP', 'DBP', 'Smoking Status', 'BMI']; ?>
    <?php $isOldClient = (date_diff(date_create(config('app.point_impl_date')), date_create($patient->created_at))->invert === 1); ?>

    <?php $nonCoreVisitNotes = $patient->nonCoreVisitNotes; ?>
    <?php $disallowPointEdits = $nonCoreVisitNotes && count($nonCoreVisitNotes); ?>

    <div class="row client-single-dashboard">
        <div class="col-6">
            <?php
            $myCurrentClientReviewRequest = $performer->pro->currentMonthClientReviewRequest($patient->id);
            ?>
            @if($myCurrentClientReviewRequest)
            <div class="pt-2 mt-2">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Client Review Request - {{ friendly_date(date('Y-m-01')) }}</h6>
                </div>
                @if($myCurrentClientReviewRequest->status === 'PENDING')
                <div class="alert alert-danger">
                    <div class="d-flex align-items-center justify-content-between">
                        <p class="mb-0">Please review this chart</p>
                        <div>
                            @include('app.patient.review-requests.partials.physician-update-status', ['record' =>$myCurrentClientReviewRequest ])
                        </div>
                    </div>
                </div>
                @endif
                @if($myCurrentClientReviewRequest->status === 'DONE')
                <div class="alert alert-success">
                    <div class="d-flex align-items-center justify-content-between">
                        <div>
                            <p class="mb-1">You have already reviewed this chart.</p>
                            @if($myCurrentClientReviewRequest->status_memo)
                            <div class="d-flex align-items-start">
                                <span class="mr-3"><i class="fas fa-calendar"></i> {{ friendly_date_time($myCurrentClientReviewRequest->status_updated_at) }}</span>
                                <small class="text-muted">{{ $myCurrentClientReviewRequest->status_memo }}
                                    @if(!$myCurrentClientReviewRequest->generic_bill_id)
                                        @include('app.patient.review-requests.partials.physician-update-status', ['record' =>$myCurrentClientReviewRequest, 'updateMemo' => true ])
                                    @endif
                                </small>
                            </div>

                            @endif
                        </div>
                        <div>
                            @if($myCurrentClientReviewRequest->generic_bill_id)
                            <button class="btn btn-sm btn-success"><i class="fas fa-check-circle"></i> Bill Submitted</button>
                            @else
                                @include('app.patient.review-requests.partials.physician-submit-client-review-bill', ['record' =>$myCurrentClientReviewRequest ])
                            @endif
                        </div>
                    </div>
                </div>
                @endif
            </div>
            @endif
            {{-- rpm summary for current caremonth --}}
            @if($patient->currentCareMonth() && $patient->currentCareMonth()->is_client_enrolled_in_rm)
                @include('app/patient/partials/caremonth-summary', ['careMonth' => $patient->currentCareMonth()])
            @endif

            {{-- appointments --}}
            @include('app/patient/partials/appointments')

            <?php $hasAllergiesInCanvas = $patient->hasDataInCanvas('allergies'); ?>

            {{-- canvas based allergies --}}
            @if($isOldClient && $hasAllergiesInCanvas)
            <div class="pt-2 mt-2 border-top">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Allergies</h6>
                    <div class="px-2 font-weight-bold alert alert-warning text-sm my-0 ml-2 py-1">Deprecated</div>
                </div>
                <div class="bg-light border p-2 mb-3">
                    @include('app.patient.canvas-sections.allergies.summary')
                </div>
            </div>
            @endif

            <!-- allergies - point -->
            @include('app.patient.point-based-partials.allergies')

            <?php $hasRxInCanvas = $patient->hasDataInCanvas('rx'); ?>

            {{-- canvas based rx --}}
            @if($isOldClient && $hasRxInCanvas)
            <div class="pt-2 mt-2 border-top">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Current Medications</h6>
                    <div class="px-2 font-weight-bold alert alert-warning text-sm my-0 ml-2 py-1">Deprecated</div>
                </div>
                <div class="bg-light border p-2 mb-3">
                    @include('app.patient.canvas-sections.rx.summary')
                </div>
            </div>
            @endif

            <!-- meds - point -->
            @include('app.patient.point-based-partials.rx')

            {{-- devices --}}
            <?php
            $availableDevices = count($devices);
            $patientDeviceIDs = \App\Models\ClientBDTDevice::select('id')->where('client_id', $patient->id)->where('is_active', true)->get()->toArray();
            $patientDeviceIDs = array_map(function ($_x) {
                return $_x["id"];
            }, $patientDeviceIDs);
            ?>
            <div class="mt-2 pb-1" id="patient-dashboard-devices">
                <div class="d-flex align-items-center mb-2 py-2 border-top border-bottom">
                    <h6 class="my-0 font-weight-bold">Devices</h6>
                    @if($availableDevices > 0 && $pro->pro_type === 'ADMIN')
                    <span class="mx-2 text-secondary">|</span>
                    <span moe>
                        <a start show>Add</a>
                        <form url="/api/clientBdtDevice/create" class="mcp-theme-1">
                            <input type="hidden" name="clientUid" value="{{$patient->uid}}">
                            <div class="mb-2">
                                <label class="text-secondary text-sm">Device</label>
                                <select name="deviceUid" class="form-control form-control-sm">
                                    <option value=""> --select-- </option>
                                    @foreach($devices as $device)
                                    @if(!in_array($device->id, $patientDeviceIDs))
                                    <option value="{{$device->uid}}">
                                        {{$device->imei}} ({{$device->category}})
                                    </option>
                                    @endif
                                    @endforeach
                                </select>
                            </div>
                            <div class="mb-2">
                                <label class="text-secondary text-sm">Instructions</label>
                                <input type="text" name="instructions" class="form-control form-control-sm">
                            </div>
                            <div class="mb-2">
                                <label class="text-secondary text-sm">Internal Memo</label>
                                <input type="text" name="internalMemo" class="form-control form-control-sm">
                            </div>
                            <div class="mb-2">
                                <label class="text-secondary text-sm">Status</label>
                                <input type="text" name="status" class="form-control form-control-sm">
                            </div>
                            <div class="mb-2">
                                <label class="text-secondary text-sm">Status Memo</label>
                                <input type="text" name="statusMemo" class="form-control form-control-sm">
                            </div>
                            <div>
                                <button submit class="btn btn-sm btn-primary mr-1">Submit</button>
                                <button cancel class="btn btn-sm btn-default border">Cancel</button>
                            </div>
                        </form>
                    </span>
                    @endif
                </div>
                <table class="table table-sm border-0 my-0 mx-2">
                    <tbody>
                        @foreach($patient->devices as $device)
                        <tr>
                            <td class="text-black p-0 border-0">
                                <div class="pb-0">
                                    <span relative moe>
                                        <a class="on-hover-opaque" start show title="Update">
                                            <i class="font-size-11 fa fa-edit text-primary"></i>
                                        </a>
                                        <form url="/api/clientBdtDevice/update">
                                            <input type="hidden" name="uid" value="{{ $device->uid }}">
                                            <div class="mb-2">
                                                <label class="text-secondary text-sm">Instructions</label>
                                                <input type="text" name="instructions" value="{{$device->instructions}}" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-secondary text-sm">Internal Memo</label>
                                                <input type="text" name="internalMemo" value="{{$device->internal_memo}}" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-secondary text-sm">Status</label>
                                                <input type="text" name="status" value="{{$device->status}}" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-secondary text-sm">Status Memo</label>
                                                <input type="text" name="statusMemo" value="{{$device->status_memo}}" class="form-control form-control-sm">
                                            </div>
                                            <div>
                                                <button submit class="btn btn-sm btn-primary mr-1">Submit</button>
                                                <button cancel class="btn btn-sm btn-default border">Cancel</button>
                                            </div>
                                        </form>
                                    </span>
                                    <b>{{$device->device->category}}</b>&nbsp;/&nbsp;IMEI: {{$device->device->imei}}
                                </div>
                            </td>
                        </tr>
                        @endforeach
                        @if(!$patient->devices || count($patient->devices) === 0)
                        <tr>
                            <td class="text-secondary p-0 border-0">
                                No devices
                            </td>
                        </tr>
                        @endif
                    </tbody>
                </table>
            </div>

            {{-- device measurements --}}
            {{-- @include('app/patient/partials/device-measurements') --}}

            {{-- measurements --}}
            @include('app/patient/partials/measurements')

            {{-- vitals --}}
            {{--@include('app/patient/partials/vitals')--}}

            <?php $hasVitalsInCanvas = $patient->hasDataInCanvas('vitals'); ?>

            {{-- canvas based vitals --}}
            @if($isOldClient && $hasVitalsInCanvas)
            <div class="pt-2 border-top">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Vitals</h6>
                    <div class="px-2 font-weight-bold alert alert-warning text-sm my-0 ml-2 py-1">Deprecated</div>
                </div>
                <div class="bg-light border p-2 mb-3">
                    @include('app.patient.canvas-sections.vitals.summary')
                </div>
            </div>
            @endif

            <!-- vitals - point -->
            <?php $latestVitals = \App\Models\Point::where('client_id', $patient->id)->where('category', 'VITALS')->orderBy('id', 'DESC')->first(); ?>
            <div class="pt-2 mt-2">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Vitals
                        @if(!!$latestVitals && $latestVitals->note && $latestVitals->note->effective_dateest)
                        <span class="text-secondary font-weight-normal pl-1">(as on
                            <a href="{{route('patients.view.notes.view.dashboard', ['patient' => $patient, 'note' => $latestVitals->note])}}">{{friendlier_date($latestVitals->note->effective_dateest)}}</a>)
                        </span>
                        @endif
                    </h6>
                    @if($isOldClient && $hasVitalsInCanvas)
                    <div class="px-2 font-weight-bold alert alert-info text-sm my-0 ml-2 py-1">New</div>
                    @endif
                </div>
                <div class="bg-light border p-2 mb-3">
                    @if(!!$latestVitals)
                    @include('app.patient.partials.latest-vitals', ['patient' => $patient, 'point' => $latestVitals])
                    @else
                    <div class="text-secondary">Nothing here yet</div>
                    @endif
                </div>
            </div>

        </div>
        <div class="col-6">

            <?php $hasDxInCanvas = $patient->hasDataInCanvas('dx'); ?>

            {{-- canvas based dx --}}
            @if($isOldClient)
            <div class="pt-2 mb-2 border-top">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Current Problems / Focus Areas</h6>
                    <div class="px-2 font-weight-bold alert alert-warning text-sm my-0 ml-2 py-1">Deprecated</div>
                </div>
                <div class="bg-light border p-2 mb-3">
                    @include('app.patient.canvas-sections.dx.summary')
                </div>
            </div>
            @endif

            <!-- probs - point -->
            @include('app.patient.point-based-partials.dx')

            <?php $hasCareTeamInCanvas = $patient->hasDataInCanvas('care-team'); ?>

            {{-- canvas based careteam --}}
            @if($isOldClient && $hasCareTeamInCanvas)
            <div class="pt-2 mt-2 border-top">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Care Team</h6>
                    <div class="px-2 font-weight-bold alert alert-warning text-sm my-0 ml-2 py-1">Deprecated</div>
                </div>
                <div class="bg-light border p-2 mb-3">
                    @include('app.patient.canvas-sections.care-team.summary')
                </div>
            </div>
            @endif

            <!-- careteam - point -->
            @include('app.patient.point-based-partials.care-team')

            <?php $hasPmhxInCanvas = $patient->hasDataInCanvas('pmhx'); ?>

            {{-- history_medical --}}
            @if($isOldClient && $hasPmhxInCanvas)
            <div class="pt-2 mt-2 border-top">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Medical History</h6>
                    <div class="px-2 font-weight-bold alert alert-warning text-sm my-0 ml-2 py-1">Deprecated</div>
                </div>
                <div class="bg-light border p-2 mb-3">
                    @include('app.patient.canvas-sections.pmhx.summary')
                </div>
            </div>
            @endif

            <!-- pmhx - point -->
            @include('app.patient.point-based-partials.pmhx')

            <?php $hasPshxInCanvas = $patient->hasDataInCanvas('pshx'); ?>

            {{-- history_surgical --}}
            @if($isOldClient && $hasPshxInCanvas)
            <div class="mt-2 border-top pt-2">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Surgical History</h6>
                    <div class="px-2 font-weight-bold alert alert-warning text-sm my-0 ml-2 py-1">Deprecated</div>
                </div>
                <div class="bg-light border p-2 mb-3">
                    @include('app.patient.canvas-sections.pshx.summary')
                </div>
            </div>
            @endif

            <!-- pshx - point -->
            @include('app.patient.point-based-partials.shx')

            <?php $hasFhxInCanvas = $patient->hasDataInCanvas('fhx'); ?>

            {{-- history_family --}}
            @if($isOldClient && $hasFhxInCanvas)
            <div class="mt-2 border-top pt-2">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Family History</h6>
                    <div class="px-2 font-weight-bold alert alert-warning text-sm my-0 ml-2 py-1">Deprecated</div>
                </div>
                <div class="bg-light border p-2 mb-3">
                    @include('app.patient.canvas-sections.fhx.summary')
                </div>
            </div>
            @endif

            <!-- fhx - point -->
            @include('app.patient.point-based-partials.fhx')

            <?php $hasSochxInCanvas = $patient->hasDataInCanvas('sochx'); ?>

            {{-- history_social --}}
            @if($isOldClient && $hasSochxInCanvas)
            <div class="mt-2 border-top pt-2">
                <div class="d-flex align-items-center pb-2">
                    <h6 class="my-0 font-weight-bold">Social History</h6>
                    <div class="px-2 font-weight-bold alert alert-warning text-sm my-0 ml-2 py-1">Deprecated</div>
                </div>
                <div class="bg-light border p-2 mb-3">
                    @include('app.patient.canvas-sections.sochx.summary')
                </div>
            </div>
            @endif

            <!-- sochx - point -->
            @include('app.patient.point-based-partials.sochx')


        </div>
    </div>

    <div class="row my-3">
        <div class="col-md-12">
            {{-- memos --}}
            <div class="mb-2 pt-3 pb-2 border-top">
                <div class="">
                    <div class="d-flex align-items-start pb-2">
                        <h6 class="my-0 font-weight-bold">Client Memos</h6>
                    </div>
                    <div moe class="w-100 p-2 border mb-3">
                        <form url="/api/clientMemo/create" show>
                            <input type="hidden" name="clientUid" value="{{ $patient->uid }}">
                            <label class="text-secondary font-weight-bold">+ New Memo</label>
                            <div class="mb-2">
                                <select class="form-control form-control-sm" name="category" required>
                                    <option value="">-- select --</option>
                                    <option value="Incoming Call">Incoming Call</option>
                                    <option value="Outgoing Call">Outgoing Call</option>
                                    <option value="Call Unspecified">Call Unspecified</option>
                                    <option value="Other">Other</option>
                                </select>
                            </div>
                            <div class="mb-2">
                                <textarea class="form-control form-control-sm" rows="5" name="content" placeholder="Content"></textarea>
                            </div>
                            @if($performer->pro->pro_type === 'ADMIN')
                            <div class="mb-2">
                                <label for="" class="checkbox">
                                    <input type="checkbox" name="isAdminOnly">
                                    Admins only
                                </label>
                            </div>
                            @endif
                            <div class="d-flex align-items-center">
                                <button class="btn btn-sm btn-primary mr-2" type="button" submit>Save</button>
                            </div>
                        </form>
                    </div>
                    <table class="table table-striped table-sm table-bordered mb-0">
                        @if($patient->memos && count($patient->memos))
                        <thead class="bg-light">
                            <tr class="text-nowrap">
                                <th class="border-0 text-secondary">Category</th>
                                <th class="border-0 text-secondary w-50">Summary</th>
                                <th class="border-0 text-secondary">Created</th>
                                @if($performer->pro->pro_type === 'ADMIN')
                                <th class="border-0 text-secondary">Admins Only</th>
                                @endif
                                <th class="border-0 text-secondary">Ack. by MCP</th>
                                <th class="border-0 text-secondary delete-column">&nbsp;</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($patient->memos as $memo)
                            @if(!$memo->is_admin_only || ($memo->is_admin_only && $performer->pro->pro_type === 'ADMIN') )
                            <tr>
                                <td class="text-nowrap">{{ $memo->category }}</td>
                                <td class="px-2">
                                    <pre class="m-0 break-spaces">{{ $memo->content }}</pre>
                                </td>
                                <td class="text-nowrap">
                                    <strong>{{$memo->createdBy->pro->name_first}} {{$memo->createdBy->pro->name_last}}</strong><br />
                                    {{ friendly_date_time($memo->created_at) }}
                                </td>
                                @if($performer->pro->pro_type === 'ADMIN' )
                                <td class="text-secondary">
                                    @if($memo->is_admin_only)
                                    <span class="text-success"><i class="fas fa-lock"></i> YES</span>
                                    @else
                                    <span><i class="fas fa-unlock"></i> NO</span>
                                    @endif
                                </td>
                                @endif
                                <td>
                                    @if($memo->mcp_stamp_id)
                                    <span class="text-success">YES <i class="fas fa-info-circle c-pointer" data-toggle="tooltip" data-placement="bottom" title="{{ friendly_date_time($memo->stamp->created_at) }}"></i></span>
                                    @else
                                    <span class="text-secondary">NO</span>
                                    @endif
                                </td>
                                <td class="text-center delete-column">
                                    @if($performer->pro->pro_type == 'ADMIN')
                                    <div moe relative bottom class="mr-2">
                                        <a start show><i class="fa fa-history"></i></a>
                                        <div action="" url right bottom>
                                            <table class="table table-condensed table-striped table-sm">
                                                <thead>
                                                    <th>Category</th>
                                                    <th>Summary</th>
                                                    <th>Created</th>

                                                </thead>
                                                <tbody>
                                                    @foreach($memo->updates as $update)
                                                    <tr>
                                                        <td>{{$update->category}}</td>
                                                        <td>{{$update->content}}</td>
                                                        <td><strong>{{$update->createdBy->proname_first}} {{$update->createdBy->pro->name_last}}</strong><br />{{ friendly_date_time($update->created_at) }}</td>

                                                    </tr>
                                                    @endforeach
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                    @endif
                                    <div moe wide relative class="mr-2">
                                        <a class="on-hover-opaque" start show title="Edit">
                                            <i class="font-size-11 fa fa-edit"></i>
                                        </a>
                                        <form url="/api/clientMemo/update" right>
                                            <input type="hidden" name="uid" value="{{ $memo->uid }}">
                                            <div class="mb-2">
                                                <select class="form-control form-control-sm" name="category" required>
                                                    <option value="">-- select --</option>
                                                    <option {{ $memo->category === "Incoming Call" ? "selected" : "" }} value="Incoming Call">Incoming Call</option>
                                                    <option {{ $memo->category === "Outgoing Call" ? "selected" : "" }} value="Outgoing Call">Outgoing Call</option>
                                                    <option {{ $memo->category === "Call Unspecified" ? "selected" : "" }} value="Call Unspecified">Call Unspecified</option>
                                                    <option {{ $memo->category === "Other" ? "selected" : "" }} value="Other">Other</option>
                                                </select>
                                            </div>
                                            <div class="mb-2">
                                                <textarea class="form-control form-control-sm" name="content" rows="5" placeholder="Content"><?= $memo->content ?></textarea>
                                            </div>
                                            <div class="d-flex align-items-center">
                                                <button class="btn btn-sm btn-primary mr-2" type="button" submit>Save</button>
                                                <button class="btn btn-sm btn-default mr-2 border" type="button" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                    <div moe relative>
                                        <a start show class="on-hover-opaque"><i class="fa fa-trash-alt text-danger"></i></a>
                                        <form url="/api/clientMemo/cancel" right>
                                            <input type="hidden" name="uid" value="{{ $memo->uid }}">
                                            <p class="small">Are you sure you want to cancel this memo?</p>
                                            <div class="d-flex align-items-center">
                                                <button class="btn btn-sm btn-danger mr-2" submit>Delete</button>
                                                <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                            @endif
                            @endforeach
                        </tbody>
                        @else
                        <tbody>
                            <tr>
                                <td class="text-secondary p-3">No memos have been created for this patient</td>
                            </tr>
                        </tbody>
                        @endif
                    </table>
                </div>
            </div>
        </div>
    </div>

</div>
<script>
    (function() {
        function init() {
            $('select[name="deviceUid"]').select2({
                width: '100%'
            });

            // refresh once ticket popup is closed
            $('body').off('stag-popup-closed')
            /*$('body').on('stag-popup-closed', function() {
                if($('#client-rx-container').length) {
                    fastReload();
                }
            });*/
            // ticket-popup
            $(document)
                .off('click', '.ticket-popup-trigger')
                .on('click', '.ticket-popup-trigger', function() {
                    showMask();
                    window.noMc = true;
                    $.get(this.href, (_data) => {
                        $('.ticket-popup').html(_data);
                        showStagPopup('ticket-popup', true);
                        // $('.ticket-popup .stag-popup.stag-slide').attr('close-all-with-self', 1);
                        runMCInitializer('patient-tickets'); // run specific mc initer
                        hideMask();
                    });
                    return false;
                });

            addMCHook('onHxPopupClosure', function() {
                closeStagPopup();
            });

        }
        addMCInitializer('patient-dashboard-devices', init, '#patient-dashboard-devices');

    }).call(window);
</script>
@endsection