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

 $trbal = mysqli_query($conn,"SELECT g.ac_g_catid,g.ac_g_cat_name,SUM(COALESCE(CASE WHEN t.dr_gcatid = g.ac_g_catid THEN amount END,0))-SUM(COALESCE(CASE WHEN t.cr_gcatid = g.ac_g_catid THEN amount END,0)) balance FROM transaction t LEFT JOIN ac_sub_g_category g ON (t.dr_gcatid=g.ac_g_catid ||t.cr_gcatid=g.ac_g_catid) WHERE t.coid='$coid' AND t.bid='$gbid' AND t.brid='$gbrid' AND t.active=1 AND date(date) BETWEEN '$fdate' AND '$edate' GROUP BY g.ac_g_cat_name HAVING balance <> 0  ORDER BY g.ac_g_catid ASC") or die(mysqli_error($conn));
    $data = [];
	while($tbl= mysqli_fetch_assoc($trbal)){
	  $data[]=$tbl;
	}
$inacc= mysqli_query($conn,"SELECT co_name,owner_name,co_phone,co_email,co_address,co_logo FROM company WHERE coid='$coid' LIMIT 1") or die(mysqli_error($conn));
$inac= mysqli_fetch_assoc($inacc);

$co_name        = $inac['co_name'];
$owner_name     = $inac['owner_name'];
$co_address     = $inac['co_address'];
$co_phone       = $inac['co_phone'];
$co_email       = $inac['co_email'];
$co_logo        = $inac['co_logo'];	
?>
<section class="content">
  <div class="row">
    <div class="col-md-12">
      <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
        <div class="card-header" style="padding: 3px 8px;">
          <h3 class="card-title">Trail Balance View</h3>
          <div class="card-tools">
            <span id="sdf"></span>
            <button type="button" onclick="pdfMe('#trbalance');" class="btn btn-tool" title="Download Pdf"><i class="fas fa-download"></i></button>
            <button type="button" class="btn btn-tool" onclick="printMe('#trbalance')" title="Print Now"><i class="fas fa-print"></i></button>
            <button type="button" class="btn btn-tool" data-card-widget="remove">
              <i class="fas fa-times"></i>
            </button>
          </div>
        </div>    	
            <div class="card-body p-1" id="trbalance">
                       <input type="hidden" id="vounameset" value="Trail Balance Report <?php echo date('d/M/y', strtotime($fdate));?> To <?php echo date('d/M/y', strtotime($edate));?> ">

              <table id="trbl_table" class="ttable" cellpadding="0" cellspacing="0">
                 <thead>
                    <tr>                  	
                       <th rowspan="3" colspan="2">
                        <div style="width:12%;float:left;">
                            <?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:77px;">
                            <?php else : ?>
                                <img class="profile-user-img img-fluid img-circle  " src="../../dist/img/a.png" style="width:80px;height:77px;"></div>
                            <?php endif; ?>
                      </div>
                         <div style="padding-left:2px;float:left;">
                           <sss style="font-size:18px;font-weight: 600;"><?php echo $co_name;?></sss><br>
                           <sss><?php echo $owner_name;?></sss><br>
                           <sss><?php echo $co_address;?></sss><br>
                           <sss><?php echo $co_phone;?></sss> # 
                           <sss><?php echo $co_email;?></sss>
                         </div>
                       </th> 
                       <th colspan="2" class="text-center" style="font-size:13px;">All Branch</th>
                    </tr> 
                    <tr>                   	
                       <th colspan="2" class="text-center" style="font-size:13px;">Trail Balance</th> 
                    </tr>
                    <tr>                   	
                       <th colspan="2" class="text-center" style="font-size:13px;"><?php echo date('d/m/y', strtotime($fdate));?> To <?php echo date('d/m/y', strtotime($edate));?></th>
                    </tr>                                     	
                    <tr class="sticky-top">
                       <th class="text-center" style="width:5%;">S/l</th>
                       <th style="width:65%;">Particulars</th>
                       <th class="text-center" style="width:10%;">Debit</th>
                       <th class="text-center" style="width:10%;">Credit</th>
                    </tr>
                 </thead>
                 <tbody id="sdsd"></tbody>
                 <tfoot>                               	
                    <tr>
                       <th class="text-right" colspan="2">Closing Balance</th>
                       <th class="text-right"><dt id="sumdrb"></dt></th>
                       <th class="text-right"><dt id="sumcrb"></dt></th>
                    </tr>
                 </tfoot>
              </table>
            </div>
           </div>
        </div>

   </div>
</section>
<script id="pscript">
	$(document).ready(function () {
			var data = '';
			var i = 1;
      var debitt = '';
      var creditt = '';
			var sumdrb = 0;
			var sumcrb = 0;
			$.each(<?php echo json_encode($data)?>, function (key, value) {

			if (Number(value.balance)>=0) {
				debitt = Number(value.balance);
        sumdrb += Number(value.balance);
			} else {
				creditt = Number(-value.balance);
        sumcrb += Number(-value.balance);
			}		

      debitt = (Number(value.balance) < 0)?'':bden.format(Number(debitt));
      creditt = (Number(value.balance)>=0)?'':bden.format(Number(creditt));      	

				data += '<tr>';
				data += '<td class="text-center">' + i++ + '</td>';

				data += '<td onclick="sbtbl(this)" gcatid="'+value.ac_g_catid+'"><div data-toggle="collapse" href="#sbtbl'+value.ac_g_catid+'">' + value.ac_g_cat_name +'<i class="fas fa-caret-left float-right"></i><div class="collapse" id="sbtbl'+value.ac_g_catid+'"></div></div></td>';

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

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

				data += '</tr>';
			});
			
			$('#trbl_table #sdsd').append(data);
			$('#trbl_table #sumdrb').text(bden.format(sumdrb));
			$('#trbl_table #sumcrb').text(bden.format(sumcrb));
      $('#pscript').remove();
        toastr.success(Showing, yourdata);
	});
</script>

<script>
 function sbtbl(sid) {
  var gcatid = $(sid).attr('gcatid');
  var viewid = $(sid).find("#sbtbl"+gcatid);
     $(".preloader").css('display','flex');
     $(viewid).load("../display_report/sub_table.php",
     ({
        gcatid: gcatid,
        gbid:'<?php echo $gbid;?>',
        gbrid:'<?php echo $gbrid;?>',
        fdate:'<?php echo $fdate;?>',
        edate:'<?php echo $edate;?>'
    }),
    function() {
     $('.preloader').hide(); 
     // $(sid).prop('onclick','false'); 
     $(sid).removeAttr("onclick");
   });
 }
</script>

<!-- <script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        url: "../accounts/data.php",
        dataType: 'json',
        type: 'get',
        delay: 3000,
        cache:false,
        success: function(data){
            /*console.log(data);*/
            var event_data = '';
            $.each(data, function(index, value){
                /*console.log(value);*/
                event_data += '<tr>';
                event_data += '<td>'+value.acid+'</td>';
                event_data += '<td>'+value.opdate+'</td>';
                event_data += '<td>'+value.address+'</td>';
                event_data += '<td>'+value.acname+'</td>';
                event_data += '<td>'+value.balance+'</td>';
                event_data += '<td>'+value.ocname+'</td>';
                event_data += '<td>'+value.brid+'</td>';
                event_data += '<td>'+value.bid+'</td>';
                event_data += '</tr>';
            });
            $('#example1 tbody').append(event_data);
        },
        error: function(d){
            /*console.log("error");*/
            alert("404. Please wait until the File is Loaded.");
        }
    });
});
</script> -->
<!-- acid,acname,ocname,address,balance,opdate,brid,bid -->