{% 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>Patient</th>
            <th>Invoice No.</th>
            {#<th>Company</th>#}
            <th>Branch</th>
            {#<th>Branch Address</th>#}
            <th>Category</th>
            <th>Term</th>
            <th>Item Type</th>
            <th>Description</th>
            <th style="text-align: right;">Quantity</th>
            {% if userData.type == 'Super Admin' or 'Admin Report Invoice Itemised Cost Net' in userData.accesses %}
                <th style="text-align: right;">Cost</th>
                <th style="text-align: right;">Total Cost</th>
            {% endif %}
            <th style="text-align: right;">Price</th>
            <th style="text-align: right;">Total Price</th>
        </tr>
        </thead>
        <tbody>
        {% set totalCount = 0 %}
        {% set totalQty = 0 %}
        {% set totalCost = 0 %}
        {% set totalTotalCost = 0 %}
        {% set totalPrice = 0 %}
        {% set totalTotalPrice = 0 %}
        {% for record in records %}
            {% set trC = loop.index > 1 and loop.index % 2 == 0 ? 'even' : 'odd' %}
            {% set totalCount = totalCount + 1 %}
            {% set totalQty = totalQty + record.qty %}
            {% set totalCost = totalCost + record.cost %}
            {% set totalTotalCost = totalTotalCost + (record.cost / record.qty) %}
            {% set totalPrice = totalPrice + record.price %}
            {% set totalTotalPrice = totalTotalPrice + (record.price * record.qty) %}
            <tr>
                <td>{{ record.patientName }}</td>
                <td><a href="{{ path('patient_detail', {patient_id : record.patientId|base64Encode, tab:'b', invoiceNo:record.invoiceNo}) }}" target="_blank">{{ record.invoiceNo }}</a></td>
                {#<td>{{ record.companyDesc }}</td>#}
                <td>{{ record.branch }}</td>
                {#<td>{{ record.branchAddress }}</td>#}
                <td>{{ record.itemCategory }}</td>
                <td>{{ record.term }}</td>
                <td>{{ record.itemType }}</td>
                <td>
                    {% if record.inv_sub_item_count > 0 %}
                        <a href="javascript:void(0)" class="view_sub_item_href" data-sub_items="{{ record.sub_items }}" data-invoice_no="{{ record.invoiceNo }}" data-item="{{ record.itemDesc }}">{{ record.itemDesc|raw }}</a>
                    {% else %}
                        <a href="{{ path('item_detail', {item_id: record.itemId|base64Encode}) }}" target="_blank">{{ record.itemDesc|raw }}</a>
                    {% endif %}
                </td>
                <td style="text-align: right;">{{ record.qty }}</td>
                {% if userData.type == 'Super Admin' or 'Admin Report Invoice Itemised Cost Net' in userData.accesses %}
                    <td style="text-align: right;">{{ record.cost|number_format(2, '.', ',') }}</td>
                    <td style="text-align: right;">{{ (record.cost / record.qty)|number_format(2, '.', ',') }}</td>
                {% endif %}
                <td style="text-align: right;">{{ record.price|number_format(2, '.', ',') }}</td>
                <td style="text-align: right;">{{ (record.price * record.qty)|number_format(2, '.', ',') }}</td>
            </tr>
        {% endfor %}
        <tr>
            <td><strong>Total:</strong></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td style="text-align: right;"><strong>{{ totalQty }}</strong></td>
            {% if userData.type == 'Super Admin' or 'Admin Report Invoice Itemised Cost Net' in userData.accesses %}
                <td style="text-align: right;"><strong>{{ totalCost|number_format(2, '.', ',') }}</strong></td>
                <td style="text-align: right;"><strong>{{ totalTotalCost|number_format(2, '.', ',') }}</strong></td>
            {% endif %}
            <td style="text-align: right;"><strong>{{ totalPrice|number_format(2, '.', ',') }}</strong></td>
            <td style="text-align: right;"><strong>{{ totalTotalPrice|number_format(2, '.', ',') }}</strong></td>
        </tr>
    </table>
    <div>Total Records: {{ totalCount }}</div>
    <div class="reveal small" id="modal" data-reveal>
        <div id="modal-content"></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[
        jQ(document).ready(function(){
            report_preview.init();
            jQ('.view_sub_item_href').click(function () {
                const sub_items = jQ(this).data('sub_items');
                console.log(sub_items);
                let html = ``;

                html += `<h4>Invoice No. ${jQ(this).data('invoice_no')} &mdash; ${jQ(this).data('item')} </h4>`;
                html += `<ul style="margin: 15px 30px;">`;

                (sub_items).split('@').forEach(function (val) {
                    let item = val.split('~')
                    html += `<li><a href="${global.settings.url}/main/item/detail/${btoa(item[0])}" target="_blank" style="font-size: 16px;">${item[1]} <span class="float-right">${item[2]}</span></a></li>`;
                });
                html += `</ul>`;

                jQ('#modal-content').html(html);

                jQ('#modal').foundation('open');
            });
        });
        //]]>
    </script>
{% endblock %}