$(document).ready(function () {
    $("body").on("keyup", ".msearch", getSearchData);
    $("body").on("click", ".mload", getSearchData);
    $("body").on("click", "#search", getSearchData);

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".mnoRecord").hide();
    $(".mloader").hide();

    $(".searchModal").on('shown.bs.modal', function () {
        $(".msearch").focus();
        $(".msearch").val("");
        $(".mfield").val("1");
        getSearchData();
    });

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".mloader").show();

        if (pagination == 'prev') {
            if (imrangeFrom > 0) {
                imrangeFrom = (imrangeFrom - 40);
                imrangeTo = (imrangeTo - 40);
            }
        }
        if (pagination == 'next') {
            imrangeFrom = (imrangeFrom + 40);
            imrangeTo = (imrangeTo + 40);
        }

        var series = $(".series").val(),
                vouchno = $(".vouchno").val(),
                msearch = $(".msearch").val(),
                mfield = $(".mfield").val(),
                moperator = $(".moperator").val(),
                data = {msearch: msearch, mfield: mfield, moperator: moperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "manufac_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".mnoRecord").show();
                    $(".mnext").hide();
                } else {
                    $(".mnext").show();
                    $(".mnoRecord").hide();

                    $.each(response, function () {
                        values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                                "<td align='left'>" + this.name + "</td>" +
                                "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                                "</tr>";
                    });
                    $(".mloader").hide();
                    $(".mTbody").empty();
                    $(".mTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#search").click(function () {
        getSearchData();
    });
    $(".mprev").click(function () {
        getSearchData('prev');
    });
    $(".mnext").click(function () {
        getSearchData('next');
    });


    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////
});