{% set absUrl = app.request.getSchemeAndHttpHost() ~ app.request.baseUrl|replace({'/app_dev.php': ''}) %}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="{{ absUrl ~ asset('bundles/rxconnexion/admin/css/report/report_wkpdf.css') }}" />
    <style>
        /*table { table-layout: fixed; }*/
        /*table th, table td { overflow: hidden; }*/
        table td { border : 1px solid grey !important; }
    </style>
</head>
<body>
<h2><strong>{{ page_title }}</strong></h2>
<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>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 }}</td>
            {% endif %}
            {% if record.rowDetailsRefHf is defined %}
                <td rowspan="{{ record.rowDetailsRefHf.rowspan }}">{{ record.referringHealthFacility }}</td>
            {% endif %}
            {% if record.rowDetailsRefProvider is defined %}
                <td rowspan="{{ record.rowDetailsRefProvider.rowspan }}">{{ record.referringProvider }}</td>
            {% endif %}

            <td style=" overflow:hidden;">{{ record.patientName }}</td>
            <td>{{ record.referral_date }}</td>
            <td>{{ record.chartType }}</td>
            <td>{{ record.chartStatus }}</td>
            <td>{{ record.sold }}</td>
            <td>{{ record.agent }}</td>
            <td>{{ record.cpapSetupSold }}</td>
            <td>1</td>
        </tr>
        {% set totalRefProvider = totalRefProvider + 1 %}
        {% set totalRefHf = totalRefHf + 1 %}
        {% set totalCompany = totalCompany + 1 %}
        {% set lastRefProvider = record.referringProvider %}
        {% set lastRefHf = record.referringHealthFacility %}
        {% set lastCompany = record.healthFacility %}

        {% 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>
</body>
</html>