<template>
    <div class="sidebar">
        <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse"  background-color="#fff"
           text-color="#34383D"    active-text-color="#20B6EB" unique-opened router>
            <template v-for="item in items">
                <template v-if="item.subs">
                    <el-submenu :index="item.index" :key="item.index">
                        <template slot="title">
                            <i :class="item.icon"></i>
                            <span slot="title">{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.subs">
                            <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
                                <template slot="title">{{ subItem.title }}</template>
                                <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.index">{{
                                    threeItem.title }}</el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else :index="subItem.index" :key="subItem.index">{{ subItem.title
                            }}</el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                        <i :class="item.icon"></i>
                        <span slot="title">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from '../common/bus';
import ajaxAuth from '../../script/api/auth.js';
import globalData from '@/components/common/globaldata/global.data.vue';
export default {
    data() {
        return {
            collapse: false,
            buttonperm: [],
            items: [
            ]
        };
    },
    computed: {
        onRoutes() {
            return this.$route.path.replace('/', '');
        }
    },
    methods: {
        whilenode(treeMenu) {
            //遍历这颗树

            let node = [];
            for (let i = 0; i < treeMenu.length; i++) {
                if (treeMenu[i].menuType == 'page' || treeMenu[i].menuType == 'iframe') {
                    //如果是true就是隐藏，不显示再右边菜单，但能用
                    let line = {
                        icon: treeMenu[i].icon,
                        index: treeMenu[i].menuUrl,
                        title: treeMenu[i].menuName
                        //subs: []
                    };
                    let subs = [];
                    if (null != treeMenu[i].childMenu && treeMenu[i].childMenu.length > 0) {
                        subs = this.whilenode(treeMenu[i].childMenu);
                    }
                    if (subs.length > 0) {
                        //console.log(children);
                        line.subs = subs;
                    }
                    node = node.concat(line);
                } else {
                    this.buttonperm.push(treeMenu[i].menuUrl);
                }
            }
            return node;
        },
        userMenu() {
            ajaxAuth
                .$ajaxSelectusermenu({
                    userId: globalData.userInfo.id
                })
                .then(res => {
                    if (res.data.errno == 402 || res.data.errno == '402' || res.data.errno != 0) {
                        this.$message.warning('身份认证失败，请登陆！');
                        this.$router.push('/login');
                    } else {
                        this.items = this.whilenode(res.data.data.menuTree); //系统菜单加载
                        // console.log(this.items);
                        window.localStorage.setItem('menulist', JSON.stringify(res.data.data.menulist));
                        window.localStorage.setItem('buttonperm', JSON.stringify(this.buttonperm));
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        }
    },
    created() {
        // console.log(globalData.userInfo);
        if (globalData.userInfo == null) {
            this.$router.push('/login');
        }
        if (!globalData.userInfo.id) {
            this.$router.push('/login');
        }

        this.userMenu();
        globalData.initUserAuth();
        globalData.initSelfCityStation();
        globalData.initHmCityStation();
        globalData.getLocationAllInOne();
        // console.log('init city station');
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', msg => {
            this.collapse = msg;
        });
    }
};
</script>

<style scoped>
.sidebar >>> .is-active.submenu-title-noDropdown{
 background: #E5F8FF!important;
  border-left:4px solid #20B6EB;
}
.sidebar >>> .is-active>.el-submenu__title{
 background: #E5F8FF!important;
  border-left:4px solid #20B6EB;
}

.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}

.sidebar::-webkit-scrollbar {
    width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
    width: 180px;
}

.sidebar>ul {
    height: 100%;
}
</style>
