<table class="table table-hover portlet light bg-inverse" id="repTable" style="width: 100%;">
    <thead>
        <tr>
            <th style="width: 10px !important;">#</th>
            <th> Visit Date</th>
            <th> Refer Dr</th>
            <th> Patient Name</th>
            <th> Patient No.</th>
            <th class=""> Patient National ID.</th>
            <th> Phone</th>
            <td> Bill No. </td>
            {if $login_role != 19 }
            <th> Total </th>
            <th> Discount </th>
            <th> Remain </th>
            <th> VAT </th>
            <th> Paid </th>
            {/if}
        </tr>
    </thead>
    <tbody>
        {assign var="i" value="1"}
        {assign var="totalTotals" value="0"}
        {assign var="totalDiscount" value="0"}
        {assign var="totalRemain" value="0"}
        {assign var="totalVat" value="0"}
        {assign var="totalPaid" value="0"}
        {foreach $allData as $data}

        {assign var="totalTotals" value=$totalTotals + $data.totaldetectionvalue}
        {assign var="totalDiscount" value=$totalDiscount + $data.discount}
        {assign var="totalRemain" value=$totalRemain + $data.restdetectionvalue}
        {assign var="totalVat" value=$totalVat + $data.vat_value}
        {assign var="totalPaid" value=$totalPaid + $data.detectionvalue_cash+$data.detectionvalue_visa}
        <tr class="odd " {if $data.detectionid==9999 || $data.billType==1 } style="background-color: #efc9cc;" {else if
            $data.customer_add==1} style="background-color: #f1f16d;" {/if}>
            <td>{$i}</td>
            <td>{$data.visitdate}</td>
            <td> {$data.refer}</td>
            <td>
                <a href="{$hosturl}/controllers/patienthistory.php?ac=show&patientid={$data.patientid}" target="_new">
                    {if $data.for_husband == 1 }
                    {$data.husdandname}
                    {else if $data.for_husband == 2}
                    طفل / {$data.wifename}
                    {else}
                    {$data.wifename}
                    {/if}
                </a>
            </td>
            <td>{$data.statusno}</td>
            <td class="">{$data.wifenationalid}</td>
            <td>{$data.phone}</td>
            <td>{$data.printserial}</td>
            {if $login_role != 19 }
            <td>
                {$data.totaldetectionvalue}
            </td>
            <td>
                {$data.discount}
            </td>
            <td>
                {$data.restdetectionvalue}
            </td>
            <td>
                {$data.vat_value}
            </td>
            <td>
                {($data.detectionvalue_cash+$data.detectionvalue_visa)|string_format:"%.2f"}
            </td>
            {/if}
        </tr>
        {assign var="i" value={$i+1}}
        {/foreach}
    </tbody>
    <tfoot>
        <tr>
            <td colspan="8">
                Total
            </td>
            <td>{$totalTotals}</td>
            <td>{$totalDiscount}</td>
            <td>{$totalRemain}</td>
            <td>{$totalVat}</td>
            <td>{$totalPaid|string_format:"%.2f"}</td>
        </tr>
    </tfoot>
</table>

<input type="hidden" id="totalTotals" value="{$totalTotals}"/>
<input type="hidden" id="totalDiscount" value="{$totalDiscount}"/>
<input type="hidden" id="totalRemain" value="{$totalRemain}"/>
<input type="hidden" id="totalVat" value="{$totalVat}"/>
<input type="hidden" id="totalPaid" value="{$totalPaid|string_format:"%.2f"}"/>
{literal}
<script type="text/javascript">
    $('#repTable').dataTable({
        dom: 'Blfrtip',
        buttons: [
            {
                extend: 'print',
                title: 'Financial Lab Report',
                customize: function (win) {
                    var total = $('#totalDiv').html();
                    $(win.document.body).append(total); //after the table
                    //    $(win.document.body).prepend('<html elements here>'); //before the table
                }
                , exportOptions: {
                    columns: ':not(.hide_me)',
                }
            }
            , {
                extend: 'excel'
                , text: 'excel '
                , title: 'Financial Lab Report'

                , customize: function (xlsx) {
                    var sheet = xlsx.xl.worksheets['sheet1.xml'];
                    var clRow = $('row', sheet);
                    var newIndex = 6 + clRow.length;

                    function Addrow(newIndex, data) {
                        row = '  <row r="' + newIndex + '"> ';
                        row += '  <c t="inlineStr" r="D' + newIndex + '"><is><t>' + data[0] + '</t></is></c> ';
                        row += ' <c t="inlineStr" r="I' + newIndex + '"><is><t>' + data[1] + ' </t></is></c>  ';
                        row += '  <c t="inlineStr" r="J' + newIndex + '"><is><t>' + data[2] + ' </t></is></c> ';
                        row += '  <c t="inlineStr" r="K' + newIndex + '"><is><t>' + data[3] + ' </t></is></c> ';
                        row += '  <c t="inlineStr" r="L' + newIndex + '"><is><t>' + data[4] + ' </t></is></c> ';
                        row += '  <c t="inlineStr" r="M' + newIndex + '"><is><t>' + data[5] + ' </t></is></c> ';
                        row += ' </row>  ';
                        $('sheetData', sheet).append(row);
                    }

                    Addrow(newIndex, ['Total', $('#totalTotals').val(), $('#totalDiscount').val(), $('#totalRemain').val(), $('#totalVat').val(), $('#totalPaid').val()]);
                }

            }
        ],
        "scrollX": true
    });


</script>
{/literal}