<?php 
 $cc=0;
 $tpayb=0;     
 $tptd=0;
 $tbal=0;
 $status="NOT PAID";
 require_once "labClass.php";
 $con = new labClass();
  ?>
<div class="container-fluid" id="co"> 
<div  class="mt-4 p-5 bg-white text-dark rounded"> 
<table class="table">
<tr><td colspan="6" align="center"><small>REPORTS OF CUSTOMERS' BY DEBITS:</small></td></tr>
<tr><td align="right">
<label for="cname" class="form-label">Customer name:</label>   
</td>
<td colspan="2">    
<input class="form-control" list="cnames" name="cname" id="cname" onchange="processData();">
<datalist id="cnames">
<?php
 $Q= $con->getOrderListBcN();
 if ($Q->rowCount()>0) {
 $lb=$Q->fetchAll();
     foreach ($lb as $key => $l){
      ?>
      <option value="<?php echo  $l["cname"];?>">  
             <?php
           }
           }
      ?></datalist>
    </td>
    <td >Pay by:</td><td width="20%"><select class="form-control" name="pOpt" id="pOpt">
    <option value="">Select</option>
    <option value="CASH">CASH</option>
    <option value="M-PESA">M-PESA</option>
    <option value="AIRTEL MONEY">AIRTEL MONEY</option>
    <option value="TIGO PESA">TIGO PESA</option>
    <option value="HALO PESA">HALO PESA</option>
    <option value="NMB">NMB</option>
    <option value="CRDB">CRDB</option>
    <option value=""></option> 
    </select></td>
  </tr>
  <tr>
  <td style="width:15%;">Date between:</td><td style="width:10%;" ><input type="date" style="width:200px;" class="form-control" id="dat1" name="dat1"  value="<?php echo Date("Y-m-d");?>" >
  </td>
  <td>and:</td><td style="width:10%;">
  <input type="date" style="width:200px;" class="form-control" id="dat2" name="dat2" value="<?php echo Date("Y-m-d");?>">
  </td><td style="width:10%;">  
  <button type="submit" class="btn btn-primary" style="width:100%; height:40px;" onclick="var cname=$('#cname').val(); var dat1=$('#dat1').val(); var dat2=$('#dat2').val();  $.post('pharmSortCusDebit.php',{dat1:dat1,dat2:dat2,cname:cname},function(data){$('#dpl').html(data).show();});">Generate</button>
 </td>      
 </tr>
 </table>
 <div class="container-fluid " id="dpl">
 <?php
 $chkp= $con->getThisCusDebits($status); 
 if ($chkp->rowCount()>0){
  $chp=$chkp->fetchAll();
  ?>
<table class="table" border="1"> 
<thead class="bg-secondary" >
    <th>  
      <small><h6>DATE</h6></small>
    </th>
    <th>  
      <small><h6>INV. NO/C'S NAME</h6></small>
    </th>
    <th>  
      <small><h6>TOTAL PAYABLE</h6></small>
    </th>
    <th>  
      <small><h6>PAID TO DATE</h6></small>
    </th>
    <th>  
      <small><h6>BALANCE</h6></small>
    </th>
    <th>  
      <small><h6>PAY</h6></small>
    </th>
</thead>
<?php
foreach ($chp as $key => $p) {
$orderID=$p['orderID'];
$cp= $con->getThisInvItemList($orderID);
$cc++; 
$nr=$cp->rowCount();
?>
 <tbody>
 <tr>
 <td>
    <?php echo $p['orderdate'];?> 
 </td>
 <td>  
 <a href="#" onclick="var orno=document.getElementById('orno<?php echo $cc;?>').innerHTML; $.post('pharmInvDetail.php',{orno:orno},function(data){$('#dpl').html(data).show();});"><sup id="orno<?php echo $cc;?>"><?php echo $p['orderID'];?></sup></a>  <a href="#" onclick="var cname=document.getElementById('cname<?php echo $cc;?>').innerHTML; $.post('cusdebtDetail.php',{cname:cname},function(data){$('#dpl').html(data).show();});"><sup id="cname<?php echo $cc;?>"><?php $parts = explode('(', $p['cname']); echo $parts[0];?></sup></a> 
</td>
<td align="right">
    <?php echo number_format($p['payableA'],2);?> 
</td>
<td align="right">
    <?php echo number_format($p['paidtodate'],2);?> 
</td>
<td align="right">
<sup class="text-danger" id="rdn<?php echo $cc;?>"></sup>
<input type="hidden" disabled="true" name="bal<?php echo $cc;?>" id="cbal<?php echo $cc;?>"  value="<?php echo $p['balance'];?>" />
<input type="number" align="right" style="width:70%; border-radius:12px; border-color:white; background-color: white;" disabled="true" name="bal<?php echo $cc;?>" id="bal<?php echo $cc;?>"  value="<?php echo $p['balance'];?>" />  
</td>
<td>
<button type="submit" class="btn btn-primary" style="width:100%; height:40px;"  name="postEdit<?php echo $cc;?>" id="postEdit<?php echo $cc;?>" value="PAY" id="submit" onclick="document.getElementById('bal<?php echo $cc;?>').disabled=false;$('#postEdit<?php echo $cc;?>').hide();$('#postUpdate<?php echo $cc;?>').show();" ><small>PAY</small></button>
        <button type="submit" class="btn btn-success" style="width:100%; height:40px;  display:none;" name="postUpdate<?php echo $cc;?>" id="postUpdate<?php echo $cc;?>"value="Update" id="submit" onclick=" var orno =document.getElementById('orno<?php echo $cc;?>').innerHTML; var sdate=$('#sdate').val(); var user=$('#user').val(); var pOpt=$('#pOpt').val(); var cbal =$('#cbal<?php echo $cc;?>').val();var bal =$('#bal<?php echo $cc;?>').val();alert(pOpt); if(bal!=''  && orno!='' && cbal!='' && user!=''&& pOpt!='') {$.post('pharmPayDebitPro.php',{sdate:sdate,bal:bal,cbal:cbal,orno:orno,pOpt:pOpt,user:user},function(data){$('#rdn<?php echo $cc;?>').html(data).show();});document.getElementById('bal<?php echo $cc;?>').disabled=true;$('#postUpdate<?php echo $cc;?>').hide();$('#postEdit<?php echo $cc;?>').show();} else{document.getElementById('rdn<?php echo $cc;?>').innerHTML='Valide item detail required!..';}" > <small>SAVE</small></button>
    </td>
    </tr>   
<?php
$tbal=$tbal+$p['balance'];
$tpayb=$tpayb+$p['payableA'];
$tptd=$tptd+$p['paidtodate'];      
}
$cc=0; 
?>
<tr><td colspan="2" align="right">Total:</td><td align="right"><?php echo number_format($tpayb,2);?></td><td align="right"><?php echo number_format($tptd,2);?></td><td align="right"><?php echo number_format($tbal,2);?></td></tr>
</tbody>
</table> 

<?php
}
?>
</div>
<script type="text/javascript">
function processData(){ 
var ecc=0;  
var cname= document.getElementById('cname').value;
$.trim(cname);
alert(cname);
var dat1 =document.getElementById('dat1').value; 
var dat2 =document.getElementById('dat2').value;  
  //FIRST NAME VALIDATION
 if(cname==""){ 
         ecc++;
         alert('Customer name required!..');
             }
  if(dat1=="")
             { 
         ecc++;
         alert('Date range is required!..');
             }
  if(dat2=="")
             { 
         ecc++;
         alert('Date range is required!..');
             }
    if(ecc==0)
    { 
        $.post("pharmSortCusDebit.php",
          {cname:cname,dat1:dat1,dat2:dat2},
          function(data){
      $('#dpl').html(data).show();

    });
    }
 
} 

 </script>