<template>
  <div class="e-nav-menu">
    <div
      :class="
        value === item[props['key']]
          ? 'nav-menu-item menu-selected'
          : 'nav-menu-item'
      "
      v-for="item in activeList"
      :key="item[props['key']]"
      :style="{
        flexWrap:
          item[props['children']] && item[props['children']].length > 0
            ? 'wrap'
            : '',
        height:
          developList.indexOf(item[props['key']]) >= 0 &&
          item[props['children']] &&
          item[props['children']].length > 0
            ? 'auto'
            : ''
      }"
      @click="handleClick(item)"
    >
      <svg-icon slot="prefix" icon-class="A_yuanjiaodian" class="icon-left" />
      <div class="nav-item-title">{{ item[props["label"]] }}</div>
      <i class="el-icon-arrow-right icon-right"></i>
      <div
        style="height: 100%; width: 100%;"
        v-if="
          developList.indexOf(item[props['key']]) >= 0 &&
          item[props['children']] &&
          item[props['children']].length > 0
        "
      >
        <div
          :class="
            value === childItem[props['key']]
              ? 'nav-menu-item nav-menu-children menu-selected'
              : 'nav-menu-item nav-menu-children'
          "
          v-show="
            developList.indexOf(item[props['key']]) >= 0 &&
            item[props['children']] &&
            item[props['children']].length > 0
          "
          v-for="childItem in item[props['children']]"
          :key="childItem[props['key']]"
          @click.stop="handleClick(childItem)"
        >
          <i
            class="icon iconfont icontongxun icon-left"
            style="margin-right: 5px"
          ></i>
          <div class="nav-item-title">{{ childItem[props["label"]] }}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "ENavMenu",
  props: {
    value: {
      type: [String, Number],
      default: () => {
        return 0;
      },
    },
    props: {
      type: Object,
      default: () => {
        return {
          label: "label",
          key: "key",
          children: "children",
        };
      },
    },
    activeList: {
      type: Array,
      default: () => {
        return [
          {
            label: "项目管理",
            key: "projectOt",
          },
          {
            label: "设备管理",
            key: "projectOt",
          },
        ];
      },
    },
    singleDevelop: {
      type: Boolean,
      default: () => {
        return true;
      },
    },
    defaultDevelop: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      developList: [],
    };
  },
  created() {
    this.developList = this.defaultDevelop;
  },
  methods: {
    handleClick(row) {
      if (
        !row[this.props["children"]] ||
        row[this.props["children"]].length <= 0
      ) {
        this.$emit("input", row);
      } else {
        if (this.developList.indexOf(row[this.props["key"]]) >= 0) {
          this.developList = this.developList.filter((v) => {
            v !== row[this.props["key"]];
          });
        } else {
          // 执行展开操作
          if (this.singleDevelop) {
            this.developList = [];
          }
          this.developList.push(row[this.props["key"]]);
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
.e-nav-menu {
  width: 150px;
  height: 100%;
  padding-top: 15px;
  border-right: 1px solid #e8e8e8;
  .nav-menu-item {
    width: 100%;
    height: 32px;
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    cursor: default;
    .nav-item-title {
      font-size: 14px;
      font-family: "Source Han Sans CN";
      font-weight: 400;
      color: #344567;
      color: #626262;
      height: 32px;
      display: flex;
      align-items: center;
    }
    .icon-left {
      font-size: 16px;
      color: #9da3af;
    }
    .icon-right {
      font-size: 18px;
    }
  }
  .nav-menu-children {
    padding-left: 35px;
    padding-left: 17px;
    justify-content: flex-start;
  }
  .menu-selected {
    background: #f4f5f7;
    .nav-item-title {
      color: #1890ff;
    }
  }
}
</style>
