
<template>
  <div>
    <div>
      <Form onsubmit="return false">
        <Form-item>
          <el-input v-model="key" placeholder="请输入关键字,回车查询" style="width:100%" clearable @keyup.enter.native="_search" />
        </Form-item>
      </Form>
    </div>
    <!--加载中-->
    <ul
      :id="treeId"
      v-loading="isLoading"
      class="ztree"
      style="overflow-x: auto;"
      :style="{height:ztreeHeight}"
      element-loading-text="拼命加载中..."
      element-loading-background="rgba(234, 237, 241, 1)"
    />
  </div>
</template>

<style>
</style>

<script>
/**
 * 添加编辑Ztree
 */
import { sysOrg } from '../../../api'

export default {
  data() {
    return {
      key: '',
      isLoading: true,
      treeId: '',
      treeObj: {},
      setting: {
        data: {
          simpleData: {
            enable: true,
            idKey: 'id',
            pIdKey: 'pid'
          }
        },
        callback: {
          onClick: this.zTreeOnClick
        }
      }
    }
  },
  computed: {
    ztreeHeight: function() {
      return this.$tableHeight('ztreeHeight')
    }
  },
  methods: {
    _open() {
      this.treeId = 'org' + this.$randomCode()
      this.isLoading = true
      this._search()
    },
    _search: async function() {
      const obj = {}
      if (this.key) {
        obj.keyword = this.key
      }
      const result = await sysOrg.orgTreeList(obj)
      this.treeObj = $.fn.zTree.init($('#' + this.treeId), this.setting, result)
      setTimeout(() => {
        this.isLoading = false
        this.treeObj.expandAll(true)
      }, 300)
    },
    zTreeOnClick(event, treeId, treeNode) {
      this.$emit('on-result-change', treeNode.id)
    }
  }
}
</script>
