<?php require_once('../auth/auth.php');?>
<?php 
 $gcatid   = $_POST['gcatid'];
 $gbid     = $_POST['gbid'];
 $gbrid    = $_POST['gbrid'];
 $fdate    = $_POST['fdate'];
 $edate    = $_POST['edate'];

$subbal = mysqli_query($conn,"SELECT a.acid,a.ac_name,
SUM(COALESCE(CASE WHEN t.dr_acid = a.acid THEN amount END,0)) debit,
SUM(COALESCE(CASE WHEN t.cr_acid = a.acid THEN amount END,0)) credit FROM transaction t LEFT JOIN accounts a ON (t.dr_acid=a.acid || t.cr_acid=a.acid) WHERE  t.coid='$coid' AND t.bid='$gbid' AND t.brid='$gbrid' AND t.active=1 AND a.gcatid = '$gcatid' AND date(t.date) BETWEEN '$fdate' AND '$edate' GROUP BY a.ac_name HAVING (debit > 0 || credit > 0) ORDER BY a.acid ASC") or die(mysqli_error($conn));
      $sbdata = [];
    while($sbbl= mysqli_fetch_assoc($subbal)){
      $sbdata[]=$sbbl;
    }

?>
<table style="width:100%;" id="subtable<?php echo $gcatid;?>">
  <thead>
    <tr>
       <th class="text-center" style="width:5%;">S/l</th>
       <th style="width:65%;">Account Name</th>
       <th class="text-center" style="width:10%;">Debit</th>
       <th class="text-center" style="width:10%;">Credit</th>
       <th class="text-center" style="width:10%;">balance</th>
    </tr>
  </thead>
  <tbody></tbody>
   <tfoot>                                
      <tr>
         <th class="text-right" colspan="2">Sub Total</th>
         <th class="text-right" id="ssumdrb"></th>
         <th class="text-right" id="ssumcrb"></th>
         <th class="text-right" id="ssbalnc"></th>
      </tr>
   </tfoot>
</table>

<script id="script">
  $(document).ready(function () {
      var sbdata = '';
      var x = 1;
      var debit = 0;
      var credit = 0;
      var balance = 0;
      var ssumdrb = 0;
      var ssumcrb = 0;
      $.each(<?php echo json_encode($sbdata)?>, function (key, svalue) {

      ssumdrb += Number(svalue.debit);
      ssumcrb += Number(svalue.credit);

       debitt  = (Number(svalue.debit) <= 0)?'':bden.format(Number(svalue.debit));
       creditt = (Number(svalue.credit) <= 0)?'':bden.format(Number(svalue.credit));        

        sbdata += '<tr>';
        sbdata += '<td class="text-center">' + x++ + '</td>';

        sbdata += '<td class="btn-outline-dark" onclick="vprofile('+svalue.acid+')">' + svalue.ac_name +'</td>';

        sbdata += '<td class="text-right">' + debitt + '</td>';

        sbdata += '<td class="text-right">' + creditt + '</td>';

        sbdata += '<td class="text-right">' + bden.format(Number(svalue.debit-svalue.credit)) + '</td>';

        sbdata += '</tr>';
      });
      
      $('#subtable<?php echo $gcatid;?> tbody').append(sbdata);
      $('#subtable<?php echo $gcatid;?> #ssumdrb').text(bden.format(ssumdrb));
      $('#subtable<?php echo $gcatid;?> #ssumcrb').text(bden.format(ssumcrb));
      $('#subtable<?php echo $gcatid;?> #ssbalnc').text(bden.format(ssumdrb-ssumcrb));
      $('#script').remove();
        toastr.success(Showing, yourdata);
  });
</script>
<script>
  function vprofile(acid){    
    $("#modal-xl").modal({backdrop: "static"});
    pagecall('../accounts/ac_profile.php?acid='+acid,'#xlmdview');   
   }    
</script>