<table class="table table-hover portlet light bg-inverse" id="repTable" style="width: 100%;">
    <thead>
        <tr>
            <th style="width: 10px !important;">#</th>
            <td>  Rec. No. </td>
            <th>   Patient Name</th>
            <th >   Patient No.</th>
            <th class="hide_me">   Wife National No.</th>
            <th class="hide_me">   Husband National No.</th>
            <th class="hide_me">   Dept.</th>
            <th>   Type</th>

            {if $login_role != 19 }
            <th>   Total      </th>
            <th>   Discount   </th>
            <th>   Cash </th>
            <th>   Visa  </th>
            <th>   Debt </th>
            {/if}

            {if $share == 1 }
            <th>RFC</th>
            <th>Refer Dr.</th>
            <th>Doctor</th>
            {/if}


            <th>Visit Doctor</th>


            <th>   Notes    </th>
            <th>   User name  </th>
            <th>   Visit Date</th>
            {if $share == 2 }  <th>   Visit Hour</th> {/if}
        </tr>
    </thead>
    <tbody>
        {assign var="i" value="1"}
        {foreach $allData as $data}
        <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.printserial}</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="hide_me">{$data.wifenationalid}</td>
            <td class="hide_me">{$data.husbandnationalid}</td>
            <td class="hide_me">{$data.deptName}</td>
            <td>{$data.type}</td>

            {if $login_role != 19 }
            <td>
                {$data.totaldetectionvalue}
            </td>
            <td>
                {$data.discount}
            </td>
            <td>
                {$data.detectionvalue_cash}
            </td>
            <td>
                {$data.detectionvalue_visa}
            </td>
            <td>
                {$data.restdetectionvalue}
            </td>
            {/if}

            {if $share == 1 }
            <td>  
                {$data.rfc}
            </td>
            <td>
                {$data.refer}
            </td>
            {if  $data.for_husband == 1 }
            <td>
                {$data.h_doctor}
            </td>
            {else}
            <td>
                {$data.doctor}
            </td>
            {/if}

            {/if}

            <td> {$data.visitDoctor}  {if  $drDeptName == 1 }  -   {$data.deptName}  {/if}  </td>

            <td>
                {$data.vnotes}
            </td>

            <td>
                {$data.insert_user}
            </td>
            <td>{$data.visitdate}</td>
            {if $share == 2 }
            <td>{$data.visitHour}</td>
            {/if}

        </tr>
        {assign var="i" value={$i+1}}
        {/foreach}
    </tbody>
</table>

{if $login_role != 19 }
<div id="totalDiv">
    {if !empty($allData)}
    <table class="table table-hover portlet light bg-inverse ">
        <tr>
        <tr>
            <th class="wds15"> </th>
            <th class="wds17"> Cost</th>
            <th class="wds17"> Discount</th>
            <th class="wds17"> Cash</th>
            <th class="wds17"> Visa</th>
            <th class="wds17"> Debt</th>
        </tr>
        </tr>
        <tr>
            <td>New Visits</td>
            <th id="totalCost"> {$totalCost}</th>
            <th  id="totalDiscount"> {$totalDiscount}</th>
            <th  id="totalCash"> {$totalCash}</th>
            <th  id="totalVisa"> {$totalVisa}</th>
            <th  id="totalDept"> {$totalDept}</th>
        </tr>
        <tr>
            <td  id="totalCost">Old Debts</td>
            <th> </th>
            <th> </th>
            <th  id="totalOldCash"> {$totalOldCash}</th>
            <th  id="totalOldVisa"> {$totalOldVisa}</th>
            <th></th>
        </tr>
        <tr  style="background-color: #efc9cc;"  >
            <td>Refund</td>
            <td> </td>
            <td> </td>
            <td  id="totalRefundCash"> {$totalRefundCash}</td>
            <td  id="totalRefundVisa"> {$totalRefundVisa}</td>
            <td></td>
        </tr>
        <tr>
            <td>Total</td>
            <th> </th>
            <th> </th>
            <th  id="totalC"> {$totalC}</th>
            <th  id="totalV">{$totalV} </th>
            <th> </th>
        </tr>
    </table>
    {/if}
</div>
{/if}


{literal}
<script type="text/javascript" >
    $('#repTable').dataTable({
        dom: 'Blfrtip',
        buttons: [
            {
                extend: 'print',
                title: 'Financial 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 Report'

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



                    function Addrow(newIndex, data) {
                        row = '  <row r="' + newIndex + '"> ';
                        row += '  <c t="inlineStr" r="C' + newIndex + '"><is><t>' + data[0] + '</t></is></c> ';
                        row += ' <c t="inlineStr" r="D' + newIndex + '"><is><t>' + data[1] + ' </t></is></c>  ';
                        row += '  <c t="inlineStr" r="E' + newIndex + '"><is><t>' + data[2] + ' </t></is></c> ';
                        row += '  <c t="inlineStr" r="F' + newIndex + '"><is><t>' + data[3] + ' </t></is></c> ';
                        row += '  <c t="inlineStr" r="G' + newIndex + '"><is><t>' + data[4] + ' </t></is></c> ';
                        row += '  <c t="inlineStr" r="H' + newIndex + '"><is><t>' + data[5] + ' </t></is></c> ';
                        row += ' </row>  ';
                        $('sheetData', sheet).append(row);
                    }

                    Addrow(newIndex, [' ', 'Cost', 'Discount', 'Cash', 'Visa', 'Debt']);
                    newIndex = 1 + newIndex;
                    Addrow(newIndex, ['New Visits', $('#totalCost').html(), $('#totalDiscount').html(), $('#totalCash').html()
                                , $('#totalVisa').html(), $('#totalDept').html()]);
                    newIndex = 1 + newIndex;
                    Addrow(newIndex, ['Old Debts', ' ', ' ', $('#totalOldCash').html(), $('#totalOldVisa').html(), ' ']);
                    newIndex = 1 + newIndex;
                    Addrow(newIndex, [' Refund', ' ', ' ', $('#totalRefundCash').html(), $('#totalRefundVisa').html(), ' ']);
                    //    $('row:first c', sheet).attr('s', '39');
                    newIndex = 1 + newIndex;
                    Addrow(newIndex, [' Total', ' ', ' ', $('#totalC').html(), $('#totalV').html(), ' ']);
                }

            }
        ],
        "scrollX": true
    });


</script>
{/literal}