{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    <style>
        table { table-layout: fixed; }
        table th, table td { overflow: hidden; }
    </style>
    <form method="GET" action="{{ path('endorsed') }}">
        <div class="large-3 columns">
            <div class="input-group">
                <label class="input-group-label">Endorsed</label>
                <input type="text" style="margin: 0px !important;" id="form-endorsedUsers">
                <input type="hidden" name="endorsed" id="form-endorsedUsers-id" />
            </div>
        </div>
        <div class="large-3 columns">
            <div class="input-group">
                <label class="input-group-label">Patient</label>
                <input type="text" name="patient_name" value="{{ app.request.get('patient_name') ? app.request.get('patient_name') : '' }}" style="margin: 0px !important;" id="form-patient">
                <input type="hidden" name="patient" value="{{ app.request.get('patient') ? app.request.get('patient') : '' }}" id="form-patient-id" />

            </div>
        </div>
        <div class="large-3 columns">
            <div class="input-group">
                <label class="input-group-label">Status</label>
                <select class="input-group-field" name="status">
                    <option value='All'>All</option>
                    {% for es in endorsedStatus %}
                        {#<option value={{ es.id }} {{ es.id == status ? 'selected' : '' }} {{ es.description|lower == 'closed' or es.description|lower == 'completed' ? 'class="hide"' : '' }} >{{ es.description }}</option>#}
                        <option value={{ es.id }} {{ es.id == status ? 'selected' : '' }} >{{ es.description }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
        <div class="large-3 columns">
            <div class="input-group">
                <label class="input-group-label">Severity</label>
                <select class="input-group-field" name="severity">
                    <option value='All'>All</option>
                    {% for s in severity %}
                        <option value={{ s.id }} {{ s.id == severity_selected ? 'selected' : '' }} >{{ s.description }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
        <div class="large-12 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">
        <table class="report-data" width="100%">
            <thead class="header">
            <tr>
                <th style="width: 1%">Ticket ID</th>
                <th style="width: 3%">Endorsed By</th>
                <th style="width: 3%">Endorsed To</th>
                <th style="width: 3%">Ticket Date</th>
                <th style="width: 2%">Ticket Status</th>
                <th style="width: 2%">Ticket Severity</th>
                <th style="width: 3%">Branch</th>
                <th style="width: 3%">Patient(s)</th>
                <th style="width: 3%">Encounter(s)</th>
                <th style="width: 9%">Latest Notes</th>
            </tr>
            </thead>
            <tbody>
            {% for record in records %}
                {% set hasPatientDetails = record.patientDetails|length > 0 %}
                <tr>
                    <td><a href="{{ path('ticket_form', {action: 'u', id: record.ticketId|base64Encode}) }}" target="_blank">{{ record.ticketId }}</a></td>
                    <td>{{ record.createdBy }}</td>
                    <td>{{ record.ticketUser }}</td>
                    <td>{{ record.ticketDate }}</td>
                    <td>{{ record.ticketStatus }}</td>
                    <td>{{ record.ticketSeverity }}</td>
                    <td>
                        {% if record.patientDetails|length > 0 %}
                            {% for patientDetail in record.patientDetails %}
                                {{ patientDetail.branch }}
                                <br />
                            {% endfor %}
                        {% endif %}
                    </td>

                    <td>
                        {% if record.patientDetails|length > 0 %}
                            {% for patientDetail in record.patientDetails %}
                                <a href="{{ path('patient_detail', {patient_id: patientDetail.id|base64Encode, tab:'c'}) }}" target="_blank">{{ patientDetail.fullName|raw }}</a>
                                <br />
                            {% endfor %}
                            {% elseif record.patientChartDetails|length > 0 %}
                                {% for patientChartDetail in record.patientChartDetails %}
                                    <a href="{{ path('patient_detail', {patient_id: patientChartDetail.patientId|base64Encode}) }}" target="_blank">{{ patientChartDetail.patientFullName|raw }}</a>
                                    <br />
                                {% endfor %}
                        {% endif %}
                    </td>
                    <td>
                        {% if record.patientChartDetails|length > 0 %}
                            {% for patientChartDetail in record.patientChartDetails %}
                                <a href="{{ path('patient_chart_form', {patient_id: patientChartDetail.patientId|base64Encode, action: 'u', id: patientChartDetail.id|base64Encode}) }}" target="_blank">{{ patientChartDetail.chartDate }} - {{ patientChartDetail.chartType }}</a>
                                <br />
                            {% endfor %}
                        {% endif %}
                    </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="reveal" id="modal-small" 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,
                endorsed_autocompleteUrl: null,
                patient_autocompleteUrl: null,
            },
            init: function() {
                dashboard_report.bindUpdateForm();
                dashboard_report.bindAutoComplete();
            },
            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);
                });
            },
            bindAutoComplete: function() {
                global.autocomplete.bind(dashboard_report.settings.endorsed_autocompleteUrl, '#form-endorsedUsers', '#form-endorsedUsers-id');
                global.autocomplete.bind(dashboard_report.settings.patient_autocompleteUrl, '#form-patient', '#form-patient-id');
            }
        };
        dashboard_report.settings.updateFormUrl = '{{ path("patient_chart_update_is_done") }}';
        dashboard_report.settings.saveUrl = '{{ path("patient_chart_update_is_done_save") }}';
        dashboard_report.settings.endorsed_autocompleteUrl = '{{ path("account_user_active_autocomplete") }}';
        dashboard_report.settings.patient_autocompleteUrl = '{{ path('patient_autocomplete') }}';
        dashboard_report.init();
        //]]>
    </script>
{% endblock %}