<?php
session_start();
include "../config/config.php";
$id = $_GET['id'];
?>
<style>
    thead{
        background: #dbcbcb;
    }
    table.table tbody tr:hover td {
        background: none !important;
    }
</style>

<?php


    $statement = $pdo->prepare("SELECT ad.*, c.customer_name, c.mobile_no,
p.hsn_code, pro.name as products_name, 
cu.name as created_user, uu.name as updated_user
FROM `tbl_advance_voucher` ad 
LEFT JOIN tbl_customers c ON c.id=ad.customer_id
LEFT JOIN tbl_product_variants p ON p.id=ad.product_variant_id
LEFT JOIN tbl_products_name pro ON pro.id=p.product_name
LEFT JOIN tbl_users cu ON cu.id=ad.created_by
LEFT JOIN tbl_users uu ON uu.id=ad.updated_by
WHERE ad.id=?");


    $statement->execute(array($id));
    $emp_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    
    
    if($totalData==1){
        foreach($emp_data as $data){
            
            $advance_id = $data['id'];
            $customer_name = $data['customer_name'];
            $mobile_no = $data['mobile_no'];
            $customer_id = $data['customer_id'];
            
        $date = $data['date'];
        $expected_date = $data['expected_date'];
        
        $receipt_id = $data['receipt_id'];
        $advance_no = $data['advance_no'];
        
        $created_user = $data['created_user'];
        $updated_user = $data['updated_user'];
        $products_name= $data['products_name'];
        $hsn_code = $data['hsn_code'];
        
        $advance_amount = $data['advance_amount'];
        	
        	
        }
    }
?>

<div class="modal-dialog" role="document" id="add_modal" style="width: 900px;">
    <div class="modal-content" id="printableArea">
      <div class="modal-header">
            <h4 class="modal-title" id="exampleModalLabel">
                <b>Advance Voucher Details</b>
                <!--<a class="btn btn-primary" href='modal/purchase-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;">Advance Total Amount: <b><?=number_format($advance_amount,2)?></b> </span>
                <!--<span class="btn btn-danger" style="padding: 3px 10px 3px 10px;">Scheme Balance 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">
    
    
    <tr>
        <th>Customer Name</th>
        <td style="vertical-align: top;"><?=$customer_name?></td>
        <th>Customer Mobile No</th>
        <td style="vertical-align: top;"><?=$mobile_no?></td>
    </tr>
    
   
    
    <tr>
        <th style="width: 20%;">Advance Date</th>
        <td style="width: 30%; background: none !important;"><?=date('d-m-Y', strtotime($date))?></td>
        <th style="width: 20%;">Purchase Date</th>
        <td style="width: 30%;"><?=date('d-m-Y', strtotime($expected_date))?></td>
    </tr>
    
    
     <tr>
        <th>Receipt ID</th>
        <td style="vertical-align: top;"><?=$receipt_id?></td>
        <th>Advance No</th>
        <td style="vertical-align: top;"><?=$advance_no?></td>
    </tr>
    
    <tr>
        <th>Product Name</th>
        <td style="vertical-align: top;"><?=$products_name?></td>
        <th>Product HSN Code</th>
        <td style="vertical-align: top;"><?=$hsn_code?></td>
    </tr>
   
</table>

</div>
 
<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">Date</th>
      <th scope="col">Amount</th>
      <th scope="col">Payment Type</th>
      <th scope="col">Reference</th>
      <th scope="col">Notes</th>
      <th scope="col">Created At</th>
      <th scope="col">Actions</th>
    </tr>
  </thead>
  <tbody>
      
<?php
    $statement = $pdo->prepare("SELECT p.*, tbl_payment_type.name as payment_type_name
    FROM `tbl_payments` p 
    LEFT JOIN tbl_payment_type ON tbl_payment_type.id=p.payment_type
    WHERE p.accounts=? and p.advance_id=? and p.customer_id=?");
    $statement->execute(array(4, $advance_id, $customer_id));
    $data_results = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    if($totalData>0){
        $i=1;
        foreach($data_results as $res){
            
           
            
            
            
            $actions='';
            
            $actions.="<button style='padding: 7px;margin-right: 3px;'  title='Delete Payment' onClick='delete_Payments(".$res["id"].")' class='tip btn btn-danger btn-xs'>
		                    <i class='fa fa-trash-o'></i>
		                </button>";
            
?>
            
            <tr>
              <td style="text-align: center;"><?=$i++?></td>
              <td style="text-align: center;"><?=date('d-m-Y', strtotime($res['date']))?></td>
              <td style="text-align: center; padding-right: 35px;">
                  <b><?=number_format($res['amount'],2)?></b>
              </td>
              <td style="text-align: center;">
                  <?=$res['payment_type_name']?>
              </td>
              <td style="text-align: center;">
                  <?=$res['reference_no']?>
              </td>
              <td style="text-align: center;">
                  <?=$res['notes']?>
              </td>
              <td style="text-align: center;">
                  <?=date('d-m-Y h:i A', strtotime($res['created_at']))?>
              </td>
              <td style="text-align: center;">
                  <?=$actions?>
              </td>
            </tr>
            
<?php   }
    }else{
        echo "<tr><td colspan='7' style='text-align: center;'>No Payments Found</td></tr>";
    }
?>
  </tbody>
</table>







      
           
            
        </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>
        <!--<button type="button" class="btn btn-success pull-right" onclick="printDiv('printableArea')"><i class="bi bi-printer-fill"></i> Print</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:'hard_delete',delete_id:id, table: 'tbl_payments'},                  
                    success: function(response) {
                        console.log(response); 
                        $("#add_modal .close").click();
                        emp_datas();
                    }
                });
            }
    }
</script>