<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>
            {if $showBranchCol  == 1}     <th class="txtc"> الفرع   </th>      {/if} 
            <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 Dr</th>
            <th>Dr Fee</th>

            {if $referDrShow == 1 && $share != 1 }  <th>   Refer Dr</th> {/if}


            <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">
                    <ul style="margin-bottom: 22px;">
                        <li style="float: right;    margin-right: -15%;">{$data.wifename}</li>
                    </ul>
                    <ul style="margin-bottom: 22px;">
                        <li style="float: right;    margin-right: -15%;">{$data.husdandname}</li>
                    </ul>
                </a>
            </td>
            <td>{$data.statusno}</td>
            {if $showBranchCol  == 1}    <td> {$data.branch_name}</td>      {/if} 
            <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 $organizations == 3  }
            <td>{$data.contract_price}</td>
            {/if}

            {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.drFees}</td>

            {if $referDrShow == 1 && $share != 1 }  <td>    {$data.refer}</td> {/if}

            <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="wds15"> Cost</th>
            <th class="wds15"> Discount</th>
            <th class="wds15"> Cash</th>
            <th class="wds15"> Visa</th>
            <th class="wds15"> Debt</th>
            <th class="wds10"> Dr Fee</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>
            <th></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>
            <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>
            <td></td>
        </tr>
        <tr>
            <td>Total</td>
            <th> </th>
            <th> </th>
            <th  id="totalC"> {$totalC}</th>
            <th  id="totalV">{$totalV} </th>
            <th> </th>
            <th> {$totalDrFees}</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}