$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $("body").on("keyup change", ".qty", calDetail);
    $(".tbody").on("blur", ".qty", autoadd);
    $("body").on("keyup", ".insearch", getSearchData);
    $("body").on(" blur change", ".vouchdt", getvoucher);
    $("body").on("click", ".inload", getSearchData);
    $("body").on("click", "#submit ,#cancel", hide_but);


    function hide_but() {
        $("#ssubmit").hide();
        $("#scancel").hide();
    }


    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);

    }

    function autoadd() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows();
        }
    }

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

        });

    }

    $('#addnew').click(function () {
        AddRows();
    });

    function remove() {
        $(this).closest("tr").remove();
        calDetail();
    }

    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_mi_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  //////////////////////////////////////////////


});