<template>
  <div class="l-flex--col c-page-sidebar">
    <div class="l-flex--row center c-page-sidebar__header">
      <router-link
        tag="img"
        class="c-page-sidebar__logo u-pointer"
        src="/logo.png"
        to="/"
      />
    </div>
    <el-scrollbar
      class="l-flex__auto c-page-sidebar__menu"
      wrap-class="c-page-sidebar__wrapper"
    >
      <el-menu
        mode="vertical"
        :default-active="activeMenu"
        text-color="#a5aec7"
        active-text-color="#fff"
        :unique-opened="false"
        :collapse-transition="false"
      >
        <sidebar-item
          v-for="route in routers"
          :key="route.path"
          :item="route"
        />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

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

function resolve (...args) {
  return path.resolve(...args)
}

function filterRoutes (routes, basePath) {
  const res = []

  routes.forEach(route => {
    if (route.hidden) {
      return
    }
    const { meta = {} } = route
    const path = basePath ? resolve(basePath, route.path) : route.path
    let children = route.children
    if (children) {
      children = filterRoutes(children, path)
      if (!children.length) {
        return
      }
      if (children.length === 1) {
        const { path: cpath, meta: cmeta = {} } = children[0]
        res.push({ path: resolve(path, cpath), meta: { ...meta, ...cmeta } })
        return
      }
      res.push({ path, meta, children })
    } else {
      res.push({ path, meta })
    }
  })

  return res
}

export default {
  name: 'Sidebar',
  components: { SidebarItem },
  computed: {
    ...mapGetters(['permissionRoutes']),
    routers () {
      // 暂时只考虑两层
      return filterRoutes(this.permissionRoutes)
    },
    activeMenu () {
      const path = this.$route.matched[1]?.path
      return path?.replace(/\/$/, '')
    }
  }
}
</script>

<style lang="scss" scoped>
.c-page-sidebar {
  width: 200px;
  background-color: #fff;
  box-shadow: 1px 0 4px rgba(0, 21, 41, 0.08);
  z-index: 9;

  &__header {
    flex: none;
    height: $height--md;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    z-index: 9;
  }

  &__logo {
    max-width: 72%;
    max-height: 64%;
  }

  &__menu {
    ::v-deep {
      .c-page-sidebar__wrapper {
        margin-right: -7px !important;
        overflow-x: hidden;
        scrollbar-width: none;
      }

      .el-scrollbar__bar {
        display: none;
      }

      .el-menu {
        width: 100%;
      }

      .el-menu-item,
      .el-submenu__title {
        display: flex;
        align-items: center;
      }

      .nest-menu .el-menu-item {
        padding-left: 48px !important;
      }

      .el-menu-item,
      .el-submenu__title,
      .el-icon-arrow-down {
        font-weight: bold;
      }

      .el-menu-item.is-active {
        color: #fff !important;
        background-color: $blue !important;
      }

      .is-active > .el-submenu__title,
      .is-active > .el-submenu__title > .el-icon-arrow-down {
        color: $blue !important;
      }

      .svg-icon {
        margin-right: $spacing--xs;
        color: inherit;
        font-size: $font-size--lg;
      }

      .sub-el-icon {
        width: $font-size--lg;
        height: $font-size--lg;
        margin-right: $spacing--xs;
        color: inherit;
      }
    }
  }
}

@media screen and (min-width: 1600px) {
  .c-page-sidebar {
    width: 240px;

    &__header {
      height: $height--2xl;
    }
  }
}
</style>
