<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"
      >
        <sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
      </el-menu>
    </el-scrollbar>
    
  </div>
</template>

<script>

import { mapGetters } from 'vuex'
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();
  },
  data() {
    return {
        routes:menusdata
    }
  },
  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带不同参数，路由没法获取，所以通过获取地址来选中
      //let getcan = menusindex.split('?id');
      //console.log(menusindex.indexOf('?id'));
      if (menusindex.indexOf('?id') != -1) {
        //console.log(menusindex.split('/#'));
        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: {
     SplicingMenuTree(menus) {
        let newMenu = null, arrMenus = [];
        menus.forEach(item => {
            //console.log(menusdata[0]);
            // if (!this.menusdata.children) {
            //     this.$set(this.menusdata,'children', [])
            //     //menusdata[0].children = []
            // }
            newMenu = {
                path: '/subfile?id='+item.id,
                meta: { title: item.directory, icon: 'dashboard' },
                children:[]
            }
            if (item.directories && item.directories.length > 0) {
                newMenu.children = this.SplicingMenuTree(item.directories)
            }

            arrMenus.push(newMenu);    
        });
        return arrMenus;
     },

     getmunusdata() {
        let thiss = this;
        //console.log(this.$route);
        // console.log(this.$store);
        mirlist().then(response => {
            let datamain = response.data;
            this.routes[1].children = this.SplicingMenuTree(datamain.data.menus);

        }).catch(function (error) {
            thiss.$message({
                showClose: true,
                message: error,
                type: 'error'
            });
        });
     }
  }
}
</script>
