<?php require_once('../auth/auth.php');?>
<?php 
 $gcatid   = $_POST['gcatid'];
 $gbid     = $_POST['gbid'];
 $gbrid    = $_POST['gbrid'];
 $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)) - SUM(COALESCE(CASE WHEN t.cr_acid = a.acid THEN amount END,0))) balance 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)<='$edate' GROUP BY a.ac_name HAVING balance > 0 ORDER BY a.acid ASC") or die(mysqli_error($conn));
      $sbdata = [];
    while($sbbl= mysqli_fetch_assoc($subbal)){
      $sbdata[]=$sbbl;
    }

?>

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

        sbdata += '<tr>';

        sbdata += '<td colspan="2" class="btn-outline-dark" style="padding:3px 40px;" onclick="vprofile('+svalue.acid+')">'+svalue.ac_name+'<span style="float:right;font-weight: 600;">'+bden.format(Number(svalue.balance))+'</span></td>';
        sbdata += '</tr>';
      });
        $('#id<?php echo $gcatid;?>').append(sbdata);
        $('#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>