@extends ('layouts.patient')

@section('inner-content')

    <?php $isOldClient = (date_diff(date_create(config('app.point_impl_date')), date_create($patient->created_at))->invert === 1); ?>

    <?php
    $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("14 days"));
    $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
    $appointments = \App\Models\Appointment::where('client_id', $patient->id)
        ->orderBy('raw_date', 'desc')->orderBy('raw_start_time', 'desc')
        ->where('raw_date', '>=', $dateLastWeek)
        ->get();
    ?>
    <div class="">
        <div class="d-flex align-items-end pb-3">
            <h4 class="font-weight-bold m-0 font-size-16">Appointments</h4>
        </div>
        <table class="table table-striped table-sm table-bordered mb-4">
            @if($appointments && count($appointments))
                <thead class="bg-light">
                <tr>
                    <th class="text-secondary border-bottom-0">Date & Time</th>
                    <th class="text-secondary border-bottom-0">Pro</th>
                    <th class="text-secondary border-bottom-0">Title</th>
                    <th class="text-secondary border-bottom-0">Description</th>
                    <th class="text-secondary border-bottom-0">Status</th>
                    <th class="text-secondary border-bottom-0">Confirmation</th>
                </tr>
                </thead>
                <tbody>
                @foreach($appointments as $appointment)
                    <tr>
                        <td>
                            <a href="/patients/view/{{$patient->uid}}/calendar/{{$appointment->uid}}" class="text-nowrap">
                                {{friendly_date($appointment->raw_date) . ' ' . friendly_time($appointment->raw_start_time)}}
                            </a>
                        </td>
                        <td>{{ $appointment->pro->displayName() }}</pre></td>
                        <td>{{ $appointment->title }}</td>
                        <td>{{ $appointment->description }}</td>
                        <td>
                            {{ $appointment->status }}
                            @if($appointment->status_memo)
                                <hr>
                                {{$appointment->status_memo}}
                            @endif
                        </td>
                        <td>
                            @if($appointment->status !== 'CANCELLED' && $appointment->status !== 'COMPLETED')
                                @include('app.patient.partials.appointment-confirmation')
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>
            @else
                <tbody>
                <tr>
                    <td class="text-secondary p-3">No recent or upcoming appointments</td>
                </tr>
                </tbody>
            @endif
        </table>
    </div>

    <div id="notes-list-{{$patient->id}}">
    <div class="pt-2 d-flex align-items-start">
        <h6 class="my-0 d-flex align-items-baseline w-100">
            <span class="font-weight-bold font-size-16">Notes</span>
            @if(true || $patient->getPrimaryCoverage()->is_covered == 'YES')
                @if($isOldClient)
                <span class="mx-2 text-secondary">|</span>
                <div moe>
                    <a start show class="py-0 mb-3">Add</a>
                    <form url="/api/note/createUsingTemplate"
                          redir="/patients/view/{{$patient->uid}}/notes/view/[data]"
                          class="mcp-theme-1">
                        <input type="hidden" name="clientUid" value="{{$patient->uid}}">
                        <input type="hidden" name="title" id="note-create-title" value="">
                        <div class="form-group mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Effective Date *</label>
                            <input name="effectiveDateEST" class="form-control" value="{{date("Y-m-d")}}">
                        </div>
                        <div class="form-group mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Pro *</label>
                            <select name="hcpProUid" class="form-control" provider-search required data-pro-uid="{{$pro->is_hcp ? $pro->uid : ''}}">
                            </select>
                        </div>
                        <div class="form-group mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Note Template *</label>
                            <select name="noteTemplateUid" class="form-control form-control-sm"
                                    onchange="$('#note-create-title').val($(this).find('option:selected').text())"
                                    required>
                                <option value=""> --select--</option>
                                @foreach($pro->noteTemplates as $noteTemplate)
                                    <?php $nT = $noteTemplate->template(); ?>
                                    <option value="{{$nT->uid}}">{{$nT->title}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Method *</label>
                            <select name="method" class="form-control form-control-sm note-method-select" required>
                                <option value="AUDIO">Audio</option>
                                <option value="VIDEO">Video</option>
                                <option value="IN_CLINIC">In-Clinic</option>
                                <option value="HOUSE_CALL">House Call</option>
                            </select>
                        </div>
                        <div class="form-group if-in-clinic">
                            <label for="" class="text-secondary text-sm mb-1">Location</label>
                            <select name="hcpCompanyLocationUid" class="form-control">
                                <option value=""></option>
                                @foreach($pro->companyLocations() as $location)
                                    <option value="{{$location->uid}}">{{$location->line1}} {{$location->city}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group m-0">
                            <button submit class="btn btn-primary btn-sm">submit</button>
                        </div>
                    </form>
                </div>
                @endif
                <span class="mx-2 text-secondary">|</span>
                <div moe id="new-appt-moe">
                    <a start show class="py-0 mb-3 text-info font-weight-bold"></a>
                    <form url="/api/appointment/create" redir="/patients/view/{{$patient->uid}}/notes?apptUid=[data]">
                        <p class="mb-2 font-weight-bold">Create Appointment</p>
                        <input type="hidden" name="clientUid" value="{{$patient->uid}}">
                        <div class="mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Pro</label>
                            <select name="proUid" class="form-control" provider-search required data-pro-uid="{{$pro->is_hcp ? $pro->uid : ''}}"></select>
                        </div>
                        <div class="mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Date</label>
                            <input type="date" name="date" required class="form-control form-control-sm" value="{{date('Y-m-d')}}">
                        </div>
                        <div class="mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Start Time</label>
                            <input type="time" name="startTime" required class="form-control form-control-sm">
                        </div>
                        <div class="mb-2">
                            <label for="" class="text-secondary text-sm mb-1">End Time</label>
                            <input type="time" name="endTime" required class="form-control form-control-sm">
                        </div>
                        <div class="mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Timezone</label>
                            <select name="timeZone" class="form-control">
                                <option value="EASTERN" selected>Eastern</option>
                                <option value="CENTRAL">Central</option>
                                <option value="MOUNTAIN">Mountain</option>
                                <option value="PACIFIC">Pacific</option>
                                <option value="ALASKA">Alaska</option>
                                <option value="HAWAII">Hawaii</option>
                                <option value="PUERTO_RICO">Puerto Rico</option>
                            </select>
                        </div>
                        <div class="mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Title</label>
                            <input type="text" name="title" required class="form-control form-control-sm" value="Visit - {{date('Y-m-d')}}">
                        </div>
                        <div class="d-flex align-items-center justify-content-center">
                            <button class="btn btn-sm btn-primary mr-2" submit>Submit</button>
                            <button class="btn btn-sm btn-default border" cancel>Cancel</button>
                        </div>
                    </form>
                </div>
                <div moe id="new-visit-moe">
                    <a start show class="py-0 mb-3 text-info font-weight-bold">Add Visit</a>
                    <form url="/api/visit/create"
                          redir="/patients/view/{{$patient->uid}}/notes/view/[data]"
                          class="mcp-theme-1">
                        <p class="mb-2 font-weight-bold">Create Visit</p>

                        @if($patient->rmCompany && !$patient->rmCompany->is_external_care_allowed)
                            <div class="alert alert-warning m-0 border-warning text-nowrap">External care is not allowed on patients<br> of
                                {{$patient->rmCompany->name}}.</div>
                        @else

                        <?php $proVisitTemplates = $pro->visitTemplates(); ?>
                        <input type="hidden" name="clientUid" value="{{$patient->uid}}">
                        @if($pro->isDefaultNA())
                        <input type="hidden" name="naProUid" value="{{$pro->uid}}">
                        @endif

                        <input type="hidden" class="form-control form-control-sm" placeholder="title"
                               name="title" id="note-create-title" value="{{count($proVisitTemplates) === 1 ? $proVisitTemplates->first()->title : 'Note'}}">

                        <div class="form-group mb-2">
                            <label for="" class="text-secondary text-sm mb-1">For Appointment *</label>
                            <select name="forAppointmentUid" class="form-control" onchange="if(this.value === 'create') {$('#new-appt-moe>a').first().click(); return false;}" required>
                                <option value="" selected>-- select --</option>
                                <option value="create">Create New</option>
                                @foreach($patient->upcomingAppointments as $appt)
                                    @if($appt->raw_date === date('Y-m-d'))
                                        <option value="{{$appt->uid}}">{{$appt->raw_date}} - {{$appt->pro->displayName()}}</option>
                                    @endif
                                @endforeach
                            </select>
                        </div>

                        <div class="form-group mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Effective Date *</label>
                            <input type="date" name="effectiveDate" class="form-control" value="{{get_current_date($pro->display_timezone)}}">
                        </div>

                        @if($pro->pro_type === 'ADMIN')
                            <div class="form-group mb-2 pt-2 d-flex">
                                <label class="my-0 mr-3 d-flex align-items-center">
                                    <input type="radio" class="mr-1" name="isNonMedical" value="0" required {{$pro->is_hcp ? 'checked' : ''}}>
                                    <span class="text-nowrap">Regular Note</span>
                                </label>
                                <label class="my-0 mr-3 d-flex align-items-center">
                                    <input type="radio" class="mr-1" name="isNonMedical" value="1" required {{$pro->is_hcp ? '' : 'checked'}}>
                                    <span class="text-nowrap">Admin Note</span>
                                </label>
                            </div>
                        @endif

                        <div class="form-group mb-2">
                            <label for="" class="text-secondary text-sm mb-1">HCP Pro *</label>
                            <select name="hcpProUid" class="form-control" provider-search required <?= $pro->pro_type !== 'ADMIN' ? 'provider-type="hcp"':'' ?> data-pro-uid="{{$pro->is_hcp ? $pro->uid : ''}}">
                            </select>
                        </div>

                        <div class="form-group mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Note Template *</label>
                            <select name="visitTemplateUid" class="form-control form-control-sm"
                                    onchange="$('#note-create-title').val($(this).find('option:selected').text())"
                                    required>
                                <option value=""> --select--</option>
                                @foreach($proVisitTemplates as $visitTemplate)
                                    <option value="{{$visitTemplate->uid}}" {{count($proVisitTemplates) === 1 ? 'selected' : ''}}>{{$visitTemplate->title}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-2">
                            <label for="" class="text-secondary text-sm mb-1">New/Follow-up</label>
                            <div class="d-flex align-items-baseline py-1">
                                <label class="mr-3 my-0 d-inline-flex align-items-center c-pointer">
                                    <input type="radio" name="newOrFuOrNa" value="NEW" required>
                                    <span class="ml-1 text-dark">New</span>
                                </label>
                                <label class="my-0 d-inline-flex align-items-center c-pointer">
                                    <input type="radio" name="newOrFuOrNa" value="FU" required>
                                    <span class="ml-1 text-dark">Follow-Up</span>
                                </label>
                            </div>
                        </div>
                        <div class="mb-2">
                            <label for="" class="text-secondary text-sm mb-1">Method *</label>
                            <select name="noteContactMethod" class="form-control form-control-sm note-method-select" required>
                                <option value="">-- select --</option>
                                <option value="AUDIO">Audio</option>
                                <option value="VIDEO">Video</option>
                                <option value="IN_CLINIC">In-Clinic</option>
                                <option value="HOUSE_CALL">House Call</option>
                            </select>
                        </div>
                        <div class="form-group if-in-clinic">
                            <label for="" class="text-secondary text-sm mb-1">Location</label>
                            <select name="hcpCompanyLocationUid" class="form-control">
                                <option value=""></option>
                                @foreach($pro->companyLocations() as $location)
                                    <option value="{{$location->uid}}">{{$location->line1}} {{$location->city}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group m-0">
                            <button submit class="btn btn-primary btn-sm">submit</button>
                        </div>

                        @endif

                    </form>
                </div>
            @else
                <span class="ml-3 text-danger">
                    <i class="fa fa-exclamation-triangle"></i>
                    <b>Cannot create note.</b> Patient is not covered or coverage is pending verification.
                </span>
                @if($pro->pro_type == 'ADMIN')
                    <a class="ml-3" href="{{route('patients.view.client-primary-coverages', ['patient' => $patient])}}">View Coverages</a>
                @endif
            @endif

            <select class="ml-auto max-width-300px form-control form-control-sm"
                    onchange="fastLoad('/patients/view/{{$patient->uid}}/notes/' + this.value, true, false, false)">
                <option value="active" {{ $filter === 'active' ? 'selected' : '' }}>Active notes</option>
                <option value="cancelled" {{ $filter === 'cancelled' ? 'selected' : '' }}>Cancelled notes</option>
                <option value="all" {{ $filter === 'all' ? 'selected' : '' }}>All notes</option>
            </select>
        </h6>
    </div>

    <table class="table table-sm table-striped table-bordered mb-0 mt-2 notes-list">
        <thead class="bg-light">
        <tr>
            <th class="border-0 text-secondary">Effective Date</th>
            <th class="border-0 text-secondary">New / Follow-up</th>
            @if($pro->pro_type == 'ADMIN')
                <th class="border-0 text-secondary">Method</th>
            @endif
            <th class="border-0 text-secondary">HCP</th>
            @if($pro->pro_type == 'ADMIN')
                <th class="border-0 text-secondary">CPT</th>
            @endif
            <th class="border-0 text-secondary">HCP Signed</th>
            @if($pro->pro_type == 'ADMIN')
            <th class="border-0 text-secondary">Claim ICDs</th>
            @endif
            @if($pro->pro_type == 'ADMIN')
                <th class="border-0 text-secondary">Is Bill Closed</th>
            @endif
            <th class="border-0 text-secondary">&nbsp;</th>
            <th class="border-0 text-secondary">Actions</th>
        </tr>
        </thead>
        <tbody>
        <?php
        $records = [];
        switch ($filter) {
            case "active":
                $records = $patient->activeNotes;
                break;
            case "cancelled":
                $records = $patient->cancelledNotes;
                break;
            case "all":
                $records = $patient->notes;
                break;
        }
        if($performer->pro->is_considered_for_supervising_physician){
            $records = $records->where('is_flagged_for_supervising_physician_review', true);
        }
        ?>
        @foreach ($records as $note)
            <tr class="{{ $note->is_cancelled ? 'cancelled-item always-clickable' : '' }}">
                <td>
                    <a href="/patients/view/{{ $patient->uid }}/notes/view/{{ $note->uid }}">
                        {{ friendly_date_time($note->effective_dateest, false) }}
                    </a>
                    @if(!!$note->visitTemplate)
                        <span class="text-info font-weight-bold">*</span>
                    @endif
                    @if($note->was_cm_setup_performed)
                        <i class="fas fa-clipboard-check text-success" title="Care plan was setup on this note."></i>
                    @endif
                    <span class="ml-1">{{ $note->is_cancelled ? '[cancelled]' : '' }}</span>
                </td>
                <td>
                    {{ $note->new_or_fu_or_na === 'NEW' ? 'New' : 'Follow-up' }}
                </td>
                @if($pro->pro_type == 'ADMIN')
                    <td>
                        {{ noteMethodDisplay($note->method) }}
                    </td>
                @endif
                <td>
                    {{$note->hcpPro ? $note->hcpPro->displayName(): '-'}}
                </td>
                @if($pro->pro_type == 'ADMIN')
                <td>
                    {{-- CPT --}}
                    <?php
                        $noteClaims = $note->claims;
                    ?>
                    @foreach ($noteClaims as $noteClaim)
                        @foreach($noteClaim->lines as $claimLine)
                            <div>
                            {{ $claimLine->cpt }}
                            @if($noteClaim->status == 'SUBMITTED')
                                <span class="ml-1" title="submitted"><i class="far fa-check-circle fa-fw text-success"></i></span>
                            @else
                            <span class="ml-1" title="Not submitted"><i class="fas fa-exclamation-triangle fa-fw text-danger"></i></span>
                            @endif
                            </div>
                        @endforeach
                    @endforeach
                </td>
                @endif
                <td>
                    {!! $note->hcpPro && $note->is_signed_by_hcp ? '<b class="text-success">Yes</b>' : 'No' !!}
                </td>
                @if($pro->pro_type == 'ADMIN')
                <td class="max-width-200px">
                    @if(count($note->reasons()))
                        @foreach ($note->reasons() as $reason)
                            <span class="d-inline-block mr-2" title="{{ $reason->description}}">{{ $reason->code }}</span>
                        @endforeach
                    @else
                        -
                    @endif
                </td>
                @endif
                @if($pro->pro_type == 'ADMIN')
                    <td>{{ $note->is_bill_closed ? 'YES':'NO' }}</td>
                @endif
                <td>
                    <div class="position-relative c-pointer text-center stag-tooltip">
                        <i class="fa fa-info-circle"></i>
                        <div class="position-absolute bg-white border rounded p-2 stag-tooltip-content">
                            <div class="text-nowrap small text-left pb-1">Created on <b>{{ friendly_date_time($note->created_at, true) }}</b></div>
                            @if($note->createdSession && $note->createdSession->pro)
                            <div class="text-nowrap small text-left">Created by <b>{{ $note->createdSession->pro->displayName() }}</b></div>
                            @endif
                        </div>
                    </div>
                </td>
                <td>
                    <div class="d-flex align-items-center">
                        <a href="/patients/view/{{ $patient->uid }}/notes/view/{{ $note->uid }}" class="font-weight-bold">Open</a>
                        @if($note->visitTemplate && $note->visitTemplate->internal_name == 'omega_soap_visit')
                            <span class="px-2 text-secondary">|</span>
                            @if(!$note->visit_template_id)
                                <a href="#" class="text-primary trigger-clone" data-uid="{{$note->uid}}">Clone</a>
                            @else
                                <div moe relative>
                                    <a start show>Clone</a>
                                    <form url="/api/visit/clone" class="mcp-theme-1" right
                                          redir="/patients/view/{{$patient->uid}}/notes/view/[data]">
                                        <input type="hidden" name="visitToCloneUid" value="{{$note->uid}}">
                                        <div class="mb-2">
                                            <label for="" class="text-sm text-secondary mb-1">Effective Date</label>
                                            <input type="date" name="effectiveDate" class="form-control input-sm" value="{{get_current_date($pro->display_timezone)}}">
                                        </div>
                                        <div class="mb-2">
                                            <label for="" class="text-secondary text-sm mb-1">New/Follow-up</label>
                                            <div class="d-flex align-items-baseline py-1">
                                                <label class="mr-3 my-0 d-inline-flex align-items-center c-pointer">
                                                    <input type="radio" name="newOrFuOrNa" value="NEW" required>
                                                    <span class="ml-1 text-dark">New</span>
                                                </label>
                                                <label class="my-0 d-inline-flex align-items-center c-pointer">
                                                    <input type="radio" name="newOrFuOrNa" value="FU" required checked>
                                                    <span class="ml-1 text-dark">Follow-Up</span>
                                                </label>
                                            </div>
                                        </div>

                                        <div class="mb-2">
                                            <label for="" class="text-sm text-secondary mb-1">Method</label>
                                            <select name="noteContactMethod" id="" class="form-control input-sm">
                                                <option value="">--select--</option>
                                                <option value="VIDEO">Video</option>
                                                <option value="AUDIO">Audio</option>
                                                <option value="IN_CLINIC">In Clinic</option>
                                                <option value="HOUSE_CALL">House Call</option>
                                            </select>
                                        </div>
                                        <div class="m-0">
                                            <button submit class="btn btn-primary btn-sm">submit</button>
                                        </div>
                                    </form>
                                </div>
                            @endif
                        @endif
                    </div>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
    </div>
    <script>
        (function() {
            function initNotesList() {

                // truncate content
                $('.note-section-content-html').each(function() {
                    if($.trim($(this).text()) === '') $(this).remove();
                    $(this).find('p, div').each(function() {
                        if($.trim($(this).text()) === '') $(this).remove();
                    });
                });
                $('.note-section-content-html-outer').each(function() {
                    let pCount = 0;
                    $(this).find('p').each(function() {
                        pCount++;
                        if(pCount > 3) {
                            $(this).hide();
                        }
                    });
                    if(pCount > 3) {
                        $('<p/>').text('…').appendTo(this);
                    }
                });

                $('.trigger-clone')
                    .off('click.clone')
                    .on('click.clone', function() {
                        $.post('/api/note/clone', {
                            uid: $(this).attr('data-uid')
                        }, function(_data) {
                            if(_data) {
                                if(_data.success) {
                                    toastr.success('Note cloned successfully');
                                    fastLoad('/patients/view/{{$patient->uid}}/notes/view/' + _data.data);
                                }
                                else {
                                    toastr.error(_data.message);
                                }
                            }
                            else {
                                toastr.error('Unable to clone the note!');
                            }
                        }, 'json');
                    });

                $('.note-method-select').change(function() {
                    let form = $(this).closest('form');
                    if(this.value === 'IN_CLINIC') {
                        form.find('.if-in-clinic').show();
                    }
                    else {
                        form.find('.if-in-clinic').hide();
                    }
                });

                @if(request('apptUid'))
                let nvMoe = $('#new-visit-moe');
                nvMoe.find('[name="forAppointmentUid"]').val('{{request('apptUid')}}');
                nvMoe.find('>a').first().click();
                @endif

            }
            addMCInitializer('notes-list', initNotesList, '#notes-list-{{$patient->id}}');
        })();
    </script>
@endsection
