<?php
 if(ISSET($_POST['update_PAY'])){
     try{
        $pymnt_id = $_REQUEST['pymnt_id'];
        $payment_amount = $_REQUEST['payment_amount'];
        $refno = $_REQUEST['refno'];
        $pymtDate = $_REQUEST['pymtDate'];
        $cmtpy = $_REQUEST['cmtpy'];                
         
        $sql = $db->prepare("UPDATE `tbl_payments` SET `txidRef`='$refno', `payMont` = '$payment_amount', `pymtDate` = '$pymtDate', `cmtpy` = '$cmtpy' WHERE `pymtid` = '$pymnt_id'");
        $sql->execute();
        
        $msg = "Updated Successfully";
        echo'<meta http-equiv="refresh"'.'content="2;URL=gate?b=recv">';
         
     }catch(PDOException $e){
         echo $e->getMessage();
     }
 }
?>   
     
 <?php
     if(isset($_GET['del_pay_id'])){
     $paymnt_id = $_GET['del_pay_id'];
     $sql = $db->prepare("UPDATE tbl_payments SET pmtSts=7 WHERE `pymtid`='$paymnt_id' ");
     $sql->execute();
     
     $msge = "Data Deleted!";
     echo'<meta http-equiv="refresh"'.'content="2;URL=gate?b=recv">';
     }
 ?>
<?php
    if(!empty($msg)) {?>
<div class="alert alert-success">
	<button type="button" class="close" data-dismiss="alert">×</button>
	<strong>Well done!</strong> <?php echo $msg; ?> 
</div>
<?php
  }
   else if(!empty($msge)) {
?>
<div class="alert alert-error">
	<button type="button" class="close" data-dismiss="alert">×</button>
	<strong>Oh snap!</strong>  <?php echo $msge; ?> 
</div>
<?php
   }
?>
<div class="table-responsive">
	 <table id="dt-basic-example" class="table table-hover table-striped w-100" style="font-size: 11px;">
		<thead class="header">
			<tr>
			    <th style="font-weight: bold;"></th>
                <th style="font-weight: bold;">CLIENT</th>
                <th style="font-weight: bold;">PAYMENT DATE</th>
                <th style="font-weight: bold;">PROJECT</th>
                <th style="font-weight: bold;">COMMENT</th>
                <th style="font-weight: bold;">BANK ACCOUNT</th>
                <th style="font-weight: bold;">REF</th>
                <th class="text-right" style="font-weight: bold;">PAID AMOUNT</th>
                <th style="font-weight: bold;" width="15%"></th>
			</tr>
		</thead>
		<tbody>
		 <?php
		    $i=0;
            $tot_PAY=0;
            $sth = $db->prepare("SELECT * FROM tbl_payments py
            INNER JOIN tbl_clients c ON c.ct_id =py.pmtClnt  
            INNER JOIN tbl_projects p ON p.projid=py.projPay
            INNER JOIN tbl_bank_accounts b ON b.bank_id=py.bankid
            WHERE pmtSts=1 AND paytpe=1 ORDER BY pymtid DESC");
            $sth->execute();
            while($ff = $sth->fetch()){
            $tot_PAY=$tot_PAY+$ff['payMont'];
            $i++;
        ?>
		<tr class="btr">
		    <td><?php echo $i;?></td>
            <td><?php echo $ff['ct_names']; ?></td>
            <td><?php echo date('d/m/Y',strtotime($ff['pymtDate']));?></td>            
            <td><?php echo $ff['projname']; ?></td>
            <td><?php echo $ff['cmtpy']; ?></td>
            <td><?php echo $ff['bank_name']; ?></td>
            <td><?php echo $ff['txidRef']; ?></td>
            <td class="text-right"><?php echo number_format($ff['payMont']).".00"; ?></td>
            <td class="cell-icon text-center">
                <a href="#" data-toggle="modal" data-target="#update<?php echo $ff['pymtid'];?>" class="btn btn-default btn-xs"><i class="fa fa-edit"></i>Edit</a>|
                <a href="gate?b=recv&del_pay_id=<?php echo $ff['pymtid'];?>" class="btn btn-default btn-xs" onclick="if(!confirm('Do you really want to Delete This invoice?'))return false;else return true;">
                <i class="fa fa-times-circle"></i> Cancel</span></a>
            </td>
       </tr>
       
        <div class="modal fade bs-example-modal-sm" id="update<?php echo $ff['pymtid']; ?>" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
               <div class="modal-dialog modal-sm">
                  <div class="modal-content">
                   <div class="modal-header header">
                    <h5 class="modal-title" id="exampleModalLabel"><b>Change payment for <?php echo $ff['ct_names']; ?></b></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                   
                       <div class="modal-body">
                          <form id="form_validation" method="POST" action="">
                              <input type="hidden" name="pymnt_id" value="<?php echo $ff['pymtid'];?>"/>
                             
                             <div class="form-group form-float">
                                 <label class="form-label">Amount</label>
                                 <div class="form-line">
                                    <input type="text" class="form-control" value="<?php echo $ff['payMont'];?>" name="payment_amount" required style="width: 100%;">
                                 </div>
                             </div>

                             <div class="form-group form-float">
                                 <label class="form-label">Ref.</label>
                                 <div class="form-line">
                                    <input type="text" class="form-control" value="<?php echo $ff['txidRef']; ?>" name="refno" required style="width: 100%;">
                                 </div>
                             </div>

                             <div class="form-group form-float">
                                 <label class="form-label">Paid Due Date</label>
                                 <div class="form-line">
                                    <input type="date" class="form-control" value="<?php echo $ff['pymtDate']?>" name="pymtDate" required style="width: 100%;">
                                 </div>
                             </div>

                             <div class="form-group form-float">
                                 <label class="form-label"> Comment </label>
                                 <div class="form-line">
                                    <textarea type="text" class="form-control" name="cmtpy" required style="width: 100%;"><?php echo $ff['cmtpy']?></textarea>
                                 </div>
                             </div>
                         </div>
                       <div class="modal-footer">
                     <button type="submit" name="update_PAY" class="btn btn-primary">Save changes</button>
                   </div>
                 </form>
                 
                <?php
                    }
                ?>
              </div>
           </div>
         </div>
         
		 </tbody>
		 
	  <tfoot>
        <tr>  
          <td style="font-weight: bold;">Tot.</td>
          <td colspan="5" ></td>
          <td></td>
          <td style="font-weight: bold;" align="right"><?php echo number_format($tot_PAY).".00";?></td>
          <td></td>
        </tr>
      </tfoot>
	</table>
 </div>