$(document).ready(function () {
    $("body").on("click", ".clickable-row", clickRow);
    $(".searchModal").on("click", ".clickable-row", clickRow);
    $("body").on("change", ".ssearch", getSearchData);
    $("body").on("change keyup", ".ssearch2", getSearchData);
    $("body").on("change", ".soperator", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    $(window).keydown(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            return false;
        }
    });

    $(".ssearch").scannerDetection({
        //https://github.com/kabachello/jQuery-Scanner-Detection

        timeBeforeScanTest: 200, // wait for the next character for upto 200ms
        avgTimeByChar: 40, // it's not a barcode if a character takes longer than 100ms
        preventDefault: true,
        endChar: [13],
        onComplete: function (ssearch, qty) {
            validScan = true;
            var nofind = 0;
            $(".ssearch").val(ssearch);

            getSearchData();

//            if (nofind == 0) {
//                var $row = $(".barcode").last().closest("tr"),
//                        prodtype = $(".prodtype").val();
//                $row.find(".barcode").val(barcode);
//                $.post('show_item_dtl.php', {itemcode: barcode, prodtype: prodtype}, function (data) {
//                    if (barcode != '') {
//                        if (data != '') {
//                            $row.find(".item").html(data).trigger("change");
//
//                            if ($row.find(".stkqty").val() > 0) {
//                                $row.find(".qty").val(1);
//                            } else {
//                                $row.find(".qty").val(0);
//                            }
////                            $row.find(".qty").val(1);
////                        caltran();
////                        AddRows();
//                            dont = 1;
//                            $row.find(".discperc").focus();
//
//                            //                $row.find(".item").html(data).trigger("change");
//
//                        } else {
//                            $row.find(".barcode").val("");
//                            alert("Barcode does not exist.");
//                        }
//                    }
//                    //            $row.find(".itemcode").val(itemcode);
//                });
//            }


        } // main callback function	,
        ,
        onError: function (string, qty) {

//	$('#userInput').val ($('#userInput').val()  + string);

        }


    });

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    function clickRow() {
        window.document.location = $(this).data("href");
    }
    $(".snoRecord").hide();
    $(".sloader").hide();
    var imrangeFrom = 0,
            imrangeTo = 40;
    $(".ssearch").focus();
//    $(".ssearch").val("");
//    $(".sfield").val("1");
//    getSearchData();

    function getSearchData(pagination) {

        var ssearch = $(".ssearch").val();
        var ssearch2 = $(".ssearch2").val();
        if (ssearch.length > 3 || ssearch2.length > 3) {

            $(".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(),
//                ssearch = $(".ssearch").val(),
                    sfield = $(".sfield").val(),
                    sortBy = $(".sortBy").val(),
                    orderAs = $(".orderAs").val(),
                    soperator = $(".soperator").val(),
                    data = {ssearch: ssearch, ssearch2: ssearch2, sfield: sfield, sortBy: sortBy, orderAs: orderAs, soperator: soperator,
                        series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo, type: type},
                    values = "";
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "visamofa_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=''>" +
                                    "<td align='left'>" + this.manualno + "</td>" +
                                    "<td align='left'>" + this.visatype + "</td>" +
                                    "<td align='left'>" + this.partyname + "</td>" +
                                    "<td align='left'>" + this.passport + "</td>" +
                                    "<td align='left'>" + this.sponsor + "</td>" +
                                    "<td align='left'>" + this.broker + "</td>" +
                                    "<td align='left'>" + this.idno + "</td>" +
                                    "<td align='left'>" + this.mofano + "</td>" +
                                    "<td align='left'>" + this.mofano2 + "</td>" +
                                    "<td align='left'>" + this.visano + "</td>" +
                                    "<td align='left'>" + this.vouchno + "</td>" +
                                    "<td align='left'>" + this.vouchdt + "</td>" +
                                    "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                                    "</tr>";
                        });
                        $(".sloader").hide();
                        $(".sTbody").empty();
                        $(".sTbody").html(values);
                        if (response) {
                            $(".search-action-bar").show();
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {

                }
            });
        } else {
            $(".sloader").show();
            $(".sTbody").empty();
        }
    }
    $("#search").click(function () {
        $(".searchModal").on('shown.bs.modal', function () {
            $(".ssearch").focus();
            $(".ssearch").val("");
            $(".sfield").val("8");
            getSearchData();
        });
        getSearchData();
    });
    $(".sprev").click(function () {
        getSearchData('prev');
    });
    $(".snext").click(function () {
        getSearchData('next');
    });
    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////
});