<?php  include "header.php"; ?>

<?php
$id = $_GET['id'];
$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="content-wrapper">
        <section class="content-header">
            <h1>Add Sales Payment</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Add Sales Payment</li>            
            </ol>
        </section>


        <div class="col-lg-12 alerts">
            <div id="custom-alerts" style="display:none;">
                <div class="alert alert-dismissable">
                    <div class="custom-msg"></div>
                </div>
            </div>
            
            <?php if ($error)  { ?>
            <div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-ban"></i> Error</h4>
                <?= $error; ?>
            </div>
            <?php $error=0;} if ($warning) { ?>
            <div class="alert alert-warning alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-warning"></i> Warning</h4>
                <?= $warning; ?>
            </div>
            <?php  $warning=0;} if ($message) { ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?= $message; ?>
            </div>
            <?php $message=0; } ?>
        </div>
        <div class="clearfix"></div>







<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">
                <div class="box-header">
                    <!--<h3 class="box-title">Please fill in the information below</h3>-->
                </div>
                <div class="box-body">
                    
                    
                <div id="content-div">
                    
                    <div class="col-lg-12">

                        <form id="form_details" autocomplete="off">
                            
                            
                            <input name="sale_id" value="<?=$id?>" hidden>
                            
                            
            <!--Hidden Values Starts Here-->
            <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="row">
                            
                            
                            <div class="col-md-3">
                                <span class="label label-success" style="padding: 5px;font-size: 18px;">
                                     Total Amount: &nbsp; <i class="fa fa-inr"></i> &nbsp;<span id="total_amount"><?=number_format($grand_total,2)?></span>
                                </span>
                            </div>
                            
                            <div class="col-md-3">
                                <span class="label label-info" style="padding: 5px;font-size: 18px;">
                                     Paid Amount: &nbsp; <i class="fa fa-inr"></i> &nbsp;<span id="paid_total_amount"><?=number_format($paid_amount,2)?></span>
                                </span>
                            </div>
                            
                            
                            <div class="col-md-3">
                                <span class="label label-danger" style="padding: 5px;font-size: 18px;">
                                     Remaining Amount: &nbsp; <i class="fa fa-inr"></i> &nbsp;<span id="balance_amount">0.00</span>
                                </span>
                            </div>
                            
                        </div>
                        
                        
        
                        
                        <div class="row" style="margin-top: 20px;">
                            <div class="col-md-2">
                                <label class="required" for="payment_date">Payment Date:</label>
                                <input type="date" name="payment_date"  value="<?=date("Y-m-d")?>"  class="form-control" id="payment_date" />
                            </div>
                            
                            <!--<div class="col-md-2">
                                 <span class="btn btn-primary btn-xs" id="add_new_row" style="margin-top: 20px;"><i class="fa fa-plus"></i> Add Payment</span>
                            </div>-->
                        </div>
                        
                        
                        
                        <div class="row" style="margin-top: 20px;">
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="discount_amount">Discount</label>
                                    <input type="text" name="discount_amount"  value="<?=$discount_amount?>"  class="form-control amount" id="discount_amount" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="discount_passed_id">Discount Passed By</label>                                   
                                    <select name="discount_passed_id" class="form-control select2" style="width:100%" id="discount_passed_id" required="required">
                                        <?php $statement = $pdo->prepare("SELECT u.*,tbl_role.role_name
                                                                        FROM `tbl_users` u
                                                                        LEFT JOIN tbl_role ON tbl_role.id=u.role
                                                                        WHERE u.status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>" <?php if($session_id==$result['id']){echo "selected";}?>><?=$result['name']?> (<?=$result['emp_code']?>) - <?=$result['role_name']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="cash_amount">Cash</label>
                                    <input type="text" name="cash_amount"  placeholder="Enter the Amount" value=""  class="form-control amount" id="cash_amount" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="upi_amount">UPI</label>
                                    <input type="text" name="upi_amount"  placeholder="Enter the Amount" value=""  class="form-control amount" id="upi_amount" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="payment_type_3">Debit Card</label>
                                    <input type="text" name="payment_type_3" placeholder="Enter the Amount"  value=""  class="form-control amount" id="payment_type_3" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="reference">Reference</label>                                   
                                    <input type="text" name="reference"  placeholder="Reference No" class="form-control"  id="reference" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="notes">Notes</label>                                   
                                    <textarea name="notes" id="notes" placeholder="Notes" rows="3" class="form-control"></textarea>
                                </div>
                            </div>
                            
                        </div>
                        
                        
                        
                        
                        
                        
                        
                
                        
                        
                        <div id="append_sections"></div>
                        
                        
                        

                        <div class="form-group" style="text-align:center; margin-top: 20px;">
                            <input type="submit"  id="submit" value="Add Payment" class="btn btn-success" />
                        </div>
                        
                        </form>

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center; margin-top: 20px;">
                            <span onclick="location.reload();" class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</span>
                            <a href="sales-list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to Sales List</button></a>
                        </div>
                        
                </div>
                
                
<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
        <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>
</div>
                    
                    
                    
                    
                    <div class="container" id="success-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
                        <center><b class="blinking" style="font-size:22px;">Sales Payment Added Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <a href="sales-view.php?id=<?=$id?>"><button class="btn btn-info"><i class="fa fa-print"></i> Print</button></a>
                            &nbsp;&nbsp;
                            <span onclick="location.reload();" class="btn btn-warning"><i class="fa fa-refresh"></i> Reset Form</span>
                            &nbsp;&nbsp;
                            <a href="sales-list.php"><button class="btn btn-danger"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
                        
                        
                        
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</section>


<?php include "footer.php"; ?>

<script>
$(document).ready(function() {
   $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('type', 'create_multiple_payments');
                    $.ajax({
                            url: 'apis/payments.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     /*alert(response['message']);*/
                                     $('#form_details').trigger("reset");
                                     $("#submit").prop('disabled', false);
                                     $('#success-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    alert(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>
<style>
    .porow{
        margin-top: 20px;
    background: aliceblue;
    padding: 10px 0px 10px 0px;
    box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px
    }
</style>

<script>
$(document).ready(function() {
    
var count = 1;
$("#add_new_row").click(function(){
    
    var row=`<div class="row porow" style="margin-top: 20px;">
                            
                            
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="paid_amount">Amount</label>                      
                                    <input type="hidden" name="payment_id[]" value="0">
                                    <input type="text" name="paid_amount[]"  placeholder="Enter the Amount" class="form-control paid_amount" required="required" id="paid_amount" />
                                </div>
                            </div>
                            
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                     <label class="required" for="payment_type">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>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="reference">Reference</label>                                   
                                    <input type="text" name="reference[]"  placeholder="Reference No" class="form-control"  id="reference" />
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="optional" for="notes">Notes</label>                                   
                                    <textarea name="notes[]" id="notes" placeholder="Notes" rows="3" class="form-control"></textarea>
                                </div>
                            </div>
                            
                            
                           
                        </div>`;
        
        $("#append_sections").append(row);
        $(".select2").select2();
        count++;
});
});
</script>

<script>
    $('.amount').keyup(function(){
        paidAmount();
    });
    

    
    $(document).ready(function(){
        paidAmount();
    });
    
    $('.paid_amount').keyup(function(){
        paidAmount();
    });
    
    $(document).on('keyup', '.paid_amount', function() {
        paidAmount();
    });

    
    function paidAmount(){
        
        var grand_total = $('#grand_total').val();
        var db_paid_amount = $('#db_paid').val();
        
        
        
        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 tot=0;
        
          $(".amount").each(function(){
            tot+=Number($(this).val());
          });
          
          
        
            paidThisPayment = parseFloat(tot);
            calculate=remaining_amount-paidThisPayment;
            
            $("#balance_amount").text(calculate.toFixed(2));
            $("#new_balance").val(calculate.toFixed(2));
            
            console.log("Balance: CAL: "+calculate);
            
            
            if($("#balance_amount").text() < 0){
                 $("#submit").prop("disabled", true);
            }else{
                $("#submit").prop("disabled", false);
            }
    }
</script>