{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    <h5 class="report-header">{{ page_title }}</h5>
    <table class="report-data" width="100%">
        <thead class="header">
        <tr>
            <th>Payer Type</th>
            <th>Patient</th>
            <th>Insurance</th>
            <th class="text-right">To Bill</th>
            <th class="text-right">Current</th>
            <th class="text-right">30 Days</th>
            <th class="text-right">60 Days</th>
            <th class="text-right">90 Days</th>
            <th class="text-right">120 Days</th>
            <th class="text-right">Credit</th>
            <th class="text-right">Amount</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% set totalCurrentBal = 0 %}
        {% set totalThirtyDays = 0 %}
        {% set totalSixtyDays = 0 %}
        {% set totalNinetyDays = 0 %}
        {% set totalOneTwentyDays = 0 %}
        {% set totalAmountBalance = 0 %}
        {% for record in records %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}
            {% set totalCurrentBal = totalCurrentBal + record.currentBal|default(0) %}
            {% set totalThirtyDays = totalThirtyDays + record.thirtyDays|default(0) %}
            {% set totalSixtyDays = totalSixtyDays + record.sixtyDays|default(0) %}
            {% set totalNinetyDays = totalNinetyDays + record.ninetyDays|default(0) %}
            {% set totalOneTwentyDays = totalOneTwentyDays + record.oneTwentyDays|default(0) %}
            {% set totalAmountBalance = totalAmountBalance + record.amountBalance|default(0) %}
            <tr class="{{ trC }}">
                <td>{{ record.payerType }}</td>
                <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode, tab:'b'}) }}" target="_blank">{{ record.full_name }}</a></td>
                <td>{{ record.patientInsurance }}</td>
                {#<td>{{ record.billTo|default('0.00') }}</td>#}
                <td class="text-right">--</td>
                <td class="text-right">{{ record.currentBal|default('0.00') }}</td>
                <td class="text-right">{{ record.thirtyDays|default('0.00') }}</td>
                <td class="text-right">{{ record.sixtyDays|default('0.00') }}</td>
                <td class="text-right">{{ record.ninetyDays|default('0.00') }}</td>
                <td class="text-right">{{ record.oneTwentyDays|default('0.00') }}</td>
                <td class="text-right">--</td>
                <td class="text-right">{{ record.amountBalance }}</td>
            </tr>
        {% endfor %}
        <tr>
            <td><strong>Total:</strong></td>
            <td></td>
            <td></td>
            {#<td></td>#}
            <td class="text-right"><b>--</b></td>
            <td class="text-right"><b>{{ totalCurrentBal|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>{{ totalThirtyDays|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>{{ totalSixtyDays|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>{{ totalNinetyDays|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>{{ totalOneTwentyDays|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>--</b></td>
            <td class="text-right"><b>{{ totalAmountBalance|number_format(2, '.', ',') }}</b></td>
        </tr>
    </table>
    <div>Total Records: {{ totalCount }}</div>
    <h5 class="report-header" style="margin-top: 30px;">AR Summary</h5>
    <table class="report-data" width="100%">
        <thead class="header">
        <tr>
            <th>Branch</th>
            <th class="text-right">Current</th>
            <th class="text-right">30 Days</th>
            <th class="text-right">60 Days</th>
            <th class="text-right">90 Days</th>
            <th class="text-right">120 Days</th>
            <th class="text-right">Amount</th>
        </tr>
        </thead>
        <tbody>
        {% set sum_totalCurrentBal = 0 %}
        {% set sum_totalThirtyDays = 0 %}
        {% set sum_totalSixtyDays = 0 %}
        {% set sum_totalNinetyDays = 0 %}
        {% set sum_totalOneTwentyDays = 0 %}
        {% set sum_totalAmountBalance = 0 %}
        {% for record2 in records2 %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set sum_totalCurrentBal = sum_totalCurrentBal + record2.currentBal|default(0) %}
            {% set sum_totalThirtyDays = sum_totalThirtyDays + record2.thirtyDays|default(0) %}
            {% set sum_totalSixtyDays = sum_totalSixtyDays + record2.sixtyDays|default(0) %}
            {% set sum_totalNinetyDays = sum_totalNinetyDays + record2.ninetyDays|default(0) %}
            {% set sum_totalOneTwentyDays = sum_totalOneTwentyDays + record2.oneTwentyDays|default(0) %}
            {% set sum_totalAmountBalance = sum_totalAmountBalance + record2.amountBalance|default(0) %}
            <tr class="{{ trC }}">
                <td>{{ record2.healthFacility }}</td>
                <td class="text-right">{{ record2.currentBal|default('0.00') }}</td>
                <td class="text-right">{{ record2.thirtyDays|default('0.00') }}</td>
                <td class="text-right">{{ record2.sixtyDays|default('0.00') }}</td>
                <td class="text-right">{{ record2.ninetyDays|default('0.00') }}</td>
                <td class="text-right">{{ record2.oneTwentyDays|default('0.00') }}</td>
                <td class="text-right">{{ record2.amountBalance }}</td>
            </tr>
        {% endfor %}
        <tr>
            <td><strong>Total:</strong></td>
            <td class="text-right"><b>{{ sum_totalCurrentBal|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>{{ sum_totalThirtyDays|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>{{ sum_totalSixtyDays|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>{{ sum_totalNinetyDays|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>{{ sum_totalOneTwentyDays|number_format(2, '.', ',') }}</b></td>
            <td class="text-right"><b>{{ sum_totalAmountBalance|number_format(2, '.', ',') }}</b></td>
        </tr>
    </table>
    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script>
        //<![CDATA[
        jQ(document).ready(function(){
            report_preview.init();
        });
        //]]>
    </script>
{% endblock %}