$(document).ready(function () {
    $("body").on("keyup", ".insearch", getSearchData);
    $("body").on(" blur change", ".vouchdt", getvoucher);
    $("body").on("click", ".inload", getSearchData);
    
    
    
   function getvoucher() {
        var date = new Date($(".vouchdt").val()),
                month = (date.getMonth() + 1).toString(),
                vouchno = $('.vouchno').val(),
                voucher = '0' + month + vouchno;
        
        $('.monno').val(month);
        $('.voucher').val(voucher);

    }
    

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".innoRecord").hide();
    
    $(".searchModal").on('shown.bs.modal', function () {
        $(".insearch").focus();
        $(".insearch").val("");
        $(".infield").val("1");
        getSearchData();
    });
    
    var smrangeFrom = 0,
            smrangeTo = 40;

    function getSearchData(pagination) {
        
        $(".inloader").show();
        
        if (pagination == 'prev') {
            if (smrangeFrom > 0) {
                smrangeFrom  = (smrangeFrom - 40);
                smrangeTo    = (smrangeTo - 40);
            }
        }
        if (pagination == 'next') {
            smrangeFrom  = (smrangeFrom + 40);
            smrangeTo    = (smrangeTo + 40);
        }

        var insearch     = $(".insearch").val(),
            series      = $(".series").val(),
            infield      = $(".infield").val(),
            inoperator   = $(".inoperator").val(),
            data        = {insearch: insearch, infield: infield, inoperator: inoperator, series: series,
                           rangeFrom: smrangeFrom, rangeTo: smrangeTo},
            values      = "";
        $.ajax({
            type    : 'GET',
            dataType: 'json',
            url     : "inward_search.php",
            data    : data,
            success: function (response) {
                $.each(response, function () {
                     values += "<tr class='searchTr'>" +
                              "<td align='left'>" + this.vouchno + "</td>" +
                              "<td align='left'>" + this.from+ "</td>" +
                              "<td align='left'>" + this.to+ "</td>" +
                              "<td align='left'>" + this.vouchdt+ "</td>" +
                              "<td align='left'>" + this.chalno+ "</td>" +
                              "<td align='left'>" + this.totqty+ "</td>" +
                              "<td align='left'>" + this.rem1+ "</td>" +
                              "<td align='left'>" + this.action+ "</td>" +
                              "</tr>";
                });
                $(".inloader").hide();
                $(".inTbody").empty();
                $(".inTbody").html(values);
                if (response) {
                    $(".search-action-bar").show();
                }
            },
            error: function (request, textStatus, errorThrown) {
                
            }
        });
    }
    $("#search").click(function () {
        getSearchData();
    });
//    $(".ssearch").change(function () {
//        getSearchData();
//    });
    $(".inprevious").click(function () {
        getSearchData('prev');
    });
    $(".innext").click(function () {
        getSearchData('next');
    });
    
    
    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////
    

});