$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $(".tbody").on("keyup blur focus", ".qty, .rate, .amount, .discperc, .discount, .total", calDetail);
    $("body").on("keyup", ".insearch", getSearchData);
    $("body").on("click", ".inload", getSearchData);

    function calDetail() {
        var tr          = $(this).closest("tr"),
            qty         = tr.find(".qty").val(),
            rate        = tr.find(".rate").val(),
            amount      = parseFloat(rate) * parseFloat(qty),
            totqty      = 0;

        tr.find(".amount").val(amount);

        $(".qty").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                totqty += parseFloat(this.value);
            }
        });
        $(".totqty").val(totqty); 
    }

    $('#addnew').click(function () {
        AddRows();
    });
    function remove() {
        $(this).closest("tr").remove();
    }

    function AddRows() {
        var ctr = $('#items').val();
        var prodtype = $('.prodtype').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;
        $.post('inward_add.php', {ctr: ctr, hide: hide_check, prodtype: prodtype}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
            $(detail).select2();
        });
    }
    
    
    
    //////////////////////////////////////////////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  //////////////////////////////////////////////
    

});