<template>
  <div class="e-nav-menu" :style="isShrink ? 'width: 40px' : ''">
    <div
      :class="
        isShrink ? 'e-shrink  is-shrink-true' : 'e-shrink is-shrink-false'
      "
    >
      <i
        class="el-icon-d-arrow-right"
        title="点击展开"
        v-if="isShrink === true"
        @click="handleShrink(false)"
      ></i>
      <i
        class="el-icon-d-arrow-left"
        title="点击折叠"
        v-else-if="isShrink === false"
        @click="handleShrink(true)"
      ></i>
    </div>
    <div v-if="isShrink === false">
      <div
        v-show="isShrink === false"
        :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>
    <div v-if="isShrink === true">
      <div
        v-show="isShrink === true"
        class="nav-menu-item"
        v-for="item in activeList"
        :key="item[props['key']]"
      >
        <el-dropdown
          type="primary"
          placement="right-start"
          :hide-on-click="true"
          trigger="click"
        >
          <svg-icon
            icon-class="A_yuanjiaodian"
            :title="item[props['label']]"
            class="icon-left"
          />

          <el-dropdown-menu
            slot="dropdown"
            class="c-dropdown-c"
            v-if="item[props['children']] && item[props['children']].length > 0"
          >
            <el-dropdown-item
              v-show="
                item[props['children']] && item[props['children']].length > 0
              "
              @click.native="handleClick(menuItem)"
              v-for="(menuItem, idx) in item[props['children']]"
              :key="idx"
              >{{ menuItem[props["label"]] }}</el-dropdown-item
            >
          </el-dropdown-menu>
          <el-dropdown-menu slot="dropdown" v-else>
            <el-dropdown-item @click.native="handleClick(item)">
              {{ item[props["label"]] }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </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: [],
      isShrink: false,
      shrinkShow: true,
    };
  },
  created() {
    this.developList = this.defaultDevelop;
  },
  methods: {
    handleShrink(isTrue) {
      this.isShrink = isTrue;
      this.$emit("eventShrink", isTrue);
    },
    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;
  .e-shrink {
    width: 20px;
    height: 20px;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: left;
    -ms-flex-pack: left;
    border: 1px solid #e9e9e9;
    border-radius: 50%;
    position: relative;
    font-size: 14px;
    font-weight: 400;
    top: -10px;
    justify-content: center;
    background: #ffffff;
  }
  .e-shrink:hover {
    border-color: #4395ff;
    color: #4395ff;
  }
  .is-shrink-true {
    left: 30px;
  }
  .is-shrink-false {
    left: 100%;
    left: calc(100% - 10px);
  }
  .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>
