<template>
  <div
    v-if="!item.hidden"
    :class="parentActive(item) ? 'isActive menu-wrapper' : 'menu-wrapper'"
    @click="selectCurr(item)"
  >
    <template
      v-if="
        hasOneShowingChild(item.children, item) &&
          (!onlyOneChild.children || onlyOneChild.noShowingChildren) &&
          !item.alwaysShow
      "
    >
      <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
        <el-menu-item
          :index="resolvePath(onlyOneChild.path)"
          :class="{ 'submenu-title-noDropdown': !isNest }"
        >
          <item
            :icon="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"
            :title="onlyOneChild.meta.title"
          />
        </el-menu-item>
      </app-link>
    </template>

    <el-menu-item
      v-else
      ref="subMenu"
      :index="resolvePath(item.path)"
      popper-append-to-body
    >
      <div slot="title" @click="current(item)">
        <item
          v-if="item.meta"
          :icon="item.meta && item.meta.icon"
          :title="item.meta.title"
        />
      </div>
    </el-menu-item>
  </div>
</template>

<script>
import path from "path";
import { isExternal } from "@/utils/validate";
import Item from "./Item";
import AppLink from "./Link";
import FixiOSBug from "./FixiOSBug";

export default {
  name: "SidebarItem",
  components: { Item, AppLink },
  mixins: [FixiOSBug],
  props: {
    item: {
      type: Object,
      required: true
    },
    isNest: {
      type: Boolean,
      default: false
    },
    basePath: {
      type: String,
      default: ""
    }
  },
  data() {
    this.onlyOneChild = null;
    return {
      currentname: ""
    };
  },
  methods: {
    current(item) {
      this.$router.push(item.redirect);
    },
    parentActive(item) {
      //选中出发二级
      let itemChildren = item.children;
      if (itemChildren && itemChildren.length > 0) {
        for (let idx in itemChildren) {
          let child = itemChildren[idx];
          let currPath = this.resolvePath(child.path);
          if (currPath === this.$route.path) {
            this.selectCurr(item);
            return true;
          }
        }
      }
    },
    selectCurr(routerObj) {
      let hasOne = this.hasOneShowingChild(routerObj.children, routerObj);
      if (!hasOne) {
        //有子项
        this.$emit("retParentObj", routerObj);
      } else {
        this.$emit("retParentObj", null);
      }
    },
    hasOneShowingChild(children = [], parent) {
      const showingChildren = children.filter(item => {
        if (item.hidden) {
          return false;
        } else {
          this.onlyOneChild = item;
          return true;
        }
      });
      if (showingChildren.length === 1) {
        return true;
      }
      if (showingChildren.length === 0) {
        this.onlyOneChild = { ...parent, path: "", noShowingChildren: true };
        return true;
      }
      return false;
    },
    resolvePath(routePath) {
      if (isExternal(routePath)) {
        return routePath;
      }
      if (isExternal(this.basePath)) {
        return this.basePath;
      }
      return path.resolve(this.basePath, routePath);
    }
  }
};
</script>

<style scoped>
.el-menu-item {
  font-size: 14px;
  /*font-weight: 600;*/
}
.none {
  color: bfcbd9;
}
.blue {
  color: #1890ff;
}
</style>
