<template>
  <div :class="{'has-logo':showLogo}">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color="variables.menuBg"
        :text-color="variables.menuText"
        :unique-opened="false"
        :active-text-color="variables.menuActiveText"
        :collapse-transition="false"
        mode="vertical"
        @open="handleOpen"
        :default-openeds="openedsindex"
      >
        <sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
      </el-menu>

      <!-- 单独新开窗口跳转链接 -->
      <div><a href="/" target="_blank" class="router-link-active"><li role="menuitem" tabindex="-1" class="el-menu-item submenu-title-noDropdown" style="padding-left: 20px; color: rgb(191, 203, 217); background-color: rgb(43, 50, 69);"><svg data-v-c8a70580="" aria-hidden="true" class="svg-icon" data-v-31ea41b3=""><use data-v-c8a70580="" xlink:href="#icon-link"></use></svg><span data-v-31ea41b3="">前端展示页面</span></li></a></div>
    </el-scrollbar>
    
  </div>
</template>

<script>

import { mapGetters } from 'vuex'
import EventBus from '@/utils/EventBus.js'
import Logo from './Logo'
import SidebarItem from './SidebarItem'
import variables from '@/styles/variables.scss'
import menusdata from './leftMenusData.js'
import { mirlist } from '@/api/user'

//console.log(menusdata);
export default {
  components: { SidebarItem, Logo },

  created: function () {
    this.getmunusdata();
    
  },

  mounted() {
    EventBus.$on('mkdirSuccess', this.getmunusdata); //刷新菜单栏事件
    //EventBus.$on('menuopemdsc', this.menuopeneds); //展开菜单栏事件
    let winpach = window.location.href;
    if (winpach.indexOf('filemange/index') != -1) {
        this.menuopeneds();
      }
    
    //console.log(this.$root);
  },

  data() {
    return {
        routes:menusdata,
        // 默认展开的菜单index ，这里submen的index值就是当前路径
        openedsindex:[]
    }
  },
  computed: {
    ...mapGetters([
      'sidebar'
    ]),
    // routes() {
    //   return this.$router.options.routes
    // },

    // 获取当前激活菜单的index，默认选中的菜单
    activeMenu() {
      const route = this.$route
      const { meta, path } = route
      const menusindex = window.location.href
        
      //  因为菜单栏的生成和路由是解耦的，因为动态获取以及添加的菜单都是用的同一个path带不同参数，路由没法获取，所以通过获取地址来选中
      if (menusindex.indexOf('?id') != -1) {
        return menusindex.split('/#')[1]
      }
      
      // if set path, the sidebar will highlight the path you set
      if (meta.activeMenu) {
        return meta.activeMenu  //当前激活的菜单
      }
      return path
    },
    showLogo() {
      return this.$store.state.settings.sidebarLogo
    },
    variables() {
      return variables
    },
    isCollapse() {
      return !this.sidebar.opened
    }
  },

  methods: {

    menuopeneds() {
        window.setTimeout(() => {
            this.openedsindex = ['/filemange/index'];     
        }, 400);
       
    },
     SplicingMenuTree(menus) {
        let newMenu = null, arrMenus = [];
        let nuni = 2;
        menus.forEach(item => {
            //console.log(menusdata[0]);
            // if (!this.menusdata.children) {
            //     this.$set(this.menusdata,'children', [])
            //     //menusdata[0].children = []
            // }
            newMenu = {
                path: '/filemange/subfile?id='+item.id,
                meta: { title: item.directory },
                class: 'file',
                id: item.id,
                pid: item.pid,
                children:[]
            }
            if (item.directories && item.directories.length > 0) {
                newMenu.children = this.SplicingMenuTree(item.directories)
            }

            arrMenus.push(newMenu);   
            //nuni++; 
        });
        return arrMenus;
     },

     getmunusdata() {
        let thiss = this;
        //console.log(1);
        // console.log(this.$store);
        mirlist().then(response => {
            let datamain = response.data;
            this.routes[1].children = [];
            if (datamain.code == 1) {
                this.routes[1].children = this.SplicingMenuTree(datamain.data.menus);
                //console.log();
                // let dirTree = datamain.data.dirTree;
                // if (dirTree.id) {
                //     //this.$router.push({path: '/filemange/subfile?id='+dirTree.id});
                // }
            }
            

        }).catch(function (error) {
            if (error.response) {
                if (error.response.status == 401) {
					thiss.$message({showClose: true,message: error.response.data.err,type: 'error'});
                    thiss.$router.push({path: '/login'});
                    return;
                }
                thiss.$message({showClose: true,message: error.response.data.err,type: 'error'});
			} else if (error.request) {
				thiss.$message({showClose: true,message: error.response.data.err,type: 'error'});
			} else {
				thiss.$message({showClose: true,message: error.response.data.err,type: 'error'});
			}
        });
     },

     handleOpen(key, keyPath) {
        //console.log(key, keyPath);
      },
  }
}
</script>
