{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    <style>
        /*table { table-layout: fixed; }*/
        /*table th, table td { overflow: hidden; }*/
        table td { border : 1px solid grey !important; }
    </style>
    <h5 class="report-header" style=" width:275%">{{ page_title }}</h5>
    <table class="report-data" width="100%%">
        <thead class="header">
        <tr>
            <th>Clinic</th>
            <th>Referring Affiliate</th>
            <th>Referring Physician</th>
            <th>Patient</th>
            <th>Service(s)</th>
            <th>Referral Date</th>
            <th>Encounter Type</th>
            <th>Encounter Status</th>
            <th>Sold</th>
            <th>Agent</th>
            <th>Rate</th>
            <th>Total</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% set totalRefProvider = 0 %}
        {% set totalRefHf = 0 %}
        {% set totalCompany = 0 %}
        {% set lastRefProvider = null %}
        {% set lastRefHf = null %}
        {% set lastCompany = null %}

        {% for record in records %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}
            {% if loop.index0 > 0 and record.healthFacility != lastCompany %}
                <tr>
                    <td>Total</td>
                    <td colspan="7"></td>
                    <td>{{ totalRefProvider }}</td>
                </tr>
                <tr>
                    <td>Total</td>
                    <td colspan="8"></td>
                    <td>{{ totalRefHf }}</td>
                </tr>
                <tr>
                    <td>Total</td>
                    <td colspan="9"></td>
                    <td><strong>{{ totalCompany }}</strong></td>
                </tr>

                {% set totalRefProvider = 0 %}
                {% set totalRefHf = 0 %}
                {% set totalCompany = 0 %}
            {% elseif loop.index0 > 0 and record.referringHealthFacility != lastRefHf %}
                <tr>
                    <td>Total</td>
                    <td colspan="7"></td>
                    <td>{{ totalRefProvider }}</td>
                </tr>
                <tr>
                    <td>Total</td>
                    <td colspan="8"></td>
                    <td >{{ totalRefHf }}</td>
                </tr>

                {% set totalRefProvider = 0 %}
                {% set totalRefHf = 0 %}
            {% elseif loop.index0 > 0 and record.referringProvider != lastRefProvider %}
                <tr>
                    <td>Total</td>
                    <td colspan="7"></td>
                    <td>{{ totalRefProvider }}</td>
                </tr>
                {% set totalRefProvider = 0 %}
            {% endif %}

            <tr class="{{ trC }}">
                {% if record.rowDetailsCompany is defined %}
                    <td rowspan="{{ record.rowDetailsCompany.rowspan }}">{{ record.healthFacility|default('N/A') }}</td>
                {% endif %}
                {% if record.rowDetailsRefHf is defined %}
                    <td rowspan="{{ record.rowDetailsRefHf.rowspan }}">{{ record.referringHealthFacility|default('N/A') }}</td>
                {% endif %}
                {% if record.rowDetailsRefProvider is defined %}
                    <td rowspan="{{ record.rowDetailsRefProvider.rowspan }}">{{ record.referringProvider|default('N/A') }}</td>
                {% endif %}

                <td style=" overflow:hidden;">{{ record.patientName|default('N/A') }}</td>
                <td style=" overflow:hidden;">
                    {% set ps_split = record.patientServices|split(',') %}
                    {% for ps in ps_split %}
                        {% set patientService = ps|split('|') %}
                        <div>{{ patientService[1]|default('') }}</div>
                    {% endfor %}
                </td>
                <td>{{ record.referral_date|default('N/A') }}</td>
                <td>{{ record.chartType|default('N/A') }}</td>
                <td>{{ record.chartStatus|default('N/A') }}</td>
                <td>{{ record.sold|default('N/A') }}</td>
                <td>{{ record.agent|default('N/A') }}</td>
                <td>{{ record.cpapSetupSold|default('N/A') }}</td>
                <td>1</td>
            </tr>
            {% set totalRefProvider = totalRefProvider + 1 %}
            {% set totalRefHf = totalRefHf + 1 %}
            {% set totalCompany = totalCompany + 1 %}
            {% set lastRefProvider = record.referringProvider|default('N/A') %}
            {% set lastRefHf = record.referringHealthFacility|default('N/A') %}
            {% set lastCompany = record.healthFacility|default('N/A') %}

            {% if loop.last %}
                <tr>
                    <td>Total</td>
                    <td colspan="7"></td>
                    <td>{{ totalRefProvider }}</td>
                </tr>
                <tr>
                    <td>Total</td>
                    <td colspan="8"></td>
                    <td>{{ totalRefHf }}</td>
                </tr>
                <tr>
                    <td>Total</td>
                    <td colspan="9"></td>
                    <td><strong>{{ totalCompany }}</strong></td>
                </tr>
            {% endif %}
        {% endfor %}


    </table>
    <div>Total Records: {{ totalCount }}</div>
    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script>
        //<![CDATA[
        jQ(document).ready(function(){
            report_preview.init();
        });
        //]]>
    </script>
{% endblock %}