$(document).ready(function () {
    $("body").on("keyup", ".csearch", getSearchData);
    $("body").on("click", ".cload", getSearchData);
    $("body").on("click", "#search", getSearchData);
  $("body").on("change", ".state", getStateCode);

    function getStateCode() {
        var state = $(".state").val();
        $.get('get_stcode.php', {state: state}, function (data) {
            $(".statecode").val(data);
        });
    }
    
    
    
    var accode = $(".vouchno").val();
    if (accode == 49) {
        $("#hdelete").hide();
    }

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".cnoRecord").hide();
    $(".cloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".cloader").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(),
                csearch = $(".csearch").val(),
                cfield = $(".cfield").val(),
                coperator = $(".coperator").val(),
                data = {csearch: csearch, cfield: cfield, coperator: coperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "cust_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".cnoRecord").show();
                    $(".cnext").hide();
                } else {
                    $(".cnext").show();
                    $(".cnoRecord").hide();

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

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


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