<?php
if( isset($_POST["dat1"]) && isset($_POST["dat2"]))
{  
 $cc=0;
 $tpayb=0;     
 $tptd=0;
 $tbal=0;
  $cnam= $_POST['cname'];
  $dat1= $_POST['dat1'];
  $dat2= $_POST['dat2'];
  
 $parts = explode(':', $cnam);  
  $cname=$parts[1];
  $status="NOT PAID";
  require_once "labClass.php"; 
 $con = new labClass();
  ?> 
 <div class="container-fluid" id="dpl">
  <?php
 if ($cnam!="" && $dat1==$dat2) 
  {    
   $chkp= $con->getThisCmpnyDebit($cname,$status);//getThisCmpnyPorderByD($cname,$status,$dat1,$dat2);
  } 
   else if ($cnam!="" && $dat1!=$dat2)
   {
   $chkp= $con->getThisCmpnyPorderByD($cname,$status,$dat1,$dat2); 
  }
   else
   {
   $chkp= $con->getThisCompnyPOrderRecBydate($status,$dat1,$dat2);
  }
     
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->getThispOrderItemList($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('pharmCmpnyDbtPayDetail.php',{orno:orno},function(data){$('#dpl').html(data).show();});"><sup id="orno<?php echo $cc;?>"><?php echo $p['orderID'];?></sup></a><?php $parts = explode('(', $p['cname']); echo $parts[0];?> 
     </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('pharmPayCompnyDebitPro.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>
<?php
}
else{
    ?>
<h6 class="text-danger">All field required!..</h6>
<?php
}
?>