<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');require_once('../auth/function.php');?>
<?php
   $search     = $_GET['search'];
   $bbid       = $_GET['bbid'];
   $bbrid      = $_GET['bbrid'];
   $creditor_id= $_GET['creditor_id'];
   $nrow       = $_GET['nrow'];
   $ttype      = $_GET['ttype'];
   $areaid     = $_GET['areaid'];

    if ($bbid=='0') {
       $bbidcond = '';
    }elseif ($bbid >0){
        $bbidcond= " AND accounts.bid=$bbid";
    }

    if ($bbrid=='0') {
       $bbridcond = '';
    }elseif ($bbrid >0){
        $bbridcond= " AND accounts.brid=$bbrid";
    }

    if ($areaid=='0') {
       $area_condi = '';
    }elseif ($areaid >0){
        $area_condi= " AND accounts.ac_area=$areaid";
    }

    if ($ttype=='0') {
       $ttype_condi = '';
    }elseif ($ttype =='dr'){
        $ttype_condi= " AND accounts.balance > 0";
    }elseif ($ttype =='cr'){
        $ttype_condi= " AND accounts.balance < 0";
    }

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


     $debitor_query= mysqli_query($conn,"SELECT accounts.*,route_name,ac_name,ac_alise,ac_number,ac_email,ac_details FROM accounts 
        LEFT JOIN route_area ON (accounts.ac_area=route_area.roid)
        WHERE accounts.coid='$coid' $bbidcond $bbridcond $blacksearch $area_condi $ttype_condi AND gcatid='$creditor_id' ORDER BY accounts.ac_name LIMIT $nrow ") or die (mysqli_error($conn));
     $dataa = [];
     while ($prow= mysqli_fetch_assoc($debitor_query)){ $dataa[]=$prow;}



?>
<table id="creditor_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 class="text-center" style="width:%;"><?php echo language(164);?></th>
        <th class="text-center" style="width:%;"><?php echo language(110);?></th>
        <th class="text-center" style="width:%;"><?php echo language(112);?></th>
        <th class="text-center" style="width:%;"><?php echo language(111);?> </th>
        <th class="text-center" style="width:%;"><?php echo language(116);?> </th>
        <th class="text-center" style="width:%;"><?php echo language(224);?> </th>
        <th class="text-center" style="width:%;"><?php echo language(233);?></th>
    </tr>
</thead>
 <tbody id="creditor_show"></tbody>
 <tfoot> 
   <tr class="sticky-bottom">
    <th class="text-right" colspan="7" style="width:%;"><?php echo language(233);?></th>
    <th class="text-right" colspan="1"><dt id="t_value"></dt></th>
   </tr>                                 
 </tfoot>   
</table>
<script id="creditview" type="text/javascript">
   $(document).ready(function(){
       var dataa = '';
       var i     = 0;
       var t_val = 0;
       $.each(<?php echo json_encode($dataa);?>, function(key,value){
       t_val  += Number(value.balance);

       dataa += '<tr>';    
       dataa += '<td class="text-center">'+ ++i +'</td>';
       dataa += '<td onclick="acprofile('+value.acid+')">' + value.ac_name +  '</td>';
       dataa += '<td class="text-center">' + value.ac_alise + '</td>';
       dataa += '<td class="text-center">' + value.ac_email + '</td>';
       dataa += '<td class="text-center">' + value.ac_number + '</td>';
       dataa += '<td>' + value.ac_details + '</td>';
       dataa += '<td class="text-center">' + value.route_name + '</td>';
       dataa += '<td class="text-right">' + value.balance + '</td>';
       dataa += '</tr>';
   });
    $("#creditor_show").html(dataa);
      $("#t_value").text(bden.format(t_val));

    $("#creditview").remove();
   });
   
   

</script>
<script type="text/javascript">
   function acprofile(acid){    
     $("#modal-xl").modal({backdrop: "static"});
     pagecall('../accounts/ac_profile.php?acid='+acid,'#xlmdview');   
   }  
</script>
