<template>
  <div v-if="!item.hidden && item.children" class="menu-wrapper">
    <div
      v-if="
        hasOneShowingChild(item.children) &&
          !onlyOneChild.children &&
          !item.alwaysShow
      "
      @click="onClick(item)"
    >
      <router-link :to="resolvePath(onlyOneChild.path)">
        <el-menu-item
          :index="resolvePath(onlyOneChild.path)"
          :class="{ 'submenu-title-noDropdown': !isNest }"
        >
          <svg-icon
            v-if="onlyOneChild.meta && onlyOneChild.meta.icon"
            :icon-class="onlyOneChild.meta.icon"
          ></svg-icon>
          <span
            v-if="onlyOneChild.meta && onlyOneChild.meta.title"
            slot="title"
          >{{ generateTitle(onlyOneChild.meta.title) }}</span>
        </el-menu-item>
      </router-link>
    </div>

    <div v-else>
      <el-submenu v-if="item.meta.isChild" id="mySubmenu" :index="item.name || item.path">
        <template slot="title">
          <svg-icon v-if="item.meta && item.meta.icon" :icon-class="item.meta.icon"></svg-icon>
          <span v-if="item.meta && item.meta.title" slot="title">
            {{
            generateTitle(item.meta.title)
            }}
          </span>
        </template>
        <template v-for="child in item.children" v-if="!child.hidden">
          <router-link
            :to="resolvePath(child.path)"
            v-if="child.meta.isChild != true"
            :key="child.name"
          >
            <el-menu-item @click="onClick(item)" :index="resolvePath(child.path)">
              <svg-icon v-if="child.meta && child.meta.icon" :icon-class="child.meta.icon"></svg-icon>
              <span v-if="child.meta && child.meta.title" slot="title">
                {{
                generateTitle(child.meta.title)
                }}
              </span>
            </el-menu-item>
          </router-link>

          <router-link v-else :to="child.path" :key="child.name">
            <el-menu-item @click="onClick(item,child.parentNum)" :index="resolvePath(child.path)">
              <svg-icon v-if="child.meta && child.meta.icon" :icon-class="child.meta.icon"></svg-icon>
              <span v-if="child.meta && child.meta.title" slot="title">
                {{
                generateTitle(child.meta.title)
                }}
              </span>
            </el-menu-item>
          </router-link>
        </template>
      </el-submenu>
      <div v-else @click="onClick(item)">
        <router-link :to="resolvePath(item.children[0].path)">
          <el-menu-item :index="item.name || item.path">
            <template slot="title">
              <svg-icon v-if="item.meta && item.meta.icon" :icon-class="item.meta.icon"></svg-icon>
              <span v-if="item.meta && item.meta.title" slot="title">
                {{
                generateTitle(item.meta.title)
                }}
              </span>
            </template>

            <!-- <template v-for="child in item.children" v-if="!child.hidden">
        <sidebar-item
          :is-nest="true"
          class="nest-menu"
          v-if="child.children && child.children.length > 0"
          :item="child"
          :key="child.path"
          :base-path="resolvePath(child.path)"
        ></sidebar-item>

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

<script>
import path from "path";
import { generateTitle } from "@/utils/i18n";
import { mapGetters } from "vuex";

export default {
  name: "SidebarItem",
  props: {
    // route object
    item: {
      type: Object,
      required: true
    },
    isNest: {
      type: Boolean,
      default: false
    },
    basePath: {
      type: String,
      default: ""
    },
    index: {
      type: Number
    }
  },
  computed: {
    ...mapGetters(["permission_routers"])
  },
  data() {
    return {
      onlyOneChild: null
    };
  },
  methods: {
    hasOneShowingChild(children) {
      const showingChildren = children.filter(item => {
        if (item.hidden) {
          return false;
        } else {
          // temp set(will be used if only has one showing child )
          this.onlyOneChild = item;
          return true;
        }
      });
      if (showingChildren.length === 1) {
        return true;
      }
      return false;
    },
    resolvePath(...paths) {
      return path.resolve(this.basePath, ...paths);
    },
    generateTitle,
    onClick(index, num) {
      this.permission_routers.map((item, i) => {
        if (this.isObjectValueEqual(item, index)) {
          if (num) {
            this.$emit("a", i, num);
          } else {
            this.$emit("a", i, 999);
          }
        }
      });
    },
    isObjectValueEqual(a, b) {
      var aProps = Object.getOwnPropertyNames(a);
      var bProps = Object.getOwnPropertyNames(b);

      if (aProps.length != bProps.length) {
        return false;
      }

      for (var i = 0; i < aProps.length; i++) {
        var propName = aProps[i];

        if (a[propName] !== b[propName]) {
          return false;
        }
      }

      return true;
    }
  },
  created() {}
};
</script>

<style lang="scss">
#mySubmenu {
  .el-menu {
    display: flex;
    flex-wrap: wrap;
    > a {
      width: 50% !important;
    }
    .el-menu-item {
      min-width: 0;
      padding: 0 5px !important;
      font-size: 12px;
      background-color:#1F2D3D !important;
    }
  }
}
.menu-wrapper{
  .el-menu-item{
   background-color:#1F2D3D !important;
   color: #fff !important;
  }
  .el-menu{
    background-color:#1F2D3D !important;
  }
  .el-submenu__title{
    background-color:#1F2D3D !important;
    color: #fff !important;
    height: 32px !important;
    line-height: 32px !important;
  }
  .router-link-exact-active{
    color: rgb(64, 158, 255) !important;
    .el-menu-item{
      color: rgb(64, 158, 255) !important;
    }
  }
}

</style>
