<?php require_once('../auth/auth.php');
$pritner_setup_query= mysqli_query($conn,"SELECT co_logo FROM company 
 WHERE coid ='$coid'  LIMIT 1 ") or die (mysql_error());
$rowps= mysqli_fetch_assoc ($pritner_setup_query);
$co_logo=$rowps['co_logo'];
?>
<?php
   $search     = $_GET['search'];
   $nrow       = $_GET['nrow'];
   $fdate      = $_GET['fdate'];
   $edate      = $_GET['edate'];

   $co_rs = mysqli_query($conn,"SELECT co_name,owner_name,co_address,co_phone,co_email FROM company WHERE coid='$coid'") or die (mysqli_error($conn));
   $cor= mysqli_fetch_assoc($co_rs);   

   

    if ($search =='0') {
         $blacksearch='';
      } else {
         $blacksearch = " AND (accounts.ac_name LIKE '%$search%' || accounts.ac_number LIKE '%$search%' || accounts.outlet_id LIKE '%$search%' )";
    }

        $debitor_id=mysqli_fetch_assoc(mysqli_query($conn,"SELECT ac_g_catid FROM ac_sub_g_category WHERE  coid='$coid' AND g_type='su-deb' LIMIT 1 "))['ac_g_catid'];

                 $sales_query=mysqli_query($conn,"SELECT  dr_acid,accounts.ac_name,accounts.ac_number,accounts.outlet_id,SUM(`amount`) as total,COUNT(amount) as inv_count
                          FROM  `transaction` LEFT JOIN accounts ON (accounts.acid=transaction.dr_acid) 
                          where
                           transaction.coid='$coid' AND transaction.bid='$bid' AND transaction.brid='$brid' AND 
                          transaction.ttype in ('ECSL','ERSL','INSL') AND transaction.active=1  AND transaction.dr_gcatid='$debitor_id'   AND date(transaction.date) BETWEEN '$fdate' AND '$edate' $blacksearch GROUP BY  accounts.ac_name ORDER BY total DESC   limit $nrow; ");
                         $dataa = [];
                         while ($invrow= mysqli_fetch_assoc($sales_query)){ $dataa[]=$invrow;}
?>



<table class="ttable border-bottom-0 d-none" id="header" cellpadding="0" cellspacing="0">
  <thead>
     <tr>
        <th rowspan="3" class="thborder border-bottom-0 text-center" style="width:10%;">
             <?php if($co_logo != ""): ?>
                            <img class="profile-user-img img-fluid img-circle" src="data:image/gif;base64,<?php echo $co_logo;?>" style="width:80px;height:75px;">
                        <?php else : ?>
                           <img class="profile-user-img img-fluid img-circle  " src="../../dist/img/a.png" style="width:80px;height:75px;">
                        <?php endif; ?>
                        
        </th>
        <th rowspan="3" class="thborder border-bottom-0" style="width:70%;">
              <dt style="font-size:18px;font-weight: 600;"><?php echo $cor['co_name'];?></dt>
              <dt><?php echo $cor['owner_name'];?></dt>
              <dt><?php echo $cor['co_address'];?></dt>
              <dt><?php echo $cor['co_phone'];?>#<?php echo $cor['co_email'];?></dt>
        </th>
        <th class="thborder text-center" style="width:20%;"><?php  echo language(397);  ?></th>
     </tr>
     <tr>
        
            <th class="thborder text-center"> <?php echo language(397);?> <?php echo $nrow;?></th>
     </tr>
     <tr>
        <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($fdate));?> To <?php echo date('d/m/y', strtotime($edate));?>
        </th>
     </tr>
  </thead>
</table>

<table id="sl_main_table" class="ttable" cellpadding="0" cellspacing="0">
<thead>
    <tr class="sticky-top">
        <th class="text-center" style="width:2%;"><?php echo language(15);?></th>
        <th style="width:17%;"><?php echo language(215);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(111);?></th>
        <th class="text-center" style="width:12%;"><?php echo language(398);?></th>
        <th class="text-center" style="width:13%;"><?php echo language(399); ?></th>
        <th class="text-center" style="width:13%;"><?php echo language(400); ?></th>
    </tr>
</thead>
 <tbody id="sinv_show"></tbody>
     <tfoot> 
        <tr class="sticky-bottom">
            <th class="text-right" colspan="5"><?php echo language(143);?></th>
            <th class="text-center"><dt id="t_bill"></dt></th>

            
   
        </tr>                                 
     </tfoot>   
</table>
<script id="inv_script" type="text/javascript">
   $(document).ready(function(){
       let dataa      = '';
       let i          = 0;
       let t_bill     = 0;

       
       $.each(<?php echo json_encode($dataa);?>, function(key,value){
       t_bill      += Number(value.total);

        dataa += '<tr>';    
        dataa += '<td class="text-center">'+ ++i +'</td>';
        dataa += '<td>' + value.ac_name +  ' / ' + value.outlet_id +  '</td>';
        dataa += '<td class="text-center">' + value.ac_number + '</td>';
        dataa += '<td class="text-center"> ' + value.total/value.inv_count +'</td>';
        dataa += '<td class="text-center">' + value.inv_count + '</td>';
        dataa += '<td class="text-center">' + value.total + '</td>';
        dataa += '</tr>';
   });
      $("#sinv_show").html(dataa);
      $("#t_bill").text(bden.format(t_bill));

      $("#inv_script").remove();
   });
</script>

