<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"
      class="ztree"
      style="overflow-x: auto;"
      :style="{height:ztreeHeight}"
      v-loading="isLoading"
      element-loading-text="拼命加载中..."
      element-loading-background="rgba(234, 237, 241, 1)"
    />
  </div>
</template>

<script>
/**
 * 左侧菜单tree
 */
import { consumeCategory } 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 = 'resource' + this.$randomCode()
      this._page()
    },
    _page: async function() {
      const data = {
        type: 0
      }
      if (this.key) {
        data.name = this.key
      }
      const result = await consumeCategory.getTree(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>
