<template>
  <div class="sy-select-project">
    <el-popover placement="bottom" trigger="click" v-model="visible" :disabled="disabled">
      <div style="height: 60vh;">
        <sy-search-tree
          ref="ztreeRef"
          :setting="ztreeSetting"
          :nodes="nodes"
          :placeholder="placeholder"
          @onClick="onClick"
          @onCreated="handleCreated"
          @searchTree="searchTree"
          :beforeClickFn="beforeClickFn"
        />
      </div>
      <a slot="reference" style="cursor: pointer;"
        >{{ projectInfo.nodeName }}<i class="el-icon-caret-bottom el-icon--right drop-icon" :class="{ isOpen: visible }"></i
      ></a>
    </el-popover>
  </div>
</template>

<script>
import merge from 'deepmerge'
import searchTree from '../../search-tree'

export default {
  name: 'SySelectProject',
  components: {
    SySearchTree: searchTree
  },
  data() {
    const defaultSetting = {
      check: {
        enable: false
      },
      data: {
        // 设置图标库(采用iconfont class形式)
        iconMap: {
          1: 'icon-jituan',
          2: 'icon-quyufengongsi',
          3: 'icon-fengongsi',
          4: 'icon-guanlichu',
          5: 'icon-xiangmujiedian',
          6: 'icon-quyujiedian',
          7: 'icon-loudong',
          8: 'icon-danyuan',
          9: 'icon-fangjian'
        },
        // 设置对应每个节点的节点类型，与数据中customType对应(25行)
        key: {
          idKey: 'nodeId',
          nodeType: 'customType',
          name: 'nodeName',
          title: 'nodeName',
          children: 'childList'
        }
      },
      view: {
        // 开启图标显示功能
        showIcon: true
      }
    }
    const mergeSetting = merge(defaultSetting, this.setting)
    return {
      visible: false,
      projectInfo: {
        nodeName: '请选择'
      },
      ztreeObj: null,
      ztreeSetting: mergeSetting
    }
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    setting: {
      type: Object,
      required: false,
      default: () => ({})
    },
    nodes: {
      type: Array,
      default: () => []
    },
    placeholder: {
      type: String,
      default: '请输入搜索关键字'
    },
    beforeClick: { type: Function } // 选中节点之前的处理事件，如不允许选择nodeType='2'的节点
  },
  watch: {
    defaultCheckedKeys: {
      handler: function(newVal) {
        if (newVal.length && this.ztreeObj) {
          this.$refs.ztreeRef.setDefaultCheckedNodes(newVal)
        }
      },
      deep: true
    }
  },
  methods: {
    beforeClickFn(arg) {
      return this.beforeClick(arg)
    },
    onClick: function(evt, treeId, treeNode) {
      this.projectInfo = { ...treeNode }
      this.visible = false
      this.$emit('click', this.projectInfo)
    },
    onCheck: function(evt, treeId, treeNode) {
      // 选中事件
      console.log(evt.type, treeNode)
    },
    handleCreated: function(ztreeObj) {
      this.ztreeObj = ztreeObj
      // onCreated 中操作ztreeObj对象展开第一个节点
      ztreeObj.expandNode(ztreeObj.getNodes()[0], true)
    },
    searchTree(keyword) {
      // 关键字搜索
      this.$emit('search', keyword)
    },
    setSelectedNode(selectedKey) {
      // 设置默认选中的节点
      setTimeout(() => {
        this.$refs.ztreeRef.setSelectedNode(selectedKey)
      }, 20)
    },
    setCheckedNodes(checkedKeys) {
      setTimeout(() => {
        this.$refs.ztreeRef.setCheckedNodes(checkedKeys)
      }, 20)
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../styles/variables.scss';
.sy-select-project {
  height: 100%;
  color: $header-color;
  font-weight: 700;

  .drop-icon {
    transition: all 0.3s linear;
    &.isOpen {
      transform: rotateZ(180deg);
    }
  }
}
</style>
