<template>
  <div class="sidebar-wrapper">
    <div class="sidebar-header">
      <i class="el-icon-menu" />
      <span>菜单入口</span>
    </div>
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color="'#fafbff'"
        :text-color="'#555'"
        :unique-opened="true"
        :active-text-color="'#4f46e5'"
        :collapse-transition="false"
        mode="vertical"
        class="sidebar-menu"
      >
        <sidebar-item
          v-for="(route, index) in sidebarRouters"
          :key="route.path + index"
          :item="route"
          :base-path="sidebarBasePath(route)"
        />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SidebarItem from './SidebarItem'

export default {
  components: { SidebarItem },
  computed: {
    ...mapGetters(['sidebarRouters', 'sidebar']),
    activeMenu() {
      const route = this.$route
      const { meta, path } = route
      if (meta.activeMenu) {
        return meta.activeMenu
      }
      return path
    },
    isCollapse() {
      return !this.sidebar.opened
    }
  },
  methods: {
    sidebarBasePath(route) {
      const p = route && route.path
      if (p && p.startsWith('/')) {
        return ''
      }
      return p || ''
    }
  }
}
</script>

<style lang="scss" scoped>
.sidebar-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  .sidebar-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 12px;
    font-weight: 600;
    color: #999;
    letter-spacing: 0.5px;
    padding: 16px 16px 10px;
    border-bottom: 1px solid #f0f0f0;
    background: #fafbff;

    .el-icon-menu {
      font-size: 14px;
      color: #bbb;
    }
  }

  .el-scrollbar {
    flex: 1;
    min-height: 0;
  }

  .sidebar-menu {
    border: none;
    width: 100% !important;
  }
}
</style>
