/*全局自适应高度*/
/*全局自适应高度*/
function autoFlashHeight(){
    function getObjHeight(select) {
        return select.length > 0 ? select.height() : 0;
    }
    /*侧栏高度*/
    var sBar1 = getObjHeight($(".sjs-bar-1"));
    var sBar2 = getObjHeight($(".sjs-bar-2"));
    var sBar3 = getObjHeight($(".sjs-bar-3"));
    var sBar4 = getObjHeight($(".sjs-bar-4"));
    var sBar5 = getObjHeight($(".sjs-bar-5"));
    /*侧栏高度*/
    var cHeader = getObjHeight($(".c-header"));
    var sBar = getObjHeight($(".sjs-bar"));
    var sBart = getObjHeight($(".sjs-bart"));
    var sBarz = getObjHeight($(".side-bar"));
    var pBarz = getObjHeight($(".toolsbar-f"));
    var bdtopc = getObjHeight($(".body-height-top"));
    var bcontent = getObjHeight($(".bcontent-wrap"));
    $(".sjs-height-0").height($(window).height()-cHeader-90);
    $(".sjs-height-1").height($(window).height()-cHeader-bcontent-90);
    $(".sjs-height-2").height($(window).height()-cHeader-sBarz-110);
    $(".sjs-height-3").height($(window).height()-cHeader-sBar-492);
    $(".sjs-height-4").height($(window).height()-cHeader-pBarz-110);
    $(".sjs-height-5").height($(window).height()-cHeader-sBart-555);
    $(".sjs-height-6").height($(window).height()-cHeader-bdtopc-156);
    $(".sjs-height-7").height($(window).height()-cHeader-sBar-110);
    $(".sp-wrap").height(bcontent-40);
    /*侧栏高度*/
    $(".sjs-sh-1").height($(window).height()-cHeader-sBar1-92);
    $(".sjs-sh-2").height($(window).height()-cHeader-sBar2-92);
    $(".sjs-sh-3").height($(window).height()-cHeader-sBar3-92);
    $(".sjs-sh-4").height($(window).height()-cHeader-sBar4-92);
    $(".sjs-sh-5").height($(window).height()-cHeader-sBar5-92);
};
$(window).resize(autoFlashHeight);
/*全局自适应高度结束*/
$(function(){
    /*侧滑*/
    $(".open-sidebar").click(function(){
        $(".slide-sidebar").animate({width:"800"}).addClass("open");
    });
    $("body").click(function(event){
        var e = event || window.event; //浏览器兼容性
        if(!$(event.target).is('a')) {
            var elem = event.target || e.srcElement;
            while (elem) { //循环判断至跟节点，防止点击的是div子元素
                if (elem.className == "open-sidebar" || elem.className == 'slide-sidebar open') {
                    return false;
                }
                elem = elem.parentNode;
            }
            $(".slide-sidebar").animate({width:"0"}).removeClass("open")// 关闭处理
        }

    });
    /*侧滑*/
    /*工具提示*/
    $(function () {
        $('[data-toggle="tooltip"]').tooltip()
    });
    /*侧栏菜单*/
    $(".bg-nav > li > a").click(function() {
        var self = $(this);
        var subMenu = $(this).siblings('ul.sub-menu');
        if(subMenu.length > 0) {
            if(subMenu.is(":visible")) {
                self.find('.menu-arrow').removeClass('fa-angle-up').addClass('fa-angle-down');
                subMenu.slideUp('fast');
                self.parent().removeClass('active');
            }else{
                self.parent().addClass('active');
                self.find('.menu-arrow').removeClass('fa-angle-down').addClass('fa-angle-up');
                subMenu.slideDown('fast');
            }
        }
    });

    // 数据提交
    $("#submit-form").click(function() {
        $("#save-form").submit();
    });

    // modal弹窗拖动
    // $(document).on("show.bs.modal", ".modal", function() {
    //     $(this).draggable({
    //         handle: ".modal-header"   // 只能点击头部拖动
    //     });
    //     $(this).css("overflow", "hidden");
    // });
});

/**
 * 提示框
 *
 * @param string message
 * @param string type
 * @param string icon
 * @return void
 */
function toast(message, type, icon) {
    var toast = $(".toast");
    toast.addClass(type);
    toast.children('.message').html(message);
    var iconClass = 'fa-' + icon;
    toast.children('.icon').addClass(iconClass);
    toast.fadeIn(500);

    setTimeout(function() {
        toast.fadeOut('fast');
        toast.children('.message').text('');
        toast.children('.icon').removeClass(iconClass);
    }, 3000);
}

/**
 * 动态请求数据
 * @param {String} url - 请求链接
 * @param data - 提交数据
 * @param {function} successCallback - 返回成功回调
 * @param {function} errorCallBack - 返回失败回调
 */
const postData = function (url, data, successCallback, errorCallBack) {
    $.ajax({
        type:"POST",
        url: url,
        data: {'data': JSON.stringify(data)},
        dataType: 'json',
        cache: false,
        timeout: 40000,
        beforeSend: function(xhr) {
            let csrfToken = Cookies.get('csrfToken');
            xhr.setRequestHeader('x-csrf-token', csrfToken);
        },
        success: function(result){
            if (result.err === 0) {
                if (successCallback) {
                    successCallback(result.data);
                }
            } else {
                toast('error: ' + result.msg, 'error', 'exclamation-circle');
                if (errorCallBack) {
                    errorCallBack(result.msg);
                }
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            toast('error ' + textStatus + " " + errorThrown, 'error', 'exclamation-circle');
            if (errorCallBack) {
                errorCallBack();
            }
        }
    });
};

/**
 * 动态请求数据(压缩数据)
 * @param {String} url - 请求链接
 * @param data - 提交数据
 * @param {function} successCallback - 返回成功回调
 * @param {function} errorCallBack - 返回失败回调
 */
const postDataCompress = function (url, data, successCallback, errorCallBack) {
    $.ajax({
        type:"POST",
        url: url,
        data: {'data': LZString.compressToUTF16(JSON.stringify(data))},
        dataType: 'json',
        cache: false,
        timeout: 60000, // 导入清单Excel（10w行）预计需要时间
        beforeSend: function(xhr) {
            let csrfToken = Cookies.get('csrfToken');
            xhr.setRequestHeader('x-csrf-token', csrfToken);
        },
        success: function(result){
            if (result.err === 0) {
                if (successCallback) {
                    successCallback(result.data);
                }
            } else {
                toast('error: ' + result.msg, 'error', 'exclamation-circle');
                if (errorCallBack) {
                    errorCallBack(result.msg);
                }
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            toast('error ' + textStatus + " " + errorThrown, 'error', 'exclamation-circle');
            if (errorCallBack) {
                errorCallBack();
            }
        }
    });
};

/**
 * 动态请求数据
 * @param {String} url - 请求链接
 * @param data - 提交数据
 * @param {function} successCallback - 返回成功回调
 * @param {function} errorCallBack - 返回失败回调
 */
const postDataWithFile = function (url, formData, successCallback, errorCallBack) {
    $.ajax({
        type:"POST",
        url: url,
        data: formData,
        dataType: 'json',
        cache: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        timeout: 5000,
        beforeSend: function(xhr) {
            let csrfToken = Cookies.get('csrfToken');
            xhr.setRequestHeader('x-csrf-token', csrfToken);
        },
        success: function(result){
            if (result.err === 0) {
                if (successCallback) {
                    successCallback(result.data);
                }
            } else {
                toast('error: ' + result.msg, 'error', 'exclamation-circle');
                if (errorCallBack) {
                    errorCallBack();
                }
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            toast('error ' + textStatus + " " + errorThrown, 'error', 'exclamation-circle');
            if (errorCallBack) {
                errorCallBack();
            }
        }
    });
};

/**
 * 获取url中参数
 * @param variable
 * @returns {*}
 */
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return(false);
}

const zeroRange = 0.00000001;
function checkZero(value) {
    return _.isNumber(value) && Math.abs(value) < zeroRange;
}

/**
 * 设置本地缓存
 *
 * @param {String} key
 * @param {String|Number} value
 * @return {void}
 */
function setLocalCache(key, value) {
    const storage = window.localStorage;
    if (!storage || key === '' || value === '') {
        return;
    }

    storage.setItem(key, value);
}

/**
 * 获取本地缓存
 *
 * @param {String} key
 * @return {String}
 */
function getLocalCache(key) {
    const storage = window.localStorage;
    if (!storage || key === '') {
        return null;
    }

    return storage.getItem(key);
}

/**
 * 移除本地缓存
 * @param {String} key
 * @returns {Boolean}
 */
function removeLocalCache(key) {
    const storage = window.localStorage;
    if (!storage || key === '') {
        return null;
    }
    return storage.removeItem(key);
}