//导航收缩展开
bind_event($(".canvas-nav-item>a"), function () {
    if (!$('.canvas-nav-item').hasClass('canvas-nav-small')) {
        if ($(this).next().css('display') == "none") {
            //展开未展开
            $('.canvas-nav-item').children('ul').slideUp(300);
            $(this).next('ul').slideDown(300);
            $(this).parent('li').addClass('canvas-nav-show').siblings('li').removeClass('canvas-nav-show');
        } else {
            //收缩已展开
            $(this).next('ul').slideUp(300);
            $('.canvas-nav-item.canvas-nav-show').removeClass('canvas-nav-show');
        }
    }
});
// 收放导航列表
bind_event($(".switch-list"), function () {
    //收缩按钮切换
    if ($(this).children("i").hasClass("layui-icon layui-icon-shrink-right")) {
        $(this).children("i").removeClass("layui-icon layui-icon-shrink-right");
        $(this).children("i").addClass("layui-icon layui-icon-spread-left");
    } else {
        $(this).children("i").removeClass("layui-icon layui-icon-spread-left");
        $(this).children("i").addClass("layui-icon layui-icon-shrink-right");
    }

    /*canvas-nav-small切换*/
    if ($('.layout-body').hasClass('canvas-nav-small')) {
        $('.layout-body').removeClass('canvas-nav-small');
    } else {
        $('.canvas-nav-item').removeClass('canvas-nav-show');
        $('.canvas-nav-item').children('ul').removeAttr('style');
        $('.layout-body').addClass('canvas-nav-small');
    }
});


//全屏
function fullScreen(id) {
    var el = document.documentElement;
    var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen;
    if (typeof rfs != "undefined" && rfs) {
        rfs.call(el);
    } else {
        msg("浏览器不支持全屏调用，请使用其他浏览器或按F11键切换全屏！");
        return;
    }

    $("#" + id).attr("onclick", "exitScreen('" + id + "')");
    $("#" + id).attr("onmouseenter", "getTips(this, '退出全屏')");
    $("#" + id).find("i").attr("class", "fa fa-arrows");
}

//退出全屏
function exitScreen(id) {
    if (document.exitFullscreen) {
        document.exitFullscreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
    } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
    }
    if (typeof cfs != "undefined" && cfs) {
        cfs.call(el);
    }

    $("#" + id).find("i").attr("class", "fa fa-arrows-alt");
    $("#" + id).attr("onclick", "fullScreen('" + id + "')");
    $("#" + id).attr("onmouseenter", "getTips(this, '全屏')");
}

//判断是否IE浏览器
function IEVersion() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
    var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
    var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    if(isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if(fIEVersion == 7) {
            return 7;
        } else if(fIEVersion == 8) {
            return 8;
        } else if(fIEVersion == 9) {
            return 9;
        } else if(fIEVersion == 10) {
            return 10;
        } else {
            return 6;//IE版本<=7
        }
    } else if(isEdge) {
        return 'edge';//edge
    } else if(isIE11) {
        return 11; //IE11
    }else{
        return -1;//不是ie浏览器
    }
}


$(function () {
    $('.tabs').on('click', function (event) {
        event.preventDefault();
        var $this = $(this), url = $this.attr('href'),
            title = $.trim($this.text());
        var id= $this.attr('id');
        var icons = $this.attr('icons');
        if (url && url !== 'javascript:;') {
            addtabs(id,title,icons,url,true);
        }
    });

    var currentTabMenuId = 'tabs-menu-0';
    var currentTabContentId = 'tabs-content-0';
    var pageCounter = 0;
    /**
     * id:       tab页签的html标签ID属性格式为"tab-"+id，内容容器的html标签ID格式为"tab-content-"+id
     * title:     tab页签的显示文本
     * url:      打开的iframe的url
     * innerTab: 是否是内部弹出页（打开的tab页触发添加新的tab页），默认为undefined/false
     */
    function addtabs(id,title,icons,url,innerTab) {

        //如果某个页面已经打开，则切换到该页显示即可，不会新添加tab页
        if ($('#tabs-menu-' + id).length > 0) {
            currentTabMenuId = "tabs-menu-"+id;
            $('#tabs-menu-' + id).addClass('active').siblings().removeClass('active');
            // 标签对应的内容容器切换
            currentTabContentId = "tabs-content-"+id;
            $(".canvas-tabs-content > div").eq($('#tabs-content-' + id).index()).addClass('active').siblings().removeClass('active');
        } else {
            var tab_id = "tabs-menu-" + id,
                tab_content_id = "tabs-content-" + id;

            currentTabMenuId = tab_id;
            //添加tab页签
            $(".canvas-tabs-menu > li").removeClass("active");
            $(".canvas-tabs-menu").append("<li id='" + tab_id + "' class='active'><div class='hover-box'></div><i class=''></i>" + title + "<i class='layui-icon layui-icon-close icon-close-padd'></i></li>");

            //添加新的内容显示
            currentTabContentId = tab_content_id;
            $(".canvas-tabs-content > div").removeClass("active");
            $(".canvas-tabs-content").append("<div id='" + tab_content_id + "' class='active'>"
                + "<iframe id='iframepage" + (pageCounter++) + "' name='iframepage" + (pageCounter++)
                + "' width='100%' frameborder='0' scrolling='yes'  src='" + url + "' class='x-iframe'></iframe></div>");
        }
    }

    //切换tab页的显示
    $(document).on('click', '.canvas-tabs-menu > li', function (e) {
        //清除原来显示的tab页,设置新的显示tab页
        var _this = $(this)
        currentTabMenuId = _this.attr("id");
        _this.addClass('active').siblings(_this).removeClass('active');
        // 标签对应的内容容器切换
        var t_id=currentTabMenuId.substr(currentTabMenuId.length-1,1);
        currentTabContentId = "tabs-content-"+t_id;
        $(".canvas-tabs-content > div").eq(_this.index()).addClass('active').siblings().removeClass('active');
    })

    //删除目标tab页的显示
    $(document).on('click', '.canvas-tabs-menu .icon-close-padd', function (e) {
        e.stopPropagation();
        //获取tabs列表
        var u_l = $(".canvas-tabs-menu > li");
        //删除目标tab页
        var _this = $(this);
        //获取当前tabs索引
        var num = _this.parent().index();
        //获取当前tabs id
        var id = _this.parent().attr('id');
        //删除当前tabs
        $("#"+id).remove();
        // 删除目标内容容器
        var t_id=id.substr(id.length-1,1);
        $("#tabs-content-"+t_id).remove();

        var u_t=u_l.eq(num-1).attr('id');
        var flag = $("#"+u_t).hasClass("active");

        if(flag){

        }else{
            currentTabMenuId = u_t;
            $("#"+u_t).addClass("active").siblings($("#"+u_t)).removeClass('active');

            var t_c_id=u_t.substr(u_t.length-1,1);
            currentTabContentId = "tabs-content-"+t_c_id;
            $("#tabs-content-"+t_c_id).addClass("active").siblings($("#tabs-content-"+t_c_id)).removeClass('active');
        }
    })

    //删除当前页
    $(document).on('click', '.closeNowPage', function (e) {
        e.stopPropagation();
        if(!(currentTabMenuId === 'tabs-menu-0')){
            //获取tabs列表
            var u_l = $(".canvas-tabs-menu > li");
            //获取当前tabs索引
            var num = $("#"+currentTabMenuId).index();
            //删除当前tabs
            $("#"+currentTabMenuId).remove();
            // 删除目标内容容器
            var t_id=currentTabMenuId.substr(currentTabMenuId.length-1,1);
            $("#tabs-content-"+t_id).remove();

            var u_t=u_l.eq(num-1).attr('id');
            var flag = $("#"+u_t).hasClass("active");

            if(!flag){
                currentTabMenuId = u_t;
                $("#"+u_t).addClass("active").siblings($("#"+u_t)).removeClass('active');

                var t_c_id=u_t.substr(u_t.length-1,1);
                currentTabContentId = "tabs-content-"+t_c_id;
                $("#tabs-content-"+t_c_id).addClass("active").siblings($("#tabs-content-"+t_c_id)).removeClass('active');
            }
        }
    })

    //删除其它页
    $(document).on('click', '.closeOtherPage', function (e) {
        e.stopPropagation();
        if(currentTabMenuId === 'tabs-menu-0'){
            //删除其它tabs
            $("#tabs-menu-0").siblings($("#tabs-menu-0")).remove();
            $("#tabs-content-0").siblings($("#tabs-content-0")).remove();
            currentTabMenuId = "tabs-menu-0";
            $("#tabs-menu-0").addClass("active")
            currentTabContentId = "tabs-content-0";
            $("#tabs-content-0").addClass("active")
        }else{
            $("#"+currentTabMenuId).nextAll().remove();
            $("#"+currentTabMenuId).prevUntil($("#tabs-menu-0")).remove();

            $("#"+currentTabContentId).nextAll().remove();
            $("#"+currentTabContentId).prevUntil($("#tabs-content-0")).remove();
        }
    })

    //删除所有页
    $(document).on('click', '.closeAllPage', function (e) {
        e.stopPropagation();
        //删除其它tabs
        $("#tabs-menu-0").siblings($("#tabs-menu-0")).remove();
        $("#tabs-content-0").siblings($("#tabs-content-0")).remove();
        currentTabMenuId = "tabs-menu-0";
        $("#tabs-menu-0").addClass("active")
        currentTabContentId = "tabs-content-0";
        $("#tabs-content-0").addClass("active")
    })
    // 呼出右侧列表
    $(document).on('click', '.dashboard,.user-info', function (e) {
        var bar = $(this).attr("tag");
        if($(".right-bar").hasClass(bar)){
            remove_node($(".right-bar").find($(".tpl-shade")));
            if ($("."+bar).css("right") == "-1500px"){
                $(".right-bar").css("right","-1500px");
                $("."+bar).css("right",0);
                $(".tpl-body").css("overflow","hidden");
            } else {
                $("."+bar).css("right","-1500px");
                $(".tpl-body").css("overflow","auto");
            }
            // shade($("."+bar).find(".right-bar-fluid"),true);
            stopPropagation(e);
        }else{
            msg("配置的tag目标为空");
        }
    })
    $(document).on('click', '.right-bar', function (e) {
        stopPropagation(e);
    })
    // 页面的点击冒泡
    $(document).on('click', '.right-bar', function (e) {
        $(".right-bar").css("right","-1500px");
    })
    bind_event($(document),function(){
        $(".right-bar").css("right","-1500px");
    })
    // 点击更换页面主题
    bind_event($("#style-list"),function(e){
        var style = $(this).attr("theme-style");
        var styleName = $(".theme-style").attr("style-name");
        $(".theme-style").removeClass(styleName);
        $(".theme-style").attr("style-name",style);
        $(".theme-style").addClass(style);
        /*$.ajax({ //使用服务器端session保存操作
            url:"../index/view",
            type:"post",
            datatype:"json",
            data:{
                stylename:style // 传递到后台，用session保存即可
            },
            success:function(data){
                l(data);
            }
        });*/
        stopPropagation(e);
    },"click","li");
});

layui.define(['layer', 'jquery', 'form', 'carousel', 'element', 'upload'], function (exports) {
    var $ = layui.$, $body = $('body');
    //弹出层组件
    var layer = layui.layer;
    //表单组件
    var form = layui.form;
    var element = layui.element;
    //轮播组件
    var carousel = layui.carousel;
    // 文件上传组件
    var upload = layui.upload;
});