$(document).ready(function () {
    $("body").on("click", ".clickable-row", clickRow);
    $(".searchModal").on("click", ".clickable-row", clickRow);
    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("change", ".soperator", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    $("body").on("change", "#destination", getMainData);

    getSearchData();    
    
    getMainData();

    function getMainData() {
        var destination = $(".destination").val();

        if (destination != "") {
            $.post("get_deststockrep.php", { destination: destination },
                function (data) {
                    if (data != "") {                        
                        $(".destination").focus();
                        $(".dataDiv").html(data);
                        getSearchData();
                    }
                }
            );
        } else {
            $(".dataDiv").html("");
        }

    }
    
    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    function clickRow() {
        window.document.location = $(this).data("href");
    }

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

    var imrangeFrom = 0,
        imrangeTo = 30;

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

    function getSearchData(pagination) {

        $(".sloader").show();

        if (pagination == 'prev') {
            if (imrangeFrom > 0) {
                imrangeFrom = (imrangeFrom - 30);
                imrangeTo = (imrangeTo - 30);
            }
        }
        if (pagination == 'next') {
            imrangeFrom = (imrangeFrom + 30);
            imrangeTo = (imrangeTo + 30);
        }

        var destination = $(".destination").val(),
            type = $(".type").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,
                rangeFrom: imrangeFrom, rangeTo: imrangeTo, type: type, destination: destination
            },
            values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "deststockrep_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='clickable-row2' data-href='" + this.action + "' style='" + this.bgcolr + "'>";

                        values += "<td align='center'><b>" + this.vouchno + "</b></td>" +

                            "<td align='center'><b>" + this.vouchdt + "</b></td>" +
                            "<td align='left'><b>" + this.customer + "</b></td>" +
                            "<td align='center'><b>" + this.nocart + "</b></td>" +
                            "<td align='left'><b>" + this.supplier + "</b></td>" +
                            "<td align='left'><b>" + this.description + "</b></td>" +
                            "<td align='center'><b>" + this.pcs + "</b></td>" +
                            "<td align='center'><b>" + this.unit + "</b></td>" +
                            "<td align='center'><b>" + this.marka + "</b></td>" +
                            "<td align='right'><b>" + this.totcbm + "</b></td>" +
                            "<td align='right'><b>" + this.weight + "</b></td>" +
                            "<td align='left'><b>" + this.containerno + "</b></td>" +
                            "<td align='center'><b>" + this.saildate + "</b></td>" +
                            "<td align='left'><b>" + this.origin + "</b></td>" +
                            "<td align='center'><b>" + this.estimdate + "</b></td>" +
                            "<td align='center'><b>" + this.delvdate + "</b></td>" +
                            "<td align='left'><b>" + this.mobile + "</b></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("8");
            getSearchData();
        });

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


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