{% extends '@RxConnexion/Admin/global.html.twig' %}
{% block body %}
    <style>
        .toggle-sub-table, .toggle-sub-table-all{
            cursor: pointer;
        }
        .sub-table {
            width: 300px;
            border: 1px solid lightgrey;
        }
        .sub-table-header{
            background-color: lightgrey;
        }
    </style>
    <h5 class="report-header">{{ page_title }}</h5>
    <table class="report-data" width="100%">
        <thead class="header">
        <tr>
            <th>Payment Type</th>
            <th width="15%">Patient</th>
            <th>Invoice No.</th>
            <th>Billing Address</th>
            <th>Billing Branch</th>
            <th>Financing Company</th>
            <th>Pay Date</th>
            <th>Amount</th>
            <th>Status</th>
            <th colspan="2">Note</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% set totalAmount = 0 %}
        {% for record in records.payment %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}
            {% set totalAmount = totalAmount + record.amount %}
            {% set breakdowns = record.patientPayment_breakdowns == '' ? [] : record.patientPayment_breakdowns|split('~') %}
            <tr class="{{ trC }}">
                <td class="{#toggle-sub-table#}">
                    {{ record.pay_type }}
                    {% if breakdowns|length > 0 %}
                    <div class="sub-table" style="display: none;">
                        <div class="row sub-table-header">
                            <div class="columns small-6"><strong>Item</strong></div>
                            <div class="columns small-6 text-right"><strong>Amount Paid</strong></div>
                        </div>
                        {% for pbb in breakdowns %}
                            {% set dt = pbb|split('|') %}
                            <div class="row">
                                <div class="small-8 columns">{{ dt[3]|default('') }}</div>
                                <div class="small-4 columns text-right">{{ dt[2]|default('') }}</div>
                            </div>
                        {% endfor %}
                    </div>
                    {% endif %}
                </td>
                <td class="toggle-sub-table"><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode, tab: 'b' ,tab2 : 'bp'}) }}" target="_blank">{{ record.patientName }}</a></td>
                <td class="toggle-sub-table"><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode, tab: 'b' ,tab2 : 'bp', invoiceNo:record.invoiceNo}) }}" target="_blank">{{ record.invoiceNo }}</a></td>
                <td class="toggle-sub-table">{{ record.branchAddress }}</td>
                <td class="toggle-sub-table">{{ record.billingBranch }}</td>
                <td class="toggle-sub-table">{{ record.financingCompany }}</td>
                <td class="toggle-sub-table">{{ record.pay_date }}</td>
                <td class="toggle-sub-table text-right">{{ record.amount|number_format(2, '.', ',') }}</td>
                <td class="toggle-sub-table">{{ record.payment_status }}</td>
                <td class="toggle-sub-table">{{ record.note|raw }}</td>
                <td class="toggle-sub-table text-right">{% if breakdowns|length > 0 %}<i class="fa fa-caret-down"></i>{% endif %}</td>
            </tr>
        {% endfor %}
        <tr>
            <td><strong>Total:</strong></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td class="text-right"><strong>{{ totalAmount|number_format(2, '.', ',') }}</strong></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>
    <div>Total Record(s): {{ totalCount }}</div>
    <div></div>
    <div style="width: 100%;">
        <div style="width: 49%; float: left;">
            <h5 class="report-header">Payment Summary</h5>
            <table class="report-data" data-floathead="false" width="100%">
                <thead class="header">
                <tr>
                    <th>Billing Branch</th>
                    <th class="text-right">Transaction Count</th>
                    <th class="text-right">Total Amount</th>
                    {#<th class="text-right">Total Bill Due Amount</th>#}
                    {#<th class="text-right">Total Bill Paid Amount</th>#}
                    {#<th class="text-right">Total Bill Balance Amount</th>#}
                </tr>
                </thead>
                <tbody>
                {% set totalCount = 0 %}
                {% set totalAmt = 0 %}
                {% set totalTranCount = 0 %}
                {% for record in records.summary %}
                    {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
                    {% set totalCount = totalCount + 1 %}
                    {% set totalAmt = totalAmt + record.totalAmt %}
                    {% set totalTranCount = totalTranCount + record.transactionCount %}
                    <tr class="{{ trC }}">
                        <td>{{ record.billingBranch }}</td>
                        <td class="text-right">{{ record.transactionCount }}</td>
                        <td class="text-right">{{ record.totalAmt|number_format(2, '.', ',') }}</td>
                        {#<td class="text-right">{{ record.totalBillDueAmt|number_format(2, '.', ',') }}</td>#}
                        {#<td class="text-right">{{ record.totalBillPaidAmt|number_format(2, '.', ',') }}</td>#}
                        {#<td class="text-right">{{ record.totalBillBalAmt|number_format(2, '.', ',') }}</td>#}
                    </tr>
                {% endfor %}
                <tr>
                    <td><strong>Total:</strong></td>
                    <td class="text-right"><strong>{{ totalTranCount }}</strong></td>
                    <td class="text-right"><strong>{{ totalAmt|number_format(2, '.', ',') }}</strong></td>
                    {#<td class="text-right">{{ record.totalBillDueAmt|number_format(2, '.', ',') }}</td>#}
                    {#<td class="text-right">{{ record.totalBillPaidAmt|number_format(2, '.', ',') }}</td>#}
                    {#<td class="text-right">{{ record.totalBillBalAmt|number_format(2, '.', ',') }}</td>#}
                </tr>
            </table>
        </div>
        <div style="width: 49%; float: right;">
            <h5 class="report-header">Breakdown</h5>
            <table class="report-data" data-floathead="false" width="100%">
                <thead class="header">
                <tr>
                    <th>Payment Type</th>
                    <th class="text-right">Transaction Count</th>
                    <th class="text-right">Total Amount</th>
                    {#<th class="text-right">Total Bill Due Amount</th>#}
                    {#<th class="text-right">Total Bill Paid Amount</th>#}
                    {#<th class="text-right">Total Bill Balance Amount</th>#}
                </tr>
                </thead>
                <tbody>
                    {% set breakdown_totalCount = 0 %}
                    {% set breakdown_totalAmt = 0 %}
                    {% set breakdown_totalTranCount = 0 %}
                    {% for record in records.breakdown %}
                        {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
                        {% set breakdown_totalCount = breakdown_totalCount + 1 %}
                        {% set breakdown_totalAmt = breakdown_totalAmt + record.totalAmt %}
                        {% set breakdown_totalTranCount = breakdown_totalTranCount + record.transactionCount %}
                        <tr class="{{ trC }}">
                            <td>{{ record.paymentType }}</td>
                            <td class="text-right">{{ record.transactionCount }}</td>
                            <td class="text-right">{{ record.totalAmt|number_format(2, '.', ',') }}</td>
                            {#<td class="text-right">{{ record.totalBillDueAmt|number_format(2, '.', ',') }}</td>#}
                            {#<td class="text-right">{{ record.totalBillPaidAmt|number_format(2, '.', ',') }}</td>#}
                            {#<td class="text-right">{{ record.totalBillBalAmt|number_format(2, '.', ',') }}</td>#}
                        </tr>
                    {% endfor %}
                    <tr>
                        <td><strong>Total:</strong></td>
                        <td class="text-right"><strong>{{ breakdown_totalTranCount }}</strong></td>
                        <td class="text-right"><strong>{{ breakdown_totalAmt|number_format(2, '.', ',') }}</strong></td>
                        {#<td class="text-right">{{ record.totalBillDueAmt|number_format(2, '.', ',') }}</td>#}
                        {#<td class="text-right">{{ record.totalBillPaidAmt|number_format(2, '.', ',') }}</td>#}
                        {#<td class="text-right">{{ record.totalBillBalAmt|number_format(2, '.', ',') }}</td>#}
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    {% include '@RxConnexion/Admin/js.html.twig' %}
    <script>
        //<![CDATA[
        jQ(document).ready(function(){
            report_preview.init();

            /*let tgl = 0;*/
            jQ(".toggle-sub-table").click(function (e) {
                let subTable = jQ(this).parent().find('.sub-table');
                subTable.slideToggle();
                /*if(tgl === 0){
                    tgl = 1;
                    jQ('html,body').animate({
                        scrollTop: jQ(window).scrollTop() + 5
                    });
                } else if(tgl === 1){
                    tgl = 0;
                    jQ('html,body').animate({
                        scrollTop: jQ(window).scrollTop() - 5
                    });
                }*/
            });
            /*jQ(".toggle-sub-table-all").click(function (e) {
                jQ('.sub-table').slideToggle();
            })*/
        });
        //]]>
    </script>
{% endblock %}