@extends ('layouts.template')

@section('content')

    <style>
        #dashboard-mcp tr.thin th,
        #dashboard-mcp tr.thin td,
        #dashboard-mcp .dashboard-stats-table tr td,
        #dashboard-mcp .dashboard-stats-table tr th {
            padding: 0.25em;
            font-weight: normal;
        }

        #dashboard-mcp table.appointments tr td {
            vertical-align: middle;
        }
    </style>

    <div id="dashboard-mcp">
    <div class="p-3">
        <div class="">
            <div class="row mcp-theme-1" id="pro-dashboard-container">
                <div class="col-md-3 mcp-theme-1">
                    <div class="mb-4">
                        <div class="pro-dashboard-inline-calendar"></div>
                    </div>
                    <div class="card mb-4" stag-collapsible-card="mcp-key-numbers">
                        <div class="card-header pl-2">
                            <strong>
                                Key Numbers
                            </strong>
                        </div>
                        <div class="card-body p-0">
                            <table class="table table-sm mb-0 dashboard-stats-table">
                                <tbody>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_patients_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.patients') }}">Patients</a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_new_patients_awaiting_visit_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.new_patients_awaiting_visit') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="New Patients Awaiting Visit">
                                            New Patients Awaiting Visit
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_notes_pending_signature_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.notes_pending_signature') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Notes Pending Signature">
                                            Notes Pending Signature
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_notes_pending_billing_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.notes_pending_billing') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Notes Pending Billing">
                                            Notes Pending Billing
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_bills_pending_signature_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.bills_pending_signature') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Bills Pending Signature">
                                            Bills Pending Signature
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_incoming_reports_pending_signature_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.reports_pending_signature') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Reports Pending Signature">
                                            Reports Pending Signature
                                        </a>
                                    </th>
                                </tr>

                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_patients_without_appointment_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.patients_without_appointments') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Patients w/o Appointments">
                                            Patients w/o Appointments
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_patients_overdue_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.patients_overdue_for_visit') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Patients Overdue for Visit">
                                            Patients Overdue for Visit
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_cancelled_appointments_pending_acknowledgement_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.cancelled_appointments_pending_review') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Cancelled Appts. Pending Review">
                                            Cancelled Appts. Pending Review
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_cancelled_bills_awaiting_review_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.cancelled_bills_pending_review') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Cancelled Bills Pending Review">
                                            Cancelled Bills Pending Review
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_cancelled_supply_orders_awaiting_review_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.cancelled_supply_orders_pending_review') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Cancelled Supply Orders">
                                            Cancelled Supply Orders
                                        </a>
                                    </th>
                                </tr>

                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_erx_and_orders_awaiting_signature_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.erx_and_orders_pending_signature') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="ERx & Orders Pending Signature">
                                            ERx & Orders Pending Signature
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_supply_orders_awaiting_signature_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.supply_orders_pending_signature') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Supply Orders Pending Signature">
                                            Supply Orders Pending Signature
                                        </a>
                                    </th>
                                </tr>
                                <tr>
                                    <th class="px-2 text-center">{{$pro->get_supply_orders_awaiting_shipment_count_as_mcp()}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.supply_orders_awaiting_shipment') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           popup-style="tall"
                                           title="Supply Orders Awaiting Shipment">
                                            Supply Orders Awaiting Shipment
                                        </a>
                                    </th>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="card mb-4" stag-collapsible-card="mcp-rm">
                        <div class="card-header pl-2">
                            <strong>
                                Remote Monitoring: {{friendly_month(date('Y-m-d'))}}
                            </strong>
                        </div>
                        <div class="card-body p-0">
                            <table class="table mb-0 dashboard-stats-table">
                                <tbody>
                                <tr class="thin">
                                    <th class="px-2 text-center">{{$pro->get_measurements_awaiting_review_count_as_mcp() ?? '-'}}</th>
                                    <th class="pl-2">
                                        <a href="{{ route('mcp.measurements_pending_stamping') }}"
                                           native target="_blank"
                                           open-in-stag-popup
                                           update-parent
                                           popup-style="tall"
                                           title="Measurements Pending Stamping">
                                            Measurements Pending Stamping
                                        </a>
                                    </th>
                                </tr>
                                <tr class="thin">
                                    <th class="px-2 text-center">{{$pro->get_patients_without_remote_measurement_in_48_hours_count_as_mcp() ?? '-'}}</th>
                                    <th class="pl-2">Patients w/o Measurement in 48 hrs.</th>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="card mb-4" stag-collapsible-card="mcp-practice-management" collapsed>
                        <div class="card-header pl-2">
                            <strong>
                                Practice Management
                            </strong>
                        </div>
                        <div class="card-body p-0">
                            <table class="table mb-0 dashboard-stats-table">
                                <tbody>
                                <tr class="thin">
                                    <th colspan="2" class="font-weight-normal px-2 pl-2">Billing & Reimbursement</th>
                                </tr>
                                <tr class="thin">
                                    <th class="font-weight-normal px-2 pl-4">{{friendly_date_time($reimbursement['nextPaymentDate'], false)}}</th>
                                    <th class="font-weight-normal pl-2">Next Payment Date</th>
                                </tr>
                                <tr class="thin">
                                    <th class="font-weight-normal px-2 pl-4">
                                        ${{friendly_money($reimbursement['currentBalance'])}}</th>
                                    <th class="font-weight-normal pl-2 w-100"><a
                                            href="/practice-management/financial-transactions">Current balance</a></th>
                                </tr>
                                <tr class="thin">
                                    <th class="font-weight-normal px-2 pl-4">
                                        ${{friendly_money($reimbursement['nextPaymentAmount'])}}</th>
                                    <th class="font-weight-normal pl-2"><a
                                            href="/practice-management/bills-under-processing">Processing</a></th>
                                </tr>
{{--
                                <tr class="thin">
                                    <th class="font-weight-normal px-2 pl-5">
                                        ${{friendly_money($reimbursement['nextPaymentAmount'])}}</th>
                                    <th class="font-weight-normal pl-2"><a
                                            href="/practice-management/bills-under-processing">Treatment Services</a>
                                    </th>
                                </tr>
                                <tr class="thin">
                                    <th class="font-weight-normal px-2 pl-5">
                                        ${{friendly_money($reimbursement['nextPaymentAmount'])}}</th>
                                    <th class="font-weight-normal pl-2"><a
                                            href="/practice-management/bills-under-processing">Remote Monitoring</a>
                                    </th>
                                </tr>
                                <tr class="thin">
                                    <th class="font-weight-normal px-2 pl-5">
                                        ${{friendly_money($reimbursement['nextPaymentAmount'])}}</th>
                                    <th class="font-weight-normal pl-2"><a
                                            href="/practice-management/bills-under-processing">Other Services</a></th>
                                </tr>
                                {{--
                                <tr>
                                    <th class="px-2">{{$reimbursement['lastPayment']}}</th>
                                    <th class="pl-2"><a href="/practice-management/financial-transactions">Last payment</a></th>
                                </tr>
                                <tr>
                                    <th class="px-2">{{friendly_date_time($reimbursement['lastPaymentDate'], false)}}</th>
                                    <th class="pl-2"><a href="/practice-management/financial-transactions">Last payment date</a></th>
                                </tr>
                                --}}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="col-md-9 pl-1">
                    <div class="row mcp-theme-1">
                        <div class="col-md-6 mcp-theme-1">
                            <div id="mcp-dashboard-appointments" class="mb-4">

                            </div>
                            <div class="card mb-4">
                                <div class="card-header pl-2">
                                    <strong>
                                        Measurements Pending Stamping
                                    </strong>
                                </div>
                                <div class="card-body p-0">
                                    @include('app.mcp.dashboard.measurements-pending-stamping')
                                </div>
                            </div>
                            @if($pro->is_considered_for_supervising_physician)
                            <!-- ENCOUNTERS PENDING MY REVIEW -->
                            <div class="card mb-4">
                                <div class="card-header pl-2">
                                    <strong>
                                        Charts Pending My Review
                                    </strong>
                                </div>
                                <div class="card-body p-0">
                                    @include('app.ps.dashboard.client-charts-pending-my-review')
                                </div>
                            </div>
                            @endif
                        </div>
                        <div class="col-md-6 mcp-theme-1 pl-1">
                            <div class="card mb-4">
                                <div class="card-header pl-2">
                                    <strong>
                                        Notifications
                                    </strong>
                                </div>
                                <div class="card-body px-3 py-2">
                                    @include('app.mcp.dashboard.notifications')
                                </div>
                            </div>
                            <div class="card mb-4">
                                <div class="card-header pl-2">
                                    <strong>
                                        Messages
                                    </strong>
                                </div>
                                <div class="card-body p-0">
                                    @include('app.mcp.dashboard.messages')
                                </div>
                            </div>
                            <div class="card mb-4">
                                <div class="card-header pl-2">
                                    <strong>
                                        Phone Calls & Memos
                                    </strong>
                                </div>
                                <div class="card-body p-0">
                                    @include('app.mcp.dashboard.calls_memos')
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </div>

    <div class="stag-popup stag-popup-md ticket-popup mcp-theme-1" stag-popup-key="ticket-popup"></div>

    <script>
        (function () {

            let datesWithEvents = [],
                selectedDate = '{{ date('Y-m-d') }}',
                calendarElem = null,
                currentMonth = null,
                currentYear = null,
                appointmentsLoaded = false;

            function formatDate(date) {
                let d = new Date(date),
                    month = '' + (d.getMonth() + 1),
                    day = '' + d.getDate(),
                    year = d.getFullYear();

                if (month.length < 2)
                    month = '0' + month;
                if (day.length < 2)
                    day = '0' + day;

                return [year, month, day].join('-');
            }

            function onDateChange(_newDate) {
                // ajax load appts list as markup directly from server
                selectedDate = _newDate;
                $.get('/pro-dashboard-events-display/' + selectedDate + '/' + selectedDate, function (_data) {
                    let apptscontainer = $('#mcp-dashboard-appointments');
                    apptscontainer.html(_data);
                    initFastLoad(apptscontainer);
                    initMoes();
                });
            }

            function loadEventDates(_refDate = false) {
                let today = new Date(_refDate ? _refDate : '{{date('Y-m-d')}}'),
                    firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1),
                    lastOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);
                selectedDate = null;
                $('td.day.active').removeClass('active');
                $.get('/pro-dashboard-event-dates/' +
                    formatDate(firstOfMonth) + '/' +
                    formatDate(lastOfMonth), (_data) => {
                    datesWithEvents = _data;
                    calendarElem.datepicker('refresh');
                    currentMonth = firstOfMonth.getMonth();
                    currentYear = firstOfMonth.getFullYear();
                    if (!_refDate && $('td.day[data-date="{{$milliseconds}}"]:visible').length) {
                        $('td.day[data-date="{{$milliseconds}}"]:visible').first().click();
                    }
                    appointmentsLoaded = true;
                }, 'json');
            }

            function getFormattedCurrentDate() {
                let date = new Date();
                let day = date.getDate();
                day = day < 10 ? '0' + day : day;
                return date.getFullYear() + '-' + parseInt(date.getMonth() + 1) + '-' + day;
            }

            function init(_target = null) {

                if(_target && _target !== '.stag-content') return;

                calendarElem = $('.pro-dashboard-inline-calendar');
                calendarElem.datepicker({
                    dateFormat: 'yy-mm-dd',
                    onSelect: function (_date) {
                        onDateChange(_date);
                    },
                    onChangeMonthYear: function (_year, _month) {
                        let date = _year + '-' + (_month < 10 ? '0' : '') + _month + '-05';
                        loadEventDates(date);
                    },
                    beforeShowDay: function (d) {
                        if (datesWithEvents && datesWithEvents.indexOf(formatDate(d)) !== -1) {
                            return [true, 'has-events'];
                        }
                        return [true, 'no-events'];
                    },
                    defaultDate: 0
                });

                let date = new Date();
                let day = date.getDate();
                day = day < 10 ? '0' + day : day;
                let mon = parseInt(date.getMonth() + 1);
                mon = mon < 10 ? '0' + mon : mon;
                let dateStr = date.getFullYear() + '-' + mon + '-' + day;
                loadEventDates(dateStr);

                onDateChange(getFormattedCurrentDate());
                selectedDate = getFormattedCurrentDate();

                $(document)
                    .off('click', '.ack-client-pro-change')
                    .on('click', '.ack-client-pro-change', function () {
                        let trigger = $(this).text('…');
                        $.post('/api/clientProChange/accept', {
                            uid: $(this).attr('data-uid')
                        }, _data => {
                            if (!hasResponseError(_data)) {
                                trigger.hide();
                                let doneElem = $('<i class="text-success fa fa-check"></i>');
                                doneElem.insertAfter(trigger);
                                setTimeout(() => {
                                    let ackContainer = trigger.closest('.ack-container');
                                    trigger.closest('div').slideUp('fast', function () {
                                        $(this).remove();
                                        if (!ackContainer.find('>div').length) {
                                            ackContainer.remove();
                                        }
                                    });
                                }, 500);
                            }
                        }, 'json');
                        return false;
                    });

                $(document)
                    .off('click', '.ack-client-memo')
                    .on('click', '.ack-client-memo', function () {
                        let trigger = $(this).text('…');
                        $.post('/api/clientMemo/stamp', {
                            uid: $(this).attr('data-uid')
                        }, _data => {
                            if (!hasResponseError(_data)) {
                                trigger.hide();
                                let doneElem = $('<i class="text-success fa fa-check"></i>');
                                doneElem.insertAfter(trigger);
                                setTimeout(() => {
                                    let tbody = trigger.closest('tbody');
                                    trigger.closest('tr').remove();
                                    if (!tbody.find('>tr').length) {
                                        tbody.closest('.ack-container').remove();
                                    }
                                }, 500);
                            }
                        }, 'json');
                        return false;
                    });

                $(document)
                    .off('click', '.ack-pro-appt-update')
                    .on('click', '.ack-pro-appt-update', function () {
                        let trigger = $(this).text('…');
                        $.post('/api/appointmentConfirmationDecision/acknowledgeAsAppointmentPro', {
                            uid: $(this).attr('data-uid')
                        }, _data => {
                            if (!hasResponseError(_data)) {
                                trigger.hide();
                                let doneElem = $('<i class="text-success fa fa-check"></i>');
                                doneElem.insertAfter(trigger);
                                setTimeout(() => {
                                    let ackContainer = trigger.closest('tbody');
                                    trigger.closest('tr').slideUp('fast', function () {
                                        $(this).remove();
                                        if (!ackContainer.find('>tr').length) {
                                            ackContainer.remove();
                                        }
                                    });
                                }, 500);
                            }
                        }, 'json');
                        return false;
                    });

                addMCHook('refreshDashboardAppointments', function() {
                    onDateChange(selectedDate);
                });
            }

            addMCInitializer('pro-dashboard', init, '#pro-dashboard-container');
        })();
    </script>
@endsection
