<template>
  <div
    :class="{'has-logo':showLogo, [sidebarClass]: true}"
    class="ec-sidebar"
  >
    <logo
      v-if="showLogo"
      :collapse="isCollapse"
    />
    <el-scrollbar
      wrap-class="scrollbar-wrapper"
      :style="sidebarLayoutSkin === 'primary' ?`background:${defaultTheme}`: ''"
    >
      <el-menu
        class="ec-sidebar__menu"
        :default-active="menuActiveName"
        :collapse="isCollapse"
        :unique-opened="true"
        :collapse-transition="false"
        mode="vertical"
      >
        <!--        <sidebar-item v-for="route in permission_routes" :key="route.path" :item="route" :base-path="route.path" />-->
        <sidebar-item
          v-for="route in leftMenuList"
          :key="route.id || route.path"
          :item="route"
          :base-path="getRouteBasePath(route)"
        />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Logo from './Logo'
import SidebarItem from './SidebarItem'
import variables from 'ecip-web/styles/variables.scss'
import { isExternal } from 'ecip-web/utils/validate'
import { unescapeHTML } from 'ecip-web/utils'

export default {
  components: { SidebarItem, Logo },
  computed: {
    ...mapGetters(['permission_routes', 'sidebar', 'device']),
    sidebarClass() {
      return 'ec-sidebar--' + this.sidebarLayoutSkin
    },
    defaultTheme() {
      if (this.$store.state.settings.darkTheme) {
        return ''
      }
      return this.$store.state.settings.defaultTheme
    },
    sidebarLayoutSkin() {
      if (this.$store.state.settings.darkTheme) {
        return ''
      }
      return this.$store.state.settings.sidebarLayoutSkin
    },
    leftMenuList() {
      return this.$store.state.app.leftMenuList
    },
    leftMenuBasePath() {
      return this.$store.state.app.leftMenuBasePath
    },
    defaultLayout() {
      return this.$store.state.settings.defaultLayout
    },
    activeMenu() {
      const route = this.$route
      const { meta, path } = route
      // 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
    },
    menuActiveName: {
      get() {
        if (this.$route.meta && this.$route.meta.isIframe) {
          return this.$store.state.app.menuActiveName
        }
        return this.$route.name || this.$route.path
      },
      set(val) {
        this.$store.commit('app/SET_MENU_ACTIVE_NAME', val)
      }
    },
    currentIframeRoute: {
      get() {
        return this.$store.state.tagsView.currentIframeRoute
      }
    }
  },
  created() {
    if (this.$route.meta.isIframe) {
      return
    }
    const menuId = this.$route.name || this.$route.path
    // console.log(this.$route, menuId)
    this.menuActiveName = menuId + ''
  },
  methods: {
    getRouteBasePath(route) {
      // console.log(route)
      if (isExternal(route.path) || route.path.startsWith('/')) {
        return unescapeHTML(route.path)
      }
      if (this.leftMenuBasePath) {
        return this.leftMenuBasePath + '/' + route.path
      }
      return route.path
    }
  }
}
</script>
