<?php require_once('../auth/auth.php');?>
<?php 
   $co_rs = mysqli_query($conn,"SELECT co_name,owner_name,co_address,co_phone,co_email FROM company WHERE coid='$coid'") or die (mysqli_error($conn));
   $cor= mysqli_fetch_assoc($co_rs);

if(isset($_GET['bbrid'])){
  $bbrid = $_GET['bbrid'];
  if($bbrid==0){
     $brid_con = '';
     $branch_name = 'ALL BRANCH';
    } else {
     $brid_con = "AND us_brid='$bbrid'";
     $branch_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT branch_name FROM branch WHERE coid='$coid' AND brid='$bbrid'"))['branch_name'];
    }
} else {
  $brid_con = '';
  $branch_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT branch_name FROM branch WHERE coid='$coid' AND brid='$brid'"))['branch_name'];
}
?>
<section class="content">
 <div class="container-fluid">
  <div class="row">
    <div class="col-md-2"></div>
    <div class="col-md-8">
      <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"><?php echo language(336);?></h3>
          <div class="card-tools">
            <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">
               <table class="ttable border-bottom-0" id="header" cellpadding="0" cellspacing="0">
                  <thead>
                     <tr>
                        <th rowspan="3" class="thborder border-bottom-0 text-center w-15">
                           <img class="profile-user-img img-fluid img-circle" src="../../dist/img/a.png" style="width:80px;height:77px;">
                        </th>
                        <th rowspan="3" class="thborder border-bottom-0 w-60">
                              <dt style="font-size:18px;font-weight: 600;"><?php echo $cor['co_name'];?></dt>
                              <dt><?php echo $cor['owner_name'];?></dt>
                              <dt><?php echo $cor['co_address'];?></dt>
                              <dt><?php echo $cor['co_phone'];?>#<?php echo $cor['co_email'];?></dt>
                        </th>
                        <th class="thborder text-center w-25"><?php echo $branch_name;?></th>
                     </tr>
                     <tr>
                        <th class="thborder text-center"><?php echo language(336);?></th>
                     </tr>
                     <tr>
                        <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($date));?>
                        </th>
                     </tr>
                  </thead>
               </table>
              <table class="ttable">
              <thead>
                  <tr class="sticky-top">
                      <th class="text-center w-5"><?php echo language(15);?></th>
                      <th class="text-center w-20"><?php echo language(337);?></th>
                      <th class="text-center w-10"><?php echo language(297);?></th>
                      <th class="text-center w-10"><?php echo language(298);?></th>
                      <th class="text-center w-10"><?php echo language(299);?></th>
                      <th class="text-center w-10"><?php echo language(300);?></th>
                      <th class="text-center w-10"><?php echo language(338);?></th>
                  </tr>
              </thead>
               <tbody>
                <?php
                  $user_q = mysqli_query($conn,"SELECT us.uid,em_acname,
                    ((SELECT (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))) FROM transaction t INNER JOIN accounts a ON (t.dr_acid=a.acid || t.cr_acid=a.acid) WHERE t.coid='$coid' AND t.active=1 AND (t.dr_acid=a.acid || t.cr_acid=a.acid) AND a.ac_type='cash' AND t.userid=us.uid)-(COALESCE((SELECT SUM(cash_amount) FROM cash_zero WHERE cash_zero.coid='$coid' AND cash_zero.uid=us.uid),0))) cash_balance,
                    ((SELECT (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))) FROM transaction t INNER JOIN accounts a ON (t.dr_acid=a.acid || t.cr_acid=a.acid) INNER JOIN ac_sub_g_category g ON (a.gcatid=g.ac_g_catid) WHERE t.coid='$coid' AND t.active=1 AND (t.dr_acid=a.acid || t.cr_acid=a.acid) AND g.g_type='bank' AND t.userid=us.uid)-(COALESCE((SELECT SUM(bank_amount) FROM cash_zero WHERE cash_zero.coid='$coid' AND cash_zero.uid=us.uid),0))) bank_balance,
                    (SELECT sum(amount) FROM transaction WHERE ttype='R' AND date(date) LIKE '$date' AND active=1 AND userid=us.uid) rec_amount,
                    (SELECT sum(amount) FROM transaction WHERE ttype='P' AND date(date) LIKE '$date' AND active=1 AND userid=us.uid) pay_amount FROM user us INNER JOIN em_employee em ON (us.us_em_id=em.em_id) WHERE us.us_coid='$coid' $brid_con AND us.default_set IS NULL HAVING (cash_balance>0||bank_balance>0||rec_amount>0||pay_amount>0)") or die (mysqli_error($conn));
                   $i=1;
                   while($cor= mysqli_fetch_assoc($user_q)){                   
                ?>                
                 <tr>
                   <td class="text-center"><?php echo $i++;?></td>
                   <td><?php echo $cor['em_acname'];?></td>
                   <td class="text-right"><input type="number" class="form-control text-center form-control-sm inputselect cash_amount" maxc="<?php echo @$cor['cash_balance'];?>" value="<?php echo @$cor['cash_balance'];?>"></td>
                   <td class="text-right"><input type="number" class="form-control text-center form-control-sm inputselect bank_amount" maxc="<?php echo @$cor['bank_balance'];?>" value="<?php echo @$cor['bank_balance'];?>"></td>
                   <td class="text-right"><?php echo @number_format($cor['rec_amount'],$decimel);?></td>
                   <td class="text-right"><?php echo @number_format($cor['pay_amount'],$decimel);?></td>
                   <td class="text-center"><button class="btn btn-primary btn-xs" uid="<?php echo $cor['uid'];?>" rec_amount="<?php echo $cor['rec_amount'];?>" pay_amount="<?php echo $cor['pay_amount'];?>"><?php echo language(338);?></button></td>
                 </tr>
               <?php } ?>
               </tbody>  
              </table>
          </div>
       </div> 
     </div>
     <div class="col-md-2"></div>
   </div>
 </div>
</section>


<script>
$(document).ready(function() {
$('[maxc]').on('input', function(e){
    e.preventDefault(); 
    let maxc = Number($(this).attr('maxc'));
    let amount = Number($(this).val());
    if (maxc < amount) {
      $(this).parent().parent().find('button').prop('disabled', true);
    } else {
      $(this).parent().parent().find('button').prop('disabled', false);
    }
  });
});

$(document).ready(function() {
$('[uid]').on('click', function(e){
    e.preventDefault(); 
    let targt = $(this);
    targt.prop('disabled', true);
    $.post("../voucher/cash_zero_db.php",
      {
        guid        : targt.attr('uid'),
        cash_amount : targt.parent().parent().find('.cash_amount').val(),
        bank_amount : targt.parent().parent().find('.bank_amount').val(),
        rec_amount  : targt.attr('rec_amount'),
        pay_amount  : targt.attr('pay_amount')
      },
      function(data){
       toastr.success(Successfully, yourdata);
       targt.parent().parent().fadeOut('slow');
    });
  });
});  
</script>