<template>
  <el-dialog
    v-loading = "loading"
    v-if="visible"
    :visible.sync="visible"
    :before-close="handleClose"
    :title="title"
    :append-to-body="appendToBody"
    width="60%">
    <el-input v-model="filterText" placeholder="输入关键字进行过滤"/>
    <el-scrollbar class="information" style="height: 500px; margin-top: 10px">
      <el-tree
        ref="tree"
        :data="tree"
        :props="defaultProp"
        :filter-node-method="filterNode"
        :expand-on-click-node="false"
        check-strictly
        default-expand-all
        check-on-click-node
        show-checkbox
        @node-click="handleNodeClick"/>
    </el-scrollbar>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeWindow">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination },
  directives: { waves },
  props: {
    title: {
      type: String,
      default: ''
    },
    appendToBody: {
      type: Boolean,
      default: false
    },
    limit: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      visible: false,
      defaultProp: {
        value: 'id',
        children: 'nodes',
        label: 'name'
      },
      tree: [],
      filterText: '',
      heirloom: {},
      treeClickCount: 0,
      loading: false
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    init(e) {
      this.heirloom = e
      this.visible = true
      this.getTree()
    },
    handleClose() {
      this.visible = false
    },
    getTree() {
      this.loading = true
      requestFN(
        '/department/listTree'
      ).then((data) => {
        this.loading = false
        this.tree = JSON.parse(data.zTreeNodes)
        if (this.tree[0]) {
          this.tree[0].disabled = false
        }
      }).catch((e) => {
        this.loading = false
        this.$message.error(e)
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    closeWindow() {
      this.visible = false
    },
    clear() {
      this.tree = []
    },
    save() {
      const list = this.$refs.tree.getCheckedNodes()
      if (list.length > this.limit) {
        this.$message.error('应该选择' + this.limit + '个,但是现在选择了' + list.length + '个')
        return
      }
      this.$emit('getResult', { info: this.$refs.tree.getCheckedNodes(), e: this.$refs.tree.getCheckedNodes(), root: this.root })
      this.visible = false
    },
    // 节点双击出发
    handleNodeClick(data, node) {
      this.treeClickCount++
      if (this.treeClickCount >= 2) {
        return
      }
      this.timer = window.setTimeout(() => {
        if (this.treeClickCount === 1) {
          this.treeClickCount = 0
        } else if (this.treeClickCount > 1) {
          this.treeClickCount = 0
          this.$emit('getResult', { info: [data], e: [data], heirloom: this.heirloom })
          this.visible = false
        }
      }, 300)
    }
  }
}

</script>

<style scoped>
.information >>> .el-scrollbar__wrap {
  overflow-x: hidden;
}
</style>
