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

<?php
    $statement = $pdo->prepare("SELECT * FROM tbl_sales e where e.id=?");
    $statement->execute(array($id));
    $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'];
            $discount_amount = $data['discount_amount'];
        }
    }
?>

<div class="modal-dialog" role="document" style="width: 565px;">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="exampleModalLabel">Add 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: 5px;font-size: 18px;"><i class="fa fa-inr"></i> Total Amount: <span id="total_amount"><?=$grand_total?></span></span></th>
                        <th style="text-align: left;"><span class="label label-danger" style="padding: 5px;font-size: 18px;"><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="sale_id" id="sale_id" value="<?=$id?>" class="form-control" />
            <input type="hidden" name="grand_total" id="grand_total" value="<?=$grand_total?>" class="form-control"/>
            <input type="hidden" name="db_paid" id="db_paid" value="<?=$paid_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();
    });
     $('#discount_amount').keyup(function(){
        paidAmount();
    });
    
    
    function paidAmount(){
        
        var grand_total = $('#grand_total').val();
        var db_paid_amount = $('#db_paid').val();
        var discount_amount = $('#discount_amount').val() || 0;
        remaining_amount = parseFloat(grand_total)-parseFloat(db_paid_amount);
        
        
        console.log("Grand Total: "+grand_total);
        console.log("Paid Amount: "+db_paid_amount);
        console.log("Balance Amount: "+remaining_amount);
        
        
            var paid=$('#paid_amount').val() || 0;
            paidThisPayment = parseFloat(paid)+parseFloat(discount_amount);
            calculate=remaining_amount-paidThisPayment;
            
            $("#balance_amount").text(calculate.toFixed(2));
            //$("#balance_amount").text(balance_format);
            $("#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('type', 'add_sales_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){
                                     alert(response['message']);
                                     location.reload();
                                     
                                }else{
                                    alert(response['message']);
                                }
                            }
                        });
                

    });
});
</script>