{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    {% set userData = app.session.get('userData') %}
    <div id="flash-messages-container" style="font-size: 18px">
        <div id="flash-messages">
            <div class="row">
                <div class="large-8 medium-10 small-11 large-centered medium-centered small-centered columns" id="flash-messages-content">
                    {% if app.session.flashbag.has('success_messages') %}
                        <div class="success callout flash-messages" data-closable>
                            <i class="fi-check fa fa-check"></i>
                            {% for successMessage in app.session.flashbag.get('success_messages') %}
                                <div>{{ successMessage|raw }}</div>
                            {% endfor %}
                            <button class="close-button flash-messages-close-button" type="button" data-close>
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                    {% endif %}
                    {% if app.session.flashbag.has('error_messages') %}
                        <div class="alert callout flash-messages" data-closable>
                            <i class="fi-x-circle fa fa-times"></i>
                            {% for errorMessage in app.session.flashbag.get('error_messages') %}
                                <div>{{ errorMessage|raw }}</div>
                            {% endfor %}
                            <button class="close-button flash-messages-close-button" type="button" data-close>
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    <div id="full-screen-loading" style="transition: 0.3s; z-index:99999; width: 100vw; height: 100vh; background-color: rgba(0,0,0,.5); color: #fff; position: fixed; left: 0; top: 0;display: none;justify-content: center;align-items: center; ">
        <i class="fa fa-spinner fa-spin" style="font-size: 100px"></i>
    </div>
    <form method="GET" action="{{ path('my_task') }}">
        <div class="large-5 columns">
            <div class="input-group">
                <label class="input-group-label">Branch</label>
                <select class="input-group-field" name="branch">
                    <option value='All'>All</option>
                    {% for h in healthFacilities %}
                        <option value={{ h.id }} {{ h.id == branch ? 'selected' : '' }}>{{ h.description }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
        <div class="large-7 columns">
            <button type="submit" class="button just-continue" value=""
                    data-message="Are you sure you want to save this information?"
            >Apply filter</button>
        </div>
    </form>
    <div class="large-12 columns">
        <h5 class="report-header">{{ page_title }}</h5>
    </div>
    <div class="large-12 columns">
        <h5>Encounters</h5>
    </div>
    <div class="large-12 columns">
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th>Encounter ID</th>
                <th>Encounter Branch</th>
                <th>Encounter Date</th>
                <th>Patient</th>
                <th>Clinician</th>
                <th>Encounter Type</th>
                <th>Encounter Status</th>
                <th>Latest Notes</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
            {% for record in records %}
                <tr>
                    <td><a href="{{ path('patient_chart_form', {patient_id : record.patientId|base64Encode, action : 'u', id: record.patientChartId|base64Encode}) }}" target="_blank">{{ record.patientChartId }} </a></td>
                    <td>{{ record.healthFacility }}</td>
                    <td>{{ record.chartDate }}</td>
                    <td>{{ record.patient }}</td>
                    <td>{{ record.provider }}</td>
                    <td>{{ record.chartType }}</td>
                    <td>{{ record.chartStatus }}</td>
                    <td>{{ record.latestNote|raw }}</td>
                    <td><a href="#" class="href-update-form" data-action="u" data-id="{{ record.patientChartId }}">Update</a></td>
                </tr>
            {% endfor %}
            <tr>
                <td colspan="6">Total: {{ records|length|number_format(0, '.', ',') }}</td>
            </tr>
        </table>
    </div>
    <div class="large-12 columns">
        {#<h5>Tickets</h5>#}
        <h5>Endorsements</h5>
    </div>
    <div class="large-12 columns">
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th>Ticket ID</th>
                <th>Endorsed To</th>
                <th>Ticket Date</th>
                <th>Ticket Status</th>
                <th>Ticket Severity</th>
                <th>Branch</th>
                <th>Patient(s)</th>
                <th>Encounter(s)</th>
                <th>Latest Notes</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
            {% for record in records2 %}
                <tr>
                    <td><a href="{{ path('ticket_form', {patient_id : record.ticketId|base64Encode, id: record.ticketId|base64Encode}) }}" target="_blank">{{ record.ticketId }}</a></td>
                    <td>{{ record.ticketUser }}</td>
                    <td>{{ record.ticketDate }}</td>
                    <td>{{ record.ticketStatus }}</td>
                    <td>{{ record.ticketSeverity }}</td>
                    <td>
                        {% set patients = record.patients|split('~') %}
                        {% for patient in patients %}
                            {% set pt = patient|split('|') %}
                            {% if pt[2] is defined  %}
                                {{ pt[2] }}
                                <br>
                            {% endif %}
                        {% endfor %}
                    </td>
                    <td>
                        {% set patients = record.patients|split('~') %}
                        {% for patient in patients %}
                            {% set pt = patient|split('|') %}
                            {% if pt[1] is defined %}
                                <a target="_blank" href="{{ path('patient_detail', {patient_id: pt[0]|base64Encode}) }}">{{ pt[1] }}</a>
                                <br>
                            {% endif %}
                        {% endfor %}
                    </td>
                    <td>
                        {% set patientCharts = record.patientCharts|split('~') %}
                        {% for patientChart in patientCharts %}
                            {% set pc = patientChart|split('|') %}
                            {% if pc[1] is defined and pc[2] is defined %}
                                <a target="_blank" href="{{ path('patient_chart_form', { patient_id: pc[2]|base64Encode, action: 'u', id: pc[0]|base64Encode }) }}">{{ pc[0] ~ ' - ' ~  pc[1] }}</a>
                                <br>
                            {% endif %}
                        {% endfor %}
                    </td>
                    <td>{{ record.latestNote|raw }}</td>
                    {#<td><a href="#" class="href-update-form" data-action="u" data-id="{{ record.patientChartId }}">Update</a></td>#}
                    <td><a href="#" class="href-reply-form" data-action="u" data-id="{{ record.ticketId|base64Encode }}">Reply</a></td>
                </tr>
            {% endfor %}
            <tr>
                <td colspan="6">Total: {{ records2|length|number_format(0, '.', ',') }}</td>
            </tr>
        </table>
    </div>
    <div class="large-12 columns">
        <h5>Reminders</h5>
    </div>
    <div class="large-12 columns">
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th>Assigned By</th>
                <th>Assigned To User</th>
                <th>Assigned To User Group</th>
                <th>Category</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Notes</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
            {% for reminder in reminders %}
                <tr>
                    <td>{{ reminder.assigner_id == userData.id ? 'Me' : reminder.assigner }}</td>
                    <td>{{ reminder.assignedTo_id == userData.id ? 'Me' : reminder.assignedTo }}</td>
                    <td>{{ reminder.assignedToUserGroup }}</td>
                    <td>{{ reminder.reminderType_description }}</td>
                    <td>{{ reminder.startDate }}</td>
                    <td>{{ reminder.endDate }}</td>
                    <td>{{ reminder.note }}</td>
                    <td></td>
                </tr>
            {% endfor %}
            <tr>
                <td colspan="6">Total: {{ reminders|length|number_format(0, '.', ',') }}</td>
            </tr>
        </table>
    </div>
    <div class="large-12 columns">
        <h5>Receive Transfer Inventory</h5>
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th>SRS No</th>
                <th>Date</th>
                {#<th>Status</th>#}
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
            {% for transferInventory in transferInventories %}
                <tr>
                    <td>{{ transferInventory.transfer_no }}</td>
                    <td>{{ transferInventory.transfer_date }}</td>
                    {#<td>{{ transferInventory.status }}</td>#}
                    <td>
                        <a href="{{ path('transfer_inventory_item', {transferInventory_id: transferInventory.id|base64Encode}) }}" target="_blank">Details</a>
                    </td>
                </tr>
            {% endfor %}
            <tr>
                <td colspan="3">Total: {{ transferInventories|length }}</td>
            </tr>
        </table>
    </div>
    <div class="reveal large" id="modal" data-reveal>
        <div id="modal-content" style="padding: 10px;"></div>
        <button class="close-button" data-close aria-label="Close modal" type="button">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script>
        //<![CDATA[
        var dashboard_report = {
            settings: {
                updateFormUrl: null,
                saveUrl: null,
                ticketReplyFormUrl: null,
                reportParams: '',
            },
            init: function() {
                dashboard_report.bindUpdateForm();
                dashboard_report.bindReplyForm();
            },
            bindUpdateForm: function() {
                if(jQ('.href-update-form').length > 0) {

                    jQ('.href-update-form').unbind('click').bind('click', function(){

                        var requestName = 'patientChartReportStatus',
                            modal = jQ('#modal-small'),
                            modalContent = jQ('#modal-content');

                        global.ajaxCancel(requestName);

                        var settings = {
                            url: dashboard_report.settings.updateFormUrl + '/' + jQ(this).data('action') + '/' + jQ(this).data('id'),
                            type: 'GET',
                            beforeSend: function() {
                                modalContent.html('').addClass('has-loading');
                                modal.foundation('open');
                            },
                            success: function(json) {
                                modal.foundation('close');
                                modalContent.removeClass('has-loading').html(json.html);
                                modal.foundation('open');
                                global.bindFormSubmitted();
                                dashboard_report.bindUpdateFormAction()
                            }
                        };

                        global.ajax(settings, requestName);
                    });
                }
            },
            bindUpdateFormAction: function() {
                jQ('.href-update-is-done-action').unbind('click').bind('click', function () {
                    var requestName = 'patientChartReportStatusFormAction',
                        _form = jQ('#form-patient_chart_update_is_done'),
                        modal = jQ('#modal-small'),
                        axn = jQ(this).data('axn');
                    _form.find('.form-action').val(axn);

                    global.formSubmitted();
                    global.ajaxCancel(requestName);

                    var settings = {
                        url: dashboard_report.settings.saveUrl,
                        type: 'POST',
                        data: _form.serialize(),
                        success: function (json) {
                            if (json.success) {
                                location.reload();
                                // modal.foundation('close');
                                // global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'success', json.successMessages);
                            } else {
                                global.formSubmittedReverse();
                                global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                            }
                        }
                    };

                    global.ajax(settings, requestName);
                });
            },
            bindReplyForm: function() {
                if(jQ('.href-reply-form').length > 0) {

                    jQ('.href-reply-form').unbind('click').bind('click', function(e){
                        e.preventDefault();

                        var requestName = 'ticketReplyForm',
                            modal = jQ('#modal'),
                            modalContent = jQ('#modal-content');

                        global.ajaxCancel(requestName);

                        var settings = {
                            url: dashboard_report.settings.ticketReplyFormUrl + '/' + jQ(this).data('action') + '/' + jQ(this).data('id') + (dashboard_report.settings.reportParams !== ''  ? ('?' + dashboard_report.settings.reportParams ) : ''),
                            type: 'GET',
                            beforeSend: function() {
                                jQ("#full-screen-loading").css('display', 'flex');
                            },
                            success: function(json) {
                                jQ("#full-screen-loading").css('display', 'none');
                                modalContent.html(json.html);
                                modal.foundation('open');
                                global.bindFormSubmitted();
                            },
                            error: function(){
                                jQ("#full-screen-loading").css('display', 'none');
                            },
                        };

                        global.ajax(settings, requestName);
                    });
                }
            },
        };
        dashboard_report.settings.updateFormUrl = '{{ path("patient_chart_update_is_done") }}';
        dashboard_report.settings.saveUrl = '{{ path("patient_chart_update_is_done_save") }}';
        dashboard_report.settings.ticketReplyFormUrl = '{{ path("ticket_reply_form") }}';
        dashboard_report.settings.reportParams = '{{ reportParams }}';
        dashboard_report.init();
        //]]>
    </script>
{% endblock %}