{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    <div class="row max-width-initial">
        <div class="large-8 columns">
            <form method="GET" action="{{ path('referral_with_other_service_preview') }}">
                <div class="large-6 columns">
                    <div class="input-group">
                        <label class="input-group-label">Branch</label>
                        <select class="input-group-field" name="branch[]" multiple size="4">
                            <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-3 columns">
                    <div class="input-group">
                        <label class="input-group-label">Month </label>
                        <select class="input-group-field" name="month">
                            <option value="" selected>All</option>
                            {% for month in monthFilter %}
                                <option value="{{ month.value }}" {{ getMonth == month.value ? 'selected' : '' }}>{{ month.display }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="input-group">
                        <label class="input-group-label">Year </label>
                        <select class="input-group-field" name="year">
                            <option value="" selected>All</option>
                            {% for year in yearFilter %}
                                <option value="{{ year.value }}" {{ getYear == year.value ? 'selected' : '' }}>{{ year.display }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="large-3 columns">
                    <button type="submit" class="button just-continue" value=""
                            data-message="Are you sure you want to save this information?"
                    >Apply filter</button>
                    <button type="submit" name="export_to_excel" class="button just-continue" value=""
                            data-message="Are you sure you want to save this information?"
                    >Export to Excel</button>
                </div>
            </form>
        </div>
        <div class="large-4 columns" style="border: 1px solid; margin-bottom: 20px;">
            <p><strong>Summary</strong></p>
            {% for summary in summaries %}
                <p style="margin-bottom: 0px;">{{ summary.summaryCount }}</p>
            {% endfor %}
        </div>
    </div>
    <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%;">Referral Date</th>
                <th style="width: 4%;">Branch</th>
                <th style="width: 4%;">Full Name</th>
                <th style="width: 20%;">Latest Patient Notes</th>
                <th style="width: 1%;">Action</th>
            </tr>
            </thead>
            <tbody>
            {% for record in records %}
                <tr>
                    <td>{{ record.referralDate }}</td>
                    <td>{{ record.healthFacility }}</td>
                    <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode}) }}" target="_blank" style="color: {{ record.noteInterval == 'red' or record.latestNotes|raw == '' ? 'red' : '' }}">{{ record.fullName }}</a></td>
                    <td>{{ record.latestNotes|raw }}</td>
                    <td><a class="href-update-form" data-id="{{ record.patientId }}">Update</a></td>
                </tr>
            {% endfor %}
            <tr>
                <td colspan="3">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,
                autoCompleteUrl: ''
            },
            init: function() {
                dashboard_report.bindUpdateForm();
            },
            bindUpdateForm: function() {
                if(jQ('.href-update-form').length > 0) {

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

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

                        global.ajaxCancel(requestName);

                        var settings = {
                            url: dashboard_report.settings.updateFormUrl + '/' + 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.autocomplete.bind(dashboard_report.settings.autoCompleteUrl, '#ticket-user-assignment-fullName', '#ticket-user-assignment');
                            }
                        };

                        global.ajax(settings, requestName);
                    });
                }
            },
            bindUpdateFormAction: function() {
                jQ('.href-update-is-done-action').unbind('click').bind('click', function () {
                    var requestName = 'patientCreateTicketFormAction',
                        _form = jQ('#form-patientCreateTicket'),
                        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);
                });
            },
        };
        dashboard_report.settings.updateFormUrl = '{{ path("patient_create_ticket_form") }}';
        dashboard_report.settings.saveUrl = '{{ path("patient_create_ticket_save") }}';
        dashboard_report.settings.autoCompleteUrl = '{{ path("account_user_autocomplete") }}';
        dashboard_report.init();
        //]]>
    </script>
{% endblock %}