<template>
  <div class="sidebar-item" v-if="!item.meta || !item.meta.hidden">
    <template
      v-if="!alwaysShowRootMenu && theOnlyOneChild && !theOnlyOneChild.children"
    >
      <router-link
        v-if="theOnlyOneChild.meta"
        :to="resolvePath(theOnlyOneChild.path)"
      >
        <el-menu-item :index="resolvePath(theOnlyOneChild.path)">
          <svg-icon
            v-if="theOnlyOneChild.meta.icon"
            :name="theOnlyOneChild.meta.icon"
          />
          <span v-if="theOnlyOneChild.meta.title" slot="title">{{
            theOnlyOneChild.meta.title
          }}</span>
        </el-menu-item>
      </router-link>
    </template>
    <el-submenu v-else :index="resolvePath(item.path)">
      <template slot="title">
        <svg-icon v-if="item.meta && item.meta.icon" :name="item.meta.icon" />
        <span v-if="item.meta && item.meta.title">{{ item.meta.title }}</span>
      </template>
      <template v-if="item.children">
        <sidebar-item
          v-for="child in item.children"
          :key="child.path"
          :value="child"
          :base-path="resolvePath(child.path)"
        />
      </template>
    </el-submenu>
  </div>
</template>

<script lang="ts">
import { resolve } from "@/utils/path";
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import { RouteConfig } from "vue-router";

@Component({
  // Set 'name' here to prevent uglifyjs from causing recursive component not work
  // See https://medium.com/haiiro-io/element-component-name-with-vue-class-component-f3b435656561 for detail
  name: "SidebarItem",
})
export default class extends Vue {
  @Prop({ required: true }) value!: RouteConfig;
  @Prop({ default: "" }) private basePath!: string;
  item: RouteConfig;

  @Watch("data", { immediate: true })
  onDataChange() {
    this.item = JSON.parse(JSON.stringify(this.value));
  }

  get alwaysShowRootMenu() {
    if (this.item.meta && this.item.meta.alwaysShow) {
      return true;
    }
    return false;
  }

  get showingChildNumber() {
    if (this.item.children) {
      const showingChildren = this.item.children.filter((item) => {
        if (item.meta && item.meta.hidden) {
          return false;
        } else {
          return true;
        }
      });
      return showingChildren.length;
    }
    return 0;
  }

  get theOnlyOneChild() {
    if (this.showingChildNumber > 1) {
      return null;
    }
    if (this.item.children) {
      for (const child of this.item.children) {
        if (!child.meta || !child.meta.hidden) {
          //如果只有有个子路路由，子路由等级提升，继承父路由icon
          child.meta = child.meta || {};
          child.meta.icon = child.meta.icon || (this.item.meta || {}).icon;
          return child;
        }
      }
    }
    // If there is no children, return itself with path removed,
    // because this.basePath already conatins item's path information
    return { ...this.item, path: "" };
  }

  resolvePath(routePath: string) {
    return resolve(this.basePath, routePath);
  }
}
</script>

<style lang="scss">
.el-menu:not(.el-menu--popup) .sidebar-item {
  .el-submenu__title,
  .el-menu-item {
    position: relative;
    height: 48px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    &::after {
      content: "";
      left: 20px;
      right: 20px;
      height: 2px;
      display: block;
      background: transparent;
      transition: background 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      position: absolute;
      bottom: 0;
    }
  }
  .el-submenu.is-active > .el-submenu__title,
  .el-submenu.is-opened > .el-submenu__title,
  .el-menu-item.is-active,
  .el-submenu > .el-submenu__title:hover,
  .el-menu-item:hover {
    color: var(--theme-color) !important;
    background: transparent;
    &::after {
      background: currentColor;
    }
  }
  .el-icon-arrow-down {
    display: none;
  }
}
</style>

<style lang="scss" scoped>
.svg-icon {
  margin-right: 10px;
}
</style>
