<?php require_once('../auth/auth.php'); ?>
<?php
  if(isset($_POST['gcoid'])){
    $gcoid =  $_POST['gcoid'];
    $fdate =  $_POST['fdate'];
    $edate =  $_POST['edate'];
    
    if($gcoid>0){
      $comcon = "b.coid = '$gcoid' AND";
    } else {
      $comcon = "";
    }

 $adbl = mysqli_query($conn,"SELECT DATE_FORMAT(date, '%d/%m/%Y') date,details,pay_method,send_gsm,tnxid,(COALESCE(CASE WHEN plus = '+' THEN amount END,'')) debit,(COALESCE(CASE WHEN plus = '-' THEN amount END,'')) credit,co_name FROM add_balance b LEFT JOIN company c ON (b.coid=c.coid) WHERE $comcon b.status=1 AND date(date) BETWEEN '$fdate' AND '$edate' ORDER BY date(date)") or die(mysqli_error($conn));
    $sdata = [];
  while($sldg= mysqli_fetch_assoc($adbl)){
    $sdata[]=$sldg;
  }

    $company_query= mysqli_query($conn,"SELECT co_name,owner_name,co_phone,co_email,co_address,co_logo FROM company WHERE coid=(IF($gcoid>0,'$gcoid',1))") or die (mysql_error());
    $inac= mysqli_fetch_assoc($company_query);
    $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="container-fluid">
  <div class="row">
    <div class="col-md-1"></div>
    <div class="col-md-10">
      <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"> Balance Statment </h3>
          <div class="card-tools">
            <button type="button" class="btn btn-tool" onclick="ExcelMe('#sttable<?php echo $gcoid;?>')"  title="Download Excel">
              <i class="far fa-file-excel"></i>
            </button>
            <button type="button" onclick="BfpdfMe('#sttable<?php echo $gcoid;?>');" class="btn btn-tool" title="Download Pdf"><i class="fas fa-download"></i></button>
            <button type="button" class="btn btn-tool" onclick="printMe('#b_statment')" title="Print Now"><i class="fas fa-print"></i></button>
            <button type="button" class="btn btn-tool" data-card-widget="remove" data-dismiss="modal">
              <i class="fas fa-times"></i>
            </button>
          </div>
        </div>      
            <div class="card-body p-1 table-responsive cscroll" id="b_statment" style="max-height: 450px;">
              <table id="sttable<?php echo $gcoid;?>" class="ttable" cellpadding="0" cellspacing="0">
                 <thead>
                    <tr >
                       <th colspan="2" rowspan="3" class="text-center">
                        <?php if($co_logo != ""): ?>
                        <img id="img_data" class="profile-user-img img-fluid img-circle" style="width:80px;height:77px;" src="data:image/gif;base64,<?php echo $co_logo;?>" alt="User profile picture">
                        <?php else : ?>
                        <img id="img_data" class="profile-user-img img-fluid img-circle" style="width:80px;height:77px;" src="../../dist/img/no_img.jpg" alt="company profile">
                        <?php endif; ?>
                      </th>                    
                       <th colspan="4" rowspan="3">
                         <div style="padding-left:2px;">
                          <sss style="font-size:18px;font-weight: 600;"><?php echo $co_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="3" class="text-center text-sm"><?php if($gcoid>0){echo $co_name;}else{echo 'All Company';}?></th>
                    </tr> 
                    <tr>                    
                       <th colspan="3" class="text-center text-sm">Balance Statment</th> 
                    </tr>
                    <tr>                    
                       <th colspan="3" class="text-center"><?php echo date('d/m/y', strtotime($fdate));?> <?php echo language(291);?> <?php echo date('d/m/y', strtotime($edate));?></th>
                    </tr>                                       
                    <tr class="sticky-top" style="background: hsl(0deg 0% 50% / 42%);">
                       <th class="text-center" style="width:3%;"> <?php echo language(15);?></th>
                       <th class="text-center" style="width:9%;"><?php echo language(131);?></th>
                       <th class="text-center" style="width:15%;"><?php echo language(193);?></th>
                       <th class="text-center" style="width:30%;"><?php echo language(295);?></th>
                       <th class="text-center" style="width:10%;"><?php echo language(207);?></th>
                       <th class="text-center" style="width:10%;"><?php echo language(289);?></th>
                       <th class="text-center" style="width:8%;"><?php echo language(217);?></th>
                       <th class="text-center" style="width:8%;"><?php echo language(218);?></th>
                       <th class="text-center" style="width:8%;"><?php echo language(290);?></th>
                    </tr>
                 </thead>
                 <tbody>
                    <tr>
                    <?php
                         $pr_dr='';
                         $pr_cr='';
                         $pr_balance = 0
                         
                        //   @$prbl = mysqli_query($conn,"SELECT (COALESCE(CASE WHEN plus = '+' THEN amount END,''))-(COALESCE(CASE WHEN plus = '-' THEN amount END,'')) pr_balance FROM add_balance WHERE $comcon AND status=1 AND date(date) = '$fdate' GROUP BY coid") or die(mysqli_error($conn));
                        // if(@$prbl){
                        //    $pr_balance= mysqli_fetch_assoc($prbl)['pr_balance'];

                        //    if ($pr_balance >= 0) {
                        //      $pr_dr = $pr_balance;
                        //   } else {
                        //      $pr_cr = (- $pr_balance);
                        //   }
                        // }
                    ?>
                       <td class="text-right" colspan="6" style="border-top:0;"><dt> <?php echo language(129);?> </dt></td>
                       <td class="text-right" style="border-top:0;"><dt><?php if ($pr_dr>0) {echo number_format($pr_dr,$decimel);}?></dt></td>
                       <td class="text-right" style="border-top:0;"><dt><?php if ($pr_cr>0) {echo number_format($pr_cr,$decimel);}?></dt></td>
                       <td class="text-right" style="border-top:0;"><dt><?php echo number_format($pr_balance,$decimel);?></dt></td>
                    </tr>
                 </tbody>
                 <tfoot> 
                    <tr class="sticky-bottom">
                       <td class="text-right" colspan="6"><dt><?php echo language(292);?></dt></td>
                       <td class="text-right"><dt id="sumdrs"></dt></td>
                       <td class="text-right"><dt id="sumcrs"></dt></td>
                       <td class="text-right"><dt id="balances"></dt></td>
                    </tr>                                 
                    <tr class="sticky-bottom">
                       <th class="text-right" colspan="6"> <?php echo language(293);?></th>
                       <th class="text-right"><dt id="drbalances"></dt></th>
                       <th class="text-right"><dt id="crbalances"></dt></th>
                       <th class="text-right"></th>
                    </tr>
                 </tfoot>
              </table>
            </div>
           </div>
        </div>
      <div class="col-md-1"></div>
   </div>
  </div>
</section>
<script id="pscripts">
  $(document).ready(function () {
      let data = '';
      let i = 1;
      let balance = Number(<?php echo $pr_balance;?>);
      let sumdr = 0;
      let sumcr = 0;
      let type = '';
      $('#sumdrs').text(bden.format(sumdr));
      $('#sumcrs').text(bden.format(sumcr));
        if (balance>0) {
            $('#drbalances').text(bden.format(balance));
          } else if(balance<0){
            $('#crbalances').text(bden.format(- balance));
          }

      $.each(<?php echo json_encode($sdata)?>, function (key, value) {

      if (Number(value.debit)>0) {
        balance += Number(value.debit);
      } else if(value.credit>0){
        balance -= Number(value.credit);
      }
              
            sumdr += (Number(value.debit)>0)?Number(value.debit):0;
            sumcr += (Number(value.credit)>0)?Number(value.credit):0;

      var debitt = (value.debit==''|| value.debit==0)?'':bden.format(Number(value.debit));
      var creditt = (value.credit==''|| value.credit==0)?'':bden.format(Number(value.credit));       

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

        data += '<td class="text-center">' + value.date + '</td>';

        data += '<td>' + value.co_name + '</td>';

        data += '<td>' + value.details + ', by '+ value.send_gsm + '</td>';

        data += '<td class="text-center">' + value.pay_method + '</td>';

        data += '<td class="text-center">' + value.tnxid + '</td>';

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

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

        data += '<td class="text-right"><b>' + bden.format(balance) + '</b></td>';

        data += '</tr>';
      });
      
      $('#sttable<?php echo $gcoid;?> tbody').append(data);
      $('#sttable<?php echo $gcoid;?> #sumdrs').text(bden.format(sumdr));
      $('#sttable<?php echo $gcoid;?> #sumcrs').text(bden.format(sumcr));
      $('#sttable<?php echo $gcoid;?> #balances').text(bden.format(balance));

        if (balance>0) {
          $('#sttable<?php echo $gcoid;?> #drbalances').text(bden.format(balance));
          $('#sttable<?php echo $gcoid;?> #crbalances').text('');
        } else if(balance<0){
          $('#sttable<?php echo $gcoid;?> #crbalances').text(bden.format(- balance));
          $('#sttable<?php echo $gcoid;?> #drbalances').text('');
        } else {
          $('#sttable<?php echo $gcoid;?> #drbalances,#sttable<?php echo $gcoid;?> #crbalances').text('');
        }

      $('#pscripts').remove();
        toastr.success(Showing, yourdata);
  });
</script>
