<template>
  <div class="menu-wrapper">
    <div class="logo">
      <img src="~imgs/logo.png">
    </div>
    <template
      v-for="item in activeRoutes"
    >
      <router-link
        v-if="hasOneShowingChildren(item.children) && !item.children[0].children&&!item.alwaysShow"
        :to="item.path+'/'+item.children[0].path"
        :key="item.children[0].name"
      >
        <el-menu-item
          :index="item.path+'/'+item.children[0].path"
          :class="{'submenu-title-noDropdown':!isNest}"
        >
          <a-icon
            class="side-icon"
            :type="item.meta.icon"
          />
          <span
            v-if="item.children[0].meta&&item.children[0].meta.title"
            slot="title"
          >
            {{ item.children[0].meta.title }}
          </span>
        </el-menu-item>
      </router-link>

      <el-submenu
        v-else
        :index="item.name||item.path"
        :key="item.name"
      >
        <template slot="title">
          <a-icon
            class="side-icon"
            :type="item.meta.icon"
          />
          <span
            v-if="item.meta&&item.meta.title"
            slot="title"
          >
            {{ item.meta.title }}
          </span>
        </template>

        <template
          v-for="child in getVisibleChildren(item.children)"
        >
          <sidebar-item
            :is-nest="true"
            class="nest-menu"
            v-if="child.children&&child.children.length>0"
            :routes="[child]"
            :key="child.path"
          />

          <router-link
            v-else
            :to="item.path+'/'+child.path"
            :key="child.name"
          >
            <el-menu-item :index="item.path+'/'+child.path">
              <svg-icon
                v-if="child.meta&&child.meta.icon"
                :icon-class="child.meta.icon"
              />
              <span
                v-if="child.meta&&child.meta.title"
                slot="title"
              >
                {{ child.meta.title }}
              </span>
            </el-menu-item>
          </router-link>
        </template>
      </el-submenu>
    </template>
  </div>
</template>

<script>
export default {
  name: 'SidebarItem',
  computed: {
    activeRoutes () {
      return this.routes.filter(item => {
        return !item.hidden && item.children
      })
    }
  },
  props: {
    routes: {
      type: Array,
      default: () => { return [] }
    },
    isNest: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    hasOneShowingChildren (children) {
      const showingChildren = children.filter(item => {
        return !item.hidden
      })
      if (showingChildren.length === 1) {
        return true
      }
      return false
    },
    getVisibleChildren (data) {
      return data.filter(item => {
        return !item.hidden
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.logo{
  text-align: center;
  img{
   height: 50px;padding: 10px 0;
  }
  &:after{
    content: '';display: block; height: 0;
  }
}
.side-icon{
  font-size: 18px; vertical-align: middle !important; margin-right: 5px;
}
</style>
