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

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

    function notsame() {
        var fgodown = $('.fgodown').val(),
                tgodown = $('.tgodown').val(),
                fseries = $('.fseries').val(),
                tseries = $('.tseries').val();
        
        if (fgodown == tgodown && fseries == tseries) {
            alert('Series cannot be same');
            $('.tseries').val('').trigger('change');
        }
    }

    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);
            }
            $(".totqty").val(totqty);
        });

    }

    $('#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_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  //////////////////////////////////////////////


});