<?php
session_start();
include "../config/config.php";
$id=$_GET['id'];
?>

<?php
    $statement = $pdo->prepare("SELECT *,(SELECT SUM(amount) FROM tbl_payments WHERE tbl_payments.sale_id=e.id) as paid_total FROM tbl_sales e where e.id='$id'");
    $statement->execute();
    $emp_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    if($totalData==1){
        foreach($emp_data as $data){
            $grand_total = $data['grand_total'];
            $paid_amount = $data['paid_total'];
            
            $current_balance = $grand_total-$paid_amount;
        }
    }
?>

<div class="modal-dialog" role="document" id="add_modal" style="width: 900px;">
    <div class="modal-content">
      <div class="modal-header">
            <h4 class="modal-title" id="exampleModalLabel">
                Payments List
                <!--<a class="btn btn-primary" href='modal/payment-add-modal.php?id=<?=$id?>' id='<?=$id?>' data-toggle='ajax-modal' title='Payment' style="margin-left: 50px;padding: 3px 10px 3px 10px;"><i class="fa fa-inr"></i>  Add Payments</a>-->
                <span class="btn btn-success" style="margin-left: 100px;padding: 3px 10px 3px 10px;">Total Amount: <b><?=number_format($grand_total,2)?></b> </span>
                <span class="btn btn-danger" style="padding: 3px 10px 3px 10px;">Remaining Amount: <b><?=number_format($current_balance,2)?></b> </span>
            </h4>
            
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true"><i class="fa fa-times" style="margin-top: -23px; font-size: 28px;"></i></span>
            </button>
      </div>
      
           
           
      <div class="modal-body">
        <div class="row">
            
            
<div class="col-md-12" style="padding-top: 5px;">
    
<table class="table table-striped">
  <thead>
    <tr>
      <th scope="col">S.No</th>
      <th scope="col">Payment Date</th>
      <th scope="col">Amount</th>
      <th scope="col">Paid By</th>
      <th scope="col">Payment Received</th>
      <th scope="col">Reference</th>
      <th scope="col">Actions</th>
    </tr>
  </thead>
  <tbody>
      
<?php

    $statement = $pdo->prepare("SELECT *,
    (SELECT name FROM tbl_users WHERE p.created_by=tbl_users.id) as payment_created_name,
    (SELECT name FROM tbl_payment_type WHERE p.payment_type=tbl_payment_type.id) as paid_by
    FROM tbl_payments p 
    WHERE p.sale_id=? and p.accounts=?");
    $statement->execute(array($id, 1));
    $emp_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    if($totalData>0){
        $i=1;
        foreach($emp_data as $data){
            $payment_date = $data['date'];
            $payment_amount = $data['amount'];
            $payment_paid_by = $data['paid_by'];
            $reference = $data['reference_no'];
            $created_username = $data['payment_created_name'];
            
            $actions='';
            $actions.="<a style='padding: 7px;margin-right: 3px;' href='modal/payment-edit-modal.php?id=".$data["id"]."' title='Edit Payment' data-toggle='ajax-modal' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a>";
            $actions.="<button style='padding: 7px;margin-right: 3px;'  title='Delete Payment' onClick='delete_Payments(".$data["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
?>
            
            <tr class="Row_<?=$data["id"]?>">
              <td style="text-align: center;"><?=$i++?></td>
              <td style="text-align: center;"><?=date('d-m-Y', strtotime($payment_date))?></td>
              <td style="text-align: center; font-weight: 700; font-size: 15px;"><?=number_format($payment_amount,2)?></td>
              <td style="text-align: center;"><?=$payment_paid_by?></td>
              <td style="text-align: center;"><?=$created_username?></td>
              <td><?=$reference?></td>
              <td style="text-align: center;"><?=$actions?></td>
            </tr>
            
<?php   }
    }else{
        echo "<tr><td colspan='7' style='text-align: center;'>No Payments Record Found</td></tr>";
    }
?>
  </tbody>
</table>
            </div>
           
            
        </div>
      </div>
      
      <div class="modal-footer">
        <button type="button" class="btn btn-danger pull-left" data-dismiss="modal"><i class="fa fa-times"></i> Close</button>
      </div>

    </div>
</div>
<script>
    function delete_Payments(id){
            var result = confirm("Confirm Want to Delete This Payments?");
            if (result) {
                    $.ajax({
                    type: 'POST',
                    url: 'api/delete.php',
                    data: {type:'sales_purchase_delete',delete_id:id},                  
                    success: function(response) {
                        console.log(response); 
                        $("#add_modal .close").click();
                        emp_datas();
                    }
                });
            }
    }
</script>