$(document).ready(function () {
    $("body").on("change blur", '.oldtable', getKot);
    $("body").on("change blur", '.oldtable', getWaiter);

    function getKot() {
        var oldtable = $(".oldtable").val();
        $.post('getKot.php', {oldtable: oldtable}, function (data) {
            $(".kotno").val(data);
        });
    }
    
    function getWaiter() {
        var oldtable = $(".oldtable").val();
        $.post('getWaiter.php', {oldtable: oldtable}, function (data) {
            $(".waiter").html(data);
        });
    }


    var para = $(".para").val();
    var admin = $(".admin").val();
    if (admin != '1') {
        $("#hdelete").hide();
        $("#hedit").hide();
    }
    $("#submit").hide();

    if (para == 'P') {
        $("#edit").click();
    }
    $("#edit").click(function () {
        $("#submit").hide();
    });

    $("#finishTab").click(function () {
        var valid = true;


//        if (staff == '') {
//            alert('Staff Cannot Be Empty');
//            return false;
//            valid = false;
//        }




        if (valid) {
            var isValid = true;

            if (isValid) {
                $("#submit").show();
                $("#submit").click();

            }
        }
    });


//search part


    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("change", ".soperator", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////

    $(".snoRecord").hide();
    $(".sloader").hide();

    var imrangeFrom = 0,
            imrangeTo = 40;

    $(".ssearch").focus();
//    $(".ssearch").val("");
//    $(".sfield").val("1");
    getSearchData();

    function getSearchData(pagination) {

        $(".sloader").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(),
                type = $(".type").val(),
                manual = $(".manual").val(),
                ssearch = $(".ssearch").val(),
                sfield = $(".sfield").val(),
                sortBy = $(".sortBy").val(),
                orderAs = $(".orderAs").val(),
                soperator = $(".soperator").val(),
                data = {ssearch: ssearch, sfield: sfield, sortBy: sortBy, orderAs: orderAs, soperator: soperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo, type: type, manual: manual},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "tabletrans_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".snoRecord").show();
                    $(".snext").hide();
                    $(".ssload").hide();
                } else {
                    $(".snext").show();
                    $(".snoRecord").hide();
                    $(".ssload").show();


                    $.each(response, function () {
                        values +=
                                "<tr class='getdtl' id='getdtl_" + this.value + " ' >" +
                                "<td align='left'>" + this.vouchno + "</td>" +
                                "<td align='left'>" + this.vouchdt + "</td>" +
                                "<td align='left'>" + this.oldtable + "</td>" +
                                "<td align='left'>" + this.newtable + "</td>" +
                                "<td align='left'>" + this.kotno + "</td>" +
                                "<td align='left'>" + this.user + "</td>" +
                                "<td align='right'>" + this.remark + "</td>" +
                                "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
//                                "<td align='center'><a href='pendkot_print.php?vouchno=" + this.vouchno + "' target='_blank'><i class='fa fa-print' aria-hidden='true'></i></a></td>" +
                                "</tr>" ;
                    });
                    $(".sloader").hide();
                    $(".sTbody").empty();
                    $(".sTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#search").click(function () {
        $(".searchModal").on('shown.bs.modal', function () {
            $(".ssearch").focus();
            $(".ssearch").val("");
            $(".sfield").val("1");
            getSearchData();
        });

        getSearchData();
    });
    $(".sprev").click(function () {
        getSearchData('prev');
    });
    $(".snext").click(function () {
        getSearchData('next');
    });


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