<!-- -->
<template>
  <el-menu
    v-if="items.length > 0"
    class="sidebar-el-menu"
    default-active=""
    @open="handleOpen"
    @close="handleClose"
    :default-openeds="defaultOpends"
  >
    <!-- 遍历菜单 -->
    <template v-for="item in items">
      <!-- 含有子菜单 -->
      <template v-if="item.children">
        <!-- 第一层 含有子菜单菜单 -->
        <el-submenu :index="item.euId" :key="item.title">
          <template slot="title">
            <div slot="title" @click="handleClick(item)">{{ item.title }}</div>
          </template>
          <menu-list
            :items="item.children"
            v-bind="$attrs"
            v-on="$listeners"
          ></menu-list
          ><!--递归调用-->
        </el-submenu>
      </template>

      <!-- 第一层 不含子菜单  -->
      <template v-else>
        <el-menu-item :index="item.euId" :key="item.title">
          <div slot="title" @click="handleClick(item)">{{ item.title }}</div>
        </el-menu-item>
      </template>
    </template>
  </el-menu>
</template>

<script>
export default {
  name: "MenuList",
  props: {
    items: Array,
    isCollapse: Boolean,
  },
  inheritAttrs: false,
  watch: {
    items: {
      handler: function (value) {
        var that = this;
        traveItems(value);
        function traveItems(value) {
          // console.log(999,that);
          if (value && value.length > 0) {
            value.map((item) => {
              if (item.euId && that.defaultOpends.indexOf(item.euId) == -1) {
                that.defaultOpends.push(item.euId);
              }
              if (item.children) {
                if (item.children.length > 0) {
                  traveItems(item.children);
                } else {
                  delete item.children;
                }
              }
            });
          }
        }
      },
      deep: true,
      immediate: true,
    },
  },
  data() {
    return {
      defaultOpends: [],
    };
  },
  methods: {
    handleOpen(key, keyPath) {},
    handleClose(key, keyPath) {
      // console.log(key, keyPath);
    },
    handleClick(item) {
      // console.log(item.title);
      this.$emit("open", item);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-menu {
  border-right: none;
}
.el-menu-item {
  background-color: #ecf5ff;
  color: #2069c4;
  &:hover {
    background-color: #2069c4;
    color: #fff;
  }
}
</style>
