<template>
  <el-tree
    :currentNodeKey="currentNodeKey"
    class="c-tree"
    ref="tree"
    v-bind="$attrs"
    v-on="$listeners"
  >
    <span class="custom_tree_node" slot-scope="{ node, data }">
      <svg ref="icon" v-if="data.icon" class="iconpark-icon l-s">
        <use :href="'#' + data.icon"></use>
      </svg>
      <el-tooltip
        effect="dark"
        :content="node.label"
        placement="top"
        :disabled="!isShowTooltip"
      >
        <span
          @mouseenter="showTooltipEvent"
          ref="treeSpan"
          class="PEN_SIDE_TREE_SPAN"
          :class="{
            tree_title: !!data.children,
            is_all: data.id === 'root',
            PEN_SIDE_TREE_ICON: !!data.icon
          }"
          >{{ node.label }}</span
        >
      </el-tooltip>
      <div class="tree_tools" v-if="treeToolsList && treeToolsList.length">
        <el-popover
          placement="bottom"
          trigger="hover"
          popper-class="classify_popper"
          v-if="currentNodeKey == data.id"
        >
          <!--           v-if="
                item.isVisible === undefined
                  ? true
                  : typeof item.isVisible === 'function'
                  ? item.isVisible(data, item)
                  : item.isVisible
              " -->
          <ul class="classify_tools_box show_hand">
            <li
              v-if="judgePrem(data, item)"
              @click="treeToolsEvent(item, data)"
              v-for="(item, index) in treeToolsList"
              :key="index"
            >
              {{ item.text }}
            </li>
          </ul>
          <svg slot="reference" class="iconpark-icon">
            <use href="#gengduo-5ebfae00"></use>
          </svg>
        </el-popover>
      </div>
    </span>
  </el-tree>
</template>
<script>
export default {
  name: 'tree-view',
  props: {
    treeToolsList: {
      type: Array,
      default: () => []
    },
    currentNodeKey: {
      type: [String, Number]
    }
  },
  data() {
    return {
      isShowTooltip: false
    }
  },
  computed: {
    permissionsData() {
      return this.$store.state.permissions
    }
  },
  methods: {
    judgePrem(data, item) {
      if (item.hasOwnProperty('premissionKey')) {
        return this.permissionsData[item.premissionKey]
      } else {
        if (item.isVisible && typeof item.isVisible === 'function') {
          return item.isVisible(data, item)
        } else {
          return item.isVisible === undefined ? true : item.isVisible
        }
      }
    },
    treeToolsEvent(item, data) {
      this.$emit('tree-tools-event', item, data)
    },
    // 判读文本是否溢出 溢出则显示tooltip
    showTooltipEvent(event) {
      const ev = event.target
      const ev_weight = ev.scrollWidth // 文本的实际宽度
      const content_weight = ev.clientWidth // 文本的可视宽度
      this.isShowTooltip = ev_weight > content_weight
    }
  }
}
</script>
<style lang="less" scoped>
.el-tree {
  background: #fafafa;
  min-width: 140px;
}

.is_all {
  font-weight: bold;
}

.tree_tools {
  position: absolute;
  width: 18px;
  height: 18px;
  right: -6px;
  top: 7px;
  /deep/ .el-popover__reference-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

/deep/ .el-tree-node__content {
  height: 32px;
  line-height: 32px;
  position: relative;
  &:hover {
    background: #f1f1f1;
  }
}

/deep/ .is-current {
  & > .el-tree-node__content {
    .custom_tree_node {
      .PEN_SIDE_TREE_SPAN {
        color: #fc6510;
        font-weight: bold;

        &.tree_title {
          font-weight: bold;
        }
      }
    }
  }
}

.custom_tree_node {
  // flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  overflow: hidden;
  // position:relative;
  .PEN_SIDE_TREE_SPAN {
    // flex: 1;
    // width: 148px;
    // min-width: 148px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
    // width: 129px;
    &.tree_title {
      color: rgba(0, 0, 0, 0.85);
    }
  }
  .PEN_SIDE_TREE_ICON {
    width: 125px;
    min-width: 125px;
  }
  .l-s {
    margin-right: 5px;
  }
}

.c-tree {
  /deep/ .is-current {
    & > .el-tree-node__content {
      .l-s {
        color: #fc6510;
      }
    }
  }
}

.classify_tools_box {
  display: flex;
  flex-direction: column;

  li {
    padding: 6px 16px;
    font-size: 13px;
    line-height: 1.4;
    text-align: left;
  }

  li:hover {
    background: rgba(0, 0, 0, 0.05);
  }
}

svg {
  width: 18px;
  height: 18px;
  color: rgba(0, 0, 0, 0.85);
}

.classify_popper {
  min-width: 88px !important;
  width: 88px !important;
  vertical-align: middle;
}
</style>
<style>
.classify_popper {
  min-width: 10px;
  padding: 6px 0;
  text-align: center;
}

.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  background-color: #fafafa;
}
</style>
