@extends('adminlte::page') @section('content')
<?php

use Illuminate\Support\Facades\Session;

Fun::lang() ?>
<?php $total = 0 ?>
<div class="page">
    <div class="ajax-alert">
        @if ($message = Session::get('success'))
        <div class="alert-message alert-success">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <i class="fa fa-check"></i>
            <h3>Success</h3>
            <p>{{$message}}</p>
        </div>
        @endif
        @if ($message = Session::get('danger'))
        <div class="alert-message alert-danger">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <i class="fa fa-exclamation-triangle"></i>
            <h3>Errors</h3>
            <p>{{$message}}</p>
        </div>
        @endif
        @if ($message = Session::get('warning'))
        <div class="alert-message alert-warning">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <i class="fa fa-exclamation-triangle"></i>
            <h3>Warning</h3>
            <p>{{$message}}</p>
        </div>
        @endif
    </div>
    <div class="top-content">
        <div class="row pos">
            <div class="col-sm-12 col-xs-12">
                <div class="pull-left">
                    <h3>VIEW - EDIT - APPROVE POS</h3>
                </div>
                <div class="pull-right">
                    <div class="button-pos">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="box-body">
        <div class="pos">
            <div class="row">
                <div class="col-sm-8">
                    <div class="pos-box">
                        <div class="row" id="search-item">
                            <div class="col-sm-12">
                                <div class="col-sm-6">
                                    <div class="col-sm-4">
                                        <label for="">Item Name</label>
                                    </div>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="item" id="item_filter">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <hr>
                        <div id="tag_container">
                            @include('administrator.external.deposit.pre-sult')
                        </div>
                        <div id="form-pay" style="display: none;">
                            {!! Form::model($saleRequest, ['method' => 'PATCH','route' => ['deposit.approve_update_info',$saleRequest->id],'enctype'=>'multipart/form-data']) !!}
                            <div class="total">
                                <strong class="green" style="display:none" id="total_amount"></strong>
                            </div>
                            <div class="form-group">
                                <div class="new-customer-button">
                                    <button class="pull-right" type="button" id="add" class="btn btn-sm btn btn-warning" style="margin-bottom:5px">
                                        <span> New Customer </span>
                                        <i class="fa fa-plus"></i>
                                    </button>
                                </div>
                                <label class="required">Select Customer</label>
                                {!! Form::select('app_client_id', $clients, null, ['class' => 'form-control select2','id'=>'client_id']) !!}
                            </div>
                            <div class="form-group">
                                <label for="required">Sale By</label>
                                {!! Form::select('sale_type_by', ['1'=>'Employee','2'=>'Agency'], null, ['class' => 'form-control select2','id'=>'sale-type-by']) !!}
                            </div>
                            @if($sale->sale_type_by==1)

                            <div id="append-sale">
                                <div class="form-group">
                                    <label for="required">Select Employee(Sale)</label>
                                    {!! Form::select('employees', $employees, $employees_sale, ['class' => 'form-control select2','required']) !!}
                                </div>
                            </div>
                            @else

                            <div id="append-sale">
                                <div class="form-group">
                                    <label for="required">Select Agency</label>
                                    {!! Form::select('app_agency_id', $agencies, null, ['class' => 'form-control select2','id'=>'agency_id']) !!}
                                </div>
                                <div class="form-group">
                                    <label for="required">Close Sale</label>
                                    {!! Form::select('close_sale_id', $closesale, null, ['class' => 'form-control select2','id'=>'close_sale_id']) !!}
                                </div>
                            </div>

                            @endif
                            <div class="form-group">
                                <label class="required">Deposit Date</label>
                                <input type="text" name="date_deposit" class="form-control number-only date_picker" placeholder="Deposit Date *" value="{{date('d-m-Y',strtotime($saleRequest->app_date_deposit))}}" required />
                            </div>
                            <div class="form-group">
                                <label class="required">Contract Date</label>
                                <input type="text" name="date_contract" class="form-control number-only date_picker" placeholder="Date Contract *" value="{{date('d-m-Y',strtotime($saleRequest->app_date_contract))}}" required />
                            </div>

                            <div class="form-group">
                                <label class="required">Discount Amount</label>
                                <input type="text" name="sale_discount_amount" class="form-control number-only" placeholder="Discount Amount ($)*" value="{{$saleRequest->sale_discount}}" readonly />
                            </div>
                            <div class="form-group" style="display: none;">
                                <label class="required">Deposit Amount</label>
                                <input type="text" name="sale_receive" class="form-control number-only sale_receive" placeholder="Deposit Amount ($)*" value="{{$saleRequest->app_booking_amount}}" readonly />
                                <input type="hidden" name="sale_id" value="{{$sale->id}}">
                                <input type="hidden" name="app_id" value="{{$saleRequest->id}}">
                            </div>
                            
                            <div class="form-group">
                                <label>Promotion</label>
                                {!! Form::select('promotion', $promotion, !empty($promotion_sale_app)?$promotion_sale_app->promotion_id:null, ['class' => 'form-control select2','id'=>'promotion']) !!}
                            </div>

                            <table class="table table-striped table-bordered">
                                <thead>
                                <tr>
                                    <th colspan="4">Product Promotion</th>
                                    <th>
                                        <span class="pull-right button_primary btn-sm plus_ens fa fa-plus add-pos-promotion"></span>
                                    </th>
                                </tr>
                                </thead>
                                <tbody class="promotion-detail">
                                    @foreach($sale_promotion_detail_app as $key=>$value)
                                    <tr>
                                        <td width="15%">
                                            <label class="control-label" ><strong> Product Name : </strong></label>
                                        </td>
                                        <td>
                                            {!! Form::select('promotion_detail[]', $promotion_detail,(!empty($value)?$value->promotion_detail_id:null), ['class' => 'form-control select2']) !!}
                                        </td>
                                        <td>
                                            <label class="control-label"><strong> Qty : </strong></label>
                                        </td>
                                        <td>
                                            <input type="text" name="product_qty[]" class="number-only form-control" placeholder="Product Qty" value="{{$value->qty}}">
                                        </td>
                                        <td></td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>

                            {{-- <div class="form-group" id="div-append-promotion-detail">
                                <label>Promotion Product</label>
                                {!! Form::select('promotion_detail[]', $promotion_detail, !empty($sale_promotion_detail_app)?$sale_promotion_detail_app->pluck('promotion_detail_id')->toArray():null, ['class' => 'form-control select2','id'=>'promotion_detail','multiple']) !!}
                            </div> --}}

                            <div class="form-group">
                                <label for="required">Payment Policy</label>
                                {!! Form::select('payment_policy', $payment_policy, !empty($payment_policy_sale)?$payment_policy_sale->pluck('payment_policy_id')->toArray():null, ['class' => 'form-control select2','id'=>'payment_policy']) !!}
                            </div>
                            <div class="form-group" id="policy_detail">
                                <label for="required">Payment Policy Detail</label>
                                {!! Form::select('payment_policy_detail', $payment_policy_detail, !empty($payment_policy_sale)?$payment_policy_sale->pluck('policy_detail_id')->toArray():null, ['class' => 'form-control select2','id'=>'payment_policy_detail']) !!}
                            </div>
                            <table class="table table-striped table-bordered" style="display:none">
                                <thead>
                                <tr>
                                    <th colspan="6">Deposit Schedule</th>
                                    <th>
                                        <span class="pull-right button_primary btn-sm plus_ens fa fa-plus add"></span>
                                    </th>
                                </tr>
                                </thead>
                                
                                <tbody class="resultbody">
                                @foreach($deposit_schedule as $key => $depo_schedule)
                                    <tr>
                                        <td width="7%">
                                            <label class="control-label"><strong> Step : </strong></label>
                                        </td>
                                        <td width="5%">
                                            <input type="text" class="form-control" name="payment_no[]" value="{{$key+1}}" readonly="readonly">
                                        </td>
                                        <td width="15%">
                                            <label class="control-label" ><strong> Deposit Date : </strong></label>
                                        </td>
                                        <td>
                                            <input type="text" id="meng_{{$key+1}}" class="form-control date-picker-a date_picker auto_date" value="{{date('d-m-Y',strtotime($depo_schedule->ds_date))}}" name="itempro_date[]">
                                        </td>
                                        <td width="15%">
                                            <label class="control-label"><strong> Deposit Amount : </strong></label>
                                        </td>
                                        <td>
                                            <input type="text" name="itempro_payamount[]" class="number-only form-control itempro_payamount meng_payment" value="{{$depo_schedule->ds_amount}}" placeholder="Pay Amount">
                                        </td>
                                        @if($key+1 != 1)
                                            <td>
                                                <span class="pull-right btn-sm button_danger delete fa fa-minus"></span>
                                            </td>
                                        @endif
                                    </tr>
                                    <input type="hidden" value="{{$key+1}}" id="key_no">
                                @endforeach
                                
                                </tbody>
                            </table>

                            <div class="form-group">
                                <label class="">Remark</label>
                                <textarea name="deposit_remark" id="" cols="30" rows="5" class="form-control">{{$saleRequest->deposit_remark}}</textarea>
                            </div>
                            <div style="display: none;">
                                @if(session('cart_edit')) @foreach(session('cart_edit') as $id => $item_cart)
                                <span>
                                    <input type="hidden" name="saledetail_itemid[]" value="{{ $item_cart['item_id'] }}"><br>
                                    <input type="hidden" name="sale_item_code[]" value="{{ $item_cart['item_name'] }}"><br>
                                    <input type="hidden" name="saledetail_price[]" value="{{ $item_cart['item_price'] }}"><br>
                                    <input type="hidden" name="saledetail_cost[]" value="{{ $item_cart['item_cost'] }}"><br>
                                    <input type="hidden" name="project_type[]" value="1"><br>
                                    <input type="hidden" name="total_amount" class="form-control">
                                </span>
                                @endforeach @endif
                            </div>
                            <div class="form-group">
                                <div class="button-pos">
                                    <button type="submit" class="btn button_success pull-right">
                                        <i class="fa fa-credit-card-alt" aria-hidden="true"></i> Approve</button>
                                    <button type="button" onClick="javascript:history.go(-1)" class="button_primary pull-right">
                                        <span class=" fa fa-arrow-circle-left"></span> Back
                                    </button>
                                </div>
                            </div>
                            {!! Form::close() !!}
                        </div>
                    </div>
                </div>
                
                <div class="col-sm-4">
                    <div class="pos-box" id="session-table">
                        <table id="cart" class="table table-hover table-condensed">
                            <thead>
                                <tr>
                                    <th width="10%">#</th>
                                    <th width="50%">Item Name</th>
                                    <th width="5%">Quantity</th>
                                    <th width="30%">Item Pirce</th>
                                    <th width="5%">Action</th>
                                </tr>
                            </thead>
                            <tbody id="tbody">
                                <?php $i = 0; ?>
                                @if(session('cart_edit')) @foreach(session('cart_edit') as $id => $item_cart)
                                <?php $i++ ?>
                                <?php $total += $item_cart['item_price'] * $item_cart['quantity'] ?>
                                <tr>
                                    <td data-th="#">
                                        {{ $i }}
                                    </td>
                                    <td data-th="Product">
                                        <div class="row">
                                            <div class="col-sm-12">
                                                {{ $item_cart['item_name'] }}
                                            </div>
                                        </div>
                                    </td>
                                    <td data-th="Quantity">
                                        {{ $item_cart['quantity'] }}
                                    </td>
                                    <td data-th="Price" class="text-right">$ {{ number_format($item_cart['item_price'],2) }}</td>
                                    <td class="actions" data-th="">
                                        <button class="btn btn-danger btn-xs remove-from-cart" data-id="{{ $id }}"><i class="fa fa-trash-o"></i></button>
                                    </td>
                                </tr>
                                @endforeach @endif
                            </tbody>
                            <tfoot id="pos-footer">
                                <tr>
                                    <td colspan="3" class="text-right">Total :</td>
                                    <td id="totalPrice" class=" text-right">$ {{number_format($total,2)}}</td>
                                    <td></td>
                                </tr>
                            </tfoot>
                        </table>
                        <!-- <div class="sum-footer">
                            <strong class="green pull-right">$ {{ $total }}</strong>
                        </div> -->
                        <input type="hidden" value="{{ $total }}" name="total" class="total">

                        <div class="col-sm-12 padding-0">
                            <div class="button-pos">
                                <button onclick="fun_pay()" class="btn button_primary"><i class="fa fa-share icon-flipped"></i> Back</button>
                                <button onclick="fun_back()" class="btn button_success"><i class="fa fa-shopping-cart"></i> Add more</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>

<div class="modal fade bs-modal-lg" id="addnew" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h3 class="modal-title">New Client</h3>
            </div>
            <br />
            <form action="{{route('add-new-client')}}" id="addForm">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-6 col-sm-6 col-md-6">
                            <div class="form-group">
                                <strong>First Name</strong>
                                <input type="text" name="first_name" class="form-control" placeholder="First Name" required="">
                            </div>
                            <div class="form-group">
                                <strong>Last Name</strong>
                                <input type="text" name="last_name" class="form-control" placeholder="First Name" required="">
                            </div>
                            <div class="form-group">
                                <strong>Gender</strong>
                                <select name="client_gender" id="" class="form-control select2">
                                    <option value="1">Male</option>
                                    <option value="2">Female</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <strong>Phone Number</strong>
                                <input type="text" name="client_phone1" class="form-control" placeholder="Phone Number" required="">
                            </div>
                            <div class="form-group">
                                <strong>Client Address</strong>
                                <input type="text" name="client_address" class="form-control" placeholder="Client Address" required="">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" data-dismiss="modal" class="btn btn-danger"><i class="fa fa-close"></i> No</button>
                    <button type="submit" class="btn btn-success" data-toggle="modal"><i class="fa fa-save"></i> OK</button>
                </div>
            </form>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<div class="overlay">
    <div id="loading-img"></div>
</div>

@endsection
@section('js')
<script type="text/javascript">
    var opt = {!! json_encode($option_promotion_detail) !!};
    $(document).on("click",".add-pos-promotion",function()
    {
        $(".promotion-detail").append(`
            <tr>
                <td width="15%">
                    <label class="control-label" ><strong> Product Name : </strong></label>
                </td>
                <td>
                    <select name = 'promotion_detail[]' class="form-control select2">
                        ${opt}
                    </select>
                </td>
                <td>
                    <label class="control-label"><strong> Qty : </strong></label>
                </td>
                <td>
                    <input type="text" name="product_qty[]" class="number-only form-control" placeholder="Product Qty">
                </td>
                <td><span class="pull-right btn-sm button_danger delete fa fa-minus" onclick="$(this).closest('tr').remove()"></span></td>
            </tr>`
        );
        $(".select2").select2();
    });
    $(document).on('change', '#promotion', function() {
        var promotion_id = $(this).val();
        $("#main-spinner").show();
        $.ajax({
            type: 'GET',
            url: '/realty/get-promotion-detail',
            dataType:'json',
            data: {
                'promotion_id': promotion_id
            },
            success: function(data){
                $("#main-spinner").hide();
                opt = `<option value="">Please Select</option>`;
                $.each(data,function(index,value)
                {
                    opt += `<option value = '${value.id}'>${value.promotion_detail}</option>`;
                });
                
                $("tbody.promotion-detail").find("tr").find("td:eq(1)").each(function(){
                    $(this).find("select").html(opt);
                });
                $(".select2").select2();
            }
        });
    });
    $(document).on("change","#sale-type-by",function()
    {
        $("#append-sale").empty();
        if($(this).val()==1)
        {
            $("#append-sale").append(`<div class="form-group">
                                    <label for="required">Select Employee(Sale)</label>
                                    {!! Form::select('employees[]', $employees, Auth::user()->id, ['class' => 'form-control select2','required']) !!}
                                </div>`);
             $(".select2").select2();
        }
        else{
            $("#append-sale").append(`
                            <div class="form-group">
                                <label for="required">Select Agency</label>
                                {!! Form::select('app_agency_id', $agencies, null, ['class' => 'form-control select2','id'=>'agency_id']) !!}
                            </div>
                            <div class="form-group">
                                <label for="required">Close Sale</label>
                                {!! Form::select('close_sale_id', $closesale, null, ['class' => 'form-control select2','id'=>'close_sale_id']) !!}
                            </div>`);
            $(".select2").select2();
        }
    });
    $(document).ready(function() {
        $("#search-item").hide();
        $("#tag_container").hide();
        $("#form-pay").show();
    });
    $(window).on('hashchange', function() {
        if (window.location.hash) {
            var page = window.location.hash.replace('#', '');
            if (page == Number.NaN || page <= 0) {
                return false;
            } else {
                getData(page);
            }
        }
    });
    $(window).on('hashchange', function() {
        if (window.location.hash) {
            var page = window.location.hash.replace('#', '');
            if (page == Number.NaN || page <= 0) {
                return false;
            } else {
                getData(page);
            }
        }
    });
    $(document).ready(function() {
        $(document).on('click', '.pagination a', function(event) {
            event.preventDefault();
            $('li').removeClass('active');
            $(this).parent('li').addClass('active');
            var myurl = $(this).attr('href');
            var page = $(this).attr('href').split('page=')[1];
            getData(page);
        });
    });

    function getData(page) {
        $.ajax({
            url: '?page=' + page,
            type: "get",
            datatype: "html"
        }).done(function(data) {
            $("#tag_container").empty().html(data);
            location.hash = page;
        }).fail(function(jqXHR, ajaxOptions, thrownError) {
            alert('No response from server');
        });
    }

    $(document).ready(function() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $('#add').click(function() {
            $('#addnew').modal('show');
            $('#addForm')[0].reset();
        });
        $('#addForm').on('submit', function(e) {
            e.preventDefault();
            var form = $(this).serialize();
            var url = $(this).attr('action');
            $.ajax({
                type: 'POST',
                url: url,
                data: form,
                dataType: 'json',
                success: function() {
                    $('#addnew').modal('hide');
                    get_new_client();
                }
            });
        });
    });

    function get_new_client() {
        $('#client_id').empty();
        $.ajax({
            type: 'GET',
            url: "{{route('pos-get-new-client')}}",
            dataType: 'json',
            success: function(data) {
                $.each(data, function(index, element) {
                    $("#client_id").append('<option value="' + element.id + '">' + element.client_firstname+' '+element.client_lastname + '</option>');
                });
            }
        });
    }

    function fun_pay() {
        $("#tag_container").hide();
        $("#search-item").hide();
        $("#form-pay").show();

        var total = $("input[name=total]").val();
        $("input[name=total_amount]").val(total);
        document.getElementById('total_amount').innerHTML = total;
    }

    function fun_back() {
        $("#tag_container").show();
        $("#search-item").show();
        $("#form-pay").hide();
    }
    $(document).on('click', '.remove-from-cart', function(e) {
        e.preventDefault();
        var ele = $(this);
        var id = $(this).data('id');
        var route = "{{route('remove-from-cart-edit',':id')}}";
        route = route.replace(':id', id);
        $.confirm({
            title: 'Confirm!',
            content: 'This Item will be removed!',
            buttons: {
                cancel: function() {

                },
                confirm: function() {
                    $('.overlay').show();
                    $.ajax({
                        url: route,
                        method: "GET",
                        data: {
                            _token: '{{ csrf_token() }}',
                            id: ele.attr("data-id")
                        },
                        success: function(response) {
                            var page = window.location.hash.replace('#', '');
                            if (page == Number.NaN || page <= 0) {
                                page = 1;
                            }
                            getData(page);
                            get_cart();
                            $('.overlay').hide();
                        }
                    });
                }
            }
        });
    });
    $(document).on('change', '#project_id', function() {
        var project_id = $(this).find(":selected").val();
        var route = "{{route('pos-filter')}}";
        var div = "";
        $('#item_list').empty();
        $.ajax({
            type: "POST",
            url: route,
            dataType: 'text',
            data: {
                project_id: project_id,
                _token: '{{ csrf_token() }}',
            },
            success: function(response) {
                $('#item_list').html(response);
            },
            error: function() {
                alert('Error');
            }
        });
    });
    $(document).on('click', '.btn-add-to-cart', function() {
        var id = $(this).data('id');
        var route = "{{route('add-to-cart-edit',':id')}}";
        route = route.replace(":id", id);
        $('.overlay').show();
        $.ajax({
            type: 'GET',
            url: route,
            dataType: 'json',
            data: {
                'id': id
            },
            success: function(data) {
                if (data.success == false) {
                    $.alert("Item was Already Added!");
                } else if (data.success == true) {
                    get_cart();
                    var page = window.location.hash.replace('#', '');
                    if (page == Number.NaN || page <= 0) {
                        page = 1;
                    }
                    getData(page);
                } else {
                    $.alert('Not found');
                }
                $('.overlay').hide();
            }
        });
    });
    $(document).on('keyup', '#item_filter', function() {
        var item_name = $(this).val();
        var project_id = $('#project_id').find(":selected").val();
        var route = "{{route('pos-filter-by-name')}}";
        if (item_name != "") {
            $.ajax({
                type: "POST",
                url: route,
                dataType: 'text',
                data: {
                    item_name: item_name,
                    project_id: project_id,
                    _token: '{{ csrf_token() }}',
                },
                success: function(response) {
                    $('#tag_container').html(response);
                },
                error: function() {
                    alert('Error');
                }
            });
        }
    });

    function get_cart() {
        var route = "{{route('get-session-cart-edit')}}";
        var table = "";
        var i = 1;
        var totalPrice = 0;
        $.ajax({
            type: 'GET',
            url: route,
            dataType: 'json',
            data: {
                _token: '{{ csrf_token() }}',
            },
            success: function(response) {
                var data = response.data;
                $('#tbody').empty();
                $.each(data, function(index, val) {
                    var amount = val.quantity*val.item_price;
                    totalPrice+=amount;
                    table += `<tr>
                                <td>${(i++)}</td>
                                <td>${val.item_name}</td>
                                <td>${val.quantity}</td>
                                <td class="text-right">$ ${parseFloat(val.item_price).toFixed(2)}</td>
                                <td><span data-id="${val.item_id}" class="btn btn-xs btn-danger fa fa-trash remove-from-cart"></span></td>
                            </tr>`;
                });
                $('#totalPrice').text('$ '+parseFloat(totalPrice).toFixed(2));
                $('#tbody').append(table);
            }
        });
    }

    $(document).on("change","#payment_policy",function()
    {
        if($(this).val()==1)
        {
            $("#down_pay").show();
            $("#policy_detail").show();
        }else {
            $("#down_pay").hide();
            $("#policy_detail").hide();
        }
    });
    $(document).on('click', '.delete', function () {
        $(this).closest('tr').remove();
        autoPaymentDate();
    });
    var num_of_month = 0;
    var nso = $('#key_no').val();
    var num_of_row = 0;
    var first_month_pay = 0;
    $(document).on('click', '.add', function () {
        nso++;
        var date = lastdateST();
        var newDate = Loan_date_generator(date);

        var n = ($('.resultbody tr').length - 0);
        n++;
        var tr = '<tr>' +
            '<td width="7%">' +
            '<label class="control-label"><strong> Step : </strong></label>' +
            '</td>' +
            '<td width="5%">' +
            '<input type="text" class="form-control" name="payment_no[]" value="' + n + '" readonly="readonly">' +
            '</td>' +
            '<td width="15%">' +
            '<label class="control-label"><strong> Deposit Date : </strong></label>' +
            '</td>' +
            '<td>' +
            '<input type="text" id="meng_' + nso + '" class="form-control date-picker date_picker num_of_month auto_date" name="itempro_date[]" value="'+newDate+'" required="true">' +
            '</td>' +
            '<td width="15%">' +
            '<label class="control-label"><strong> Deposit Amount : </strong></label>' +
            '</td>' +
            '<td>' +
            '<input type="text" name="itempro_payamount[]" class="number-only form-control itempro_payamount meng_payment" placeholder="Pay Amount" required="true">' +
            '</td>' +
            '<td>' +
            '<span class="pull-right btn-sm button_danger delete fa fa-minus"></span>' +
            '</td>' +
            '</tr>';
        $('.resultbody').append(tr);
        autoPaymentDate();
        $('.date_picker').datepicker({
            yearRange: "-100:+0",
            dateFormat: "dd-mm-yy",
            changeMonth: true,
            changeYear: true
        });
    });
    function autoPaymentDate(){
        var index = 0;
        var date  = '';
        $('.auto_date').each(function(){
            if(index == 0){
                date = $(this).val();
            }else{
                date = Loan_date_generator(date);
                $(this).val(date);
            }
            index++;
        });
    }
    function lastdateST() {
        var date = '';
        $('.resultbody .date_picker').each(function () {
            date = $(this).val();
        });
        return date;
    }
    function Loan_date_generator(date) {

        var next_date = date;
        var next_date = next_date.split('-');

        var str_date =   next_date[2]+ '-' + next_date[1] + '-' + next_date[0];

        var next_date = new Date(str_date);

        next_date.setMonth(next_date.getMonth() + 1);
        var new_next_date = new Date(next_date);
        var dd = new_next_date.getDate();
        var mm = new_next_date.getMonth() + 1;

        var yyyy = new_next_date.getFullYear();
        if (dd < 10) {
            dd = '0' + dd;
        }
        if (mm < 10) {
            mm = '0' + mm;
        }

        return new_next_date =  dd+ '-' + mm + '-' + yyyy;
    }
</script>
@endsection