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

    $statement = $pdo->prepare("SELECT ad.* FROM tbl_advance_voucher AS ad WHERE ad.id=?");
    $statement->execute(array($id));
    $get_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    if($totalData==1){
        foreach($get_data as $data){
            $total_amount = $data['total_amount'];
            $advance_amount = $data['advance_amount'];
            $per_gram_amount = $data['per_gram_amount'];
        }
    }
?>

<div class="modal-dialog" role="document" style="width:650px;">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="exampleModalLabel">Add Advance Payment</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>
       <form action="" enctype="multipart/form-data" id="add_payment_form" method="post" accept-charset="utf-8">
         
           
      <div class="modal-body">
        <div class="row">
            
            <div class="col-md-12" style="padding-top: 5px;">
                <table style="width: 100%;">
                    <tr>
                        <th style="text-align: left;"><span class="label label-success" style="padding:3px;font-size:15px;"><i class="fa fa-inr"></i> Total Amount: <span id="grand_total"><?=$total_amount?></span></span></th>
                        <th style="text-align: left;"><span class="label label-success" style="padding:3px;font-size:15px;"><i class="fa fa-inr"></i> Advance Amount: <span id="advance_amount"><?=$advance_amount?></span></span></th>
                        <th style="text-align: left;"><span class="label label-danger" style="padding:3px;font-size: 15px;"><i class="fa fa-inr"></i> Remaining Amount: <span id="balance_amount">0.00</span></span></th>
                    </tr>
                    
                </table>
            </div>
            
            
            
            <div class="col-md-6" style="padding-top: 25px;">
                <label>Amount</label>
                <input type="text" name="paid_amount"  placeholder="Enter the Amount" class="form-control" required="required" id="paid_amount" />
            </div>
            <div class="col-md-6" style="padding-top: 25px;">
                <label>Payment Date</label>
                <input type="date" name="payment_date"  value="<?=date("Y-m-d")?>"  class="form-control" id="payment_date" />
            </div>
            
            <!--Hidden Values Starts Here-->
            <input type="hidden" name="advance_id" id="advance_id" value="<?=$id?>" class="form-control" />
            <input type="hidden" name="total_amount" id="total_amount" value="<?=$total_amount?>" class="form-control"/>
            <input type="hidden" name="db_advance" id="db_advance" value="<?=$advance_amount?>" class="form-control"/>
            <input type="hidden" name="new_balance" id="new_balance" value="" class="form-control"/>
            <!--Hidden Values Ends Here-->
            
            

            
            
            <div class="col-md-6" style="padding-top: 8px;">
                <label>Payment Type</label>
                <select name="payment_type" class="form-control select2" id="payment_type" style="width: 100%;">
                    <?php 
                    $statement = $pdo->prepare("SELECT * FROM `tbl_payment_type` WHERE status=1");
                    $statement->execute();
                    $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                        foreach ($location_result as $result) { ?>
                            <option value="<?=$result['id']?>"><?=$result['name']?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="col-md-6" style="padding-top: 8px;">
                <label>Reference</label>
                <input type="text" name="reference"  placeholder="Reference No" class="form-control"  id="reference" />
            </div>
            <div class="col-md-12" style="padding-top: 8px;">
                <label>Notes</label>
                <textarea name="notes" id="notes" placeholder="Notes" rows="4" class="form-control"></textarea>
            </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>
        <button type="submit" class="btn btn-success" id="submit"><i class="fa fa-save"></i> Save</button>
      </div>
       </form>  
    </div>
</div>
<script>
    $(document).ready(function(){
        paidAmount();
    });
    
    $('#paid_amount').keyup(function(){
        paidAmount();
    });
 
    
    
    function paidAmount(){
        
        var total_amount = $('#total_amount').val();
        var db_advance_amount = $('#db_advance').val();
        remaining_amount = parseFloat(total_amount)-parseFloat(db_advance_amount);
        
        
        console.log("Grand Total: "+total_amount);
        console.log("Paid Amount: "+db_advance_amount);
        console.log("Balance Amount: "+remaining_amount);
        
        
            var paid=$('#paid_amount').val() || 0;
            paidThisPayment = parseFloat(paid);
            calculate=remaining_amount-paidThisPayment;
            add_advance=parseFloat(db_advance_amount)+paidThisPayment
            
            $("#balance_amount").text(calculate.toFixed(2));
            $("#advance_amount").text(add_advance);
           
            $("#new_balance").val(calculate.toFixed(2));
            
            if($("#balance_amount").text() < 0){
                 $("#submit").prop("disabled", true);
            }else{
                $("#submit").prop("disabled", false);
            }
    }
</script>
<script>
$(document).ready(function(){
    $('#add_payment_form').on('submit', function(e){
        e.preventDefault();
        var balance = $('#balance_amount').text();
        console.log("Submitted");
                var formData = new FormData(this);
                formData.append('field_type', 'add_advance_payment');
                            $.ajax({
                            url: 'apis/payments.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                if(response['status']==200){
                                     toastr.success(response['message']);
                                     setTimeout(function(){ location.reload(); },1500); 
                                     
                                }else{
                                    toastr.error(response['message']);
                                }
                            }
                        });
                

    });
});
</script>