$(document).ready(function () {
    $("body").on('blur', '.init', checkInit);
    $("body").on("keyup", ".bsearch", getSearchData);
    $("body").on("click", ".bload", getSearchData);
    $("body").on("click", "#search", getSearchData);

    function checkInit() {
        var init = $('.init').val();
        var accode = $('.accode').val();
        var type = "bank"
        $.post('check_init.php', {init: init, type: type, accode: accode}, function (data) {
            if (data == 1) {
                alert("Init Already Exist");
                $(".init").val('');
            }
        });
    }


    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".bnoRecord").hide();
    $(".bloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".bloader").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(),
                isearch = $(".bsearch").val(),
                ifield = $(".bfield").val(),
                ioperator = $(".boperator").val(),
                data = {isearch: isearch, ifield: ifield, ioperator: ioperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "bank_search.php",
            data: data,
            success: function (response) {
                $.each(response, function () {
                    values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                            "<td align='left'>" + this.name + "</td>" +
                            "<td align='left'>" + this.init + "</td>" +
                            "<td align='left'>" + this.openbal + "</td>" +
                            "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                            "</tr>";
                });
                $(".bloader").hide();
                $(".bTbody").empty();
                $(".bTbody").html(values);
                if (response) {
                    $(".search-action-bar").show();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#bsearch").click(function () {
        getSearchData();
    });
    $(".bprevious").click(function () {
        getSearchData('prev');
    });
    $(".bnext").click(function () {
        getSearchData('next');
    });


    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////


});