<template>
  <div ref="sideTree" class="side_tree">
    <div class="side_tree_box">
      <p class="side_tree_title">{{ sideTreeTitle }}</p>
      <slot name="customTreeTitle"></slot>
    </div>
    <slot name="customTreeSlot"></slot>
    <div class="tree_box" v-if="!hideTree">
      <tree-view
        ref="treeView"
        v-bind="$attrs"
        v-on="$listeners"
        :indent="10"
        node-key="id"
        default-expand-all
        check-on-click-node
        :expand-on-click-node="false"
        highlight-current
      />
    </div>
    <div
      class="line"
      unselectable="on"
      onselectstart="return false"
      ref="line"
    ></div>
  </div>
</template>
<script>
import treeView from './tree'

export default {
  name: 'side-tree-view',
  components: { treeView },
  props: {
    sideTreeTitle: String,
    hideTree: Boolean
  },
  mounted() {
    this.onLineSlideEvent()
  },
  methods: {
    onLineSlideEvent() {
      let slide = this.$refs.line
      let move = e => {
        let sideTree = this.$refs.sideTree
        let menuWidth = sideTree.getBoundingClientRect().left
        sideTree.style.width = e.clientX - menuWidth + 'px'
        // 获取子组件的元素 并计算宽度
        this.$nextTick(() => {
          let treeSpanList = document.querySelectorAll('.PEN_SIDE_TREE_SPAN')
          let hasIcon = document.querySelectorAll('.PEN_SIDE_TREE_ICON') || []
          treeSpanList.forEach(item => {
            if (hasIcon.length) {
              item.style.width = e.clientX - menuWidth - 77 - 23 + 'px'
            } else {
              item.style.width = e.clientX - menuWidth - 77 + 'px'
            }
          })
        })
      }
      let down = () => {
        document.addEventListener('mousemove', move)
      }

      slide.addEventListener('mousedown', down)

      slide.addEventListener('mouseup', () => {
        document.removeEventListener('mousemove', move)
      })

      document.addEventListener('mouseup', () => {
        document.removeEventListener('mousemove', move)
      })
    }
  }
}
</script>
<style lang="less" scoped>
.side_tree {
  min-width: 225px;
  width: 225px;
  background: #fafafa;
  box-shadow: 7px 0px 7px -7px rgba(0, 0, 0, 0.12);
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 100%;
  .side_tree_box {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #e9e9e9;
    justify-content: space-between;
    padding: 0 10px;
  }
  .side_tree_title {
    height: 46px;
    line-height: 46px;
    font-size: 14px;
    font-weight: bold;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .tree_box {
    flex: 1;
    padding: 0 10px;
    overflow: hidden;
    overflow-y: auto;
  }

  .tree_box::-webkit-scrollbar {
    display: none;
  }

  .line {
    width: 5px;
    min-width: 5px;
    cursor: ew-resize;
    background: transparent;
    position: absolute;
    z-index: 99;
    top: 0;
    right: 0;
    bottom: 0;
  }
}

.side_tree::-webkit-scrollbar {
  display: none;
}
</style>
