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

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".dnoRecord").hide();
    $(".dloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".dloader").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(),
                dsearch = $(".dsearch").val(),
                dfield = $(".dfield").val(),
                doperator = $(".doperator").val(),
                data = {dsearch: dsearch, dfield: dfield, doperator: doperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "dept_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".dnoRecord").show();
                    $(".dnext").hide();
                } else {
                    $(".dnext").show();
                    $(".dnoRecord").hide();

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

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


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