<template>
  <div>
    <div>
      <Form onsubmit="return false">
        <Form-item>
          <el-input
            v-model="key"
            placeholder="请输入关键字,回车查询"
            style="width:100%"
            @keyup.enter.native="_page"
          />
        </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>

<script>
/**
 * 左侧组织tree
 */
import { sysOrg } from '../../../../api'
export default {
  data() {
    return {
      key: '',
      isLoading: true,
      treeId: '',
      treeObj: {},
      setting: {
        data: {
          simpleData: {
            enable: true,
            idKey: 'id',
            pIdKey: 'pid'
          }
        },
        view: {
          showIcon: false
        },
        callback: {
          onClick: this._zTreeOnClick
        }
      }
    }
  },
  computed: {
    ztreeHeight: function() {
      return this.$tableHeight('ztreeHeight')
    }
  },
  methods: {
    _open() {
      this.isLoading = true
      this.treeId = 'org' + this.$randomCode()
      this._page()
    },
    _page: async function() {
      const data = {}
      if (this.key) {
        data.keyword = this.key
      }
      const result = await sysOrg.orgTreeList(data)
      if (result) {
        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>
