<template>
  <div>
    <div>
      <Form onsubmit="return false">
        <Form-item>
          <Input
            v-model="key"
            placeholder="请输入关键字,回车查询"
            style="width:100%"
            icon="ios-search-strong"
            @on-enter="_page"
          />
        </Form-item>
      </Form>
    </div>
    <!--加载中-->
    <div v-show="isLoading" class="demo-spin-container spin-bg">
      <Spin fix size="large" />
    </div>
    <ul :id="treeId" class="ztree" style="overflow-x: auto;" :style="{height:ztreeHeight}" />
  </div>
</template>

<script>
/**
 * 左侧组织tree
 */
import { sampleType } from '../../../api'
export default {
  data() {
    return {
      key: '',
      isLoading: true,
      treeId: '',
      treeObj: {},
      type: '',
      setting: {
        data: {
          simpleData: {
            enable: true,
            idKey: 'id',
            pIdKey: 'pid'
          }
        },
        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.name = this.key
      }
      const result = await sampleType.pointClassTree(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)
    }
  }
}
</script>
