
<template>
  <div>
    <div>
      <Form onsubmit="return false">
        <Form-item>
          <el-input
            v-model="key"
            placeholder="请输入关键字,回车查询"
            style="width:100%"
            @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 { sysRole } 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.treeId = 'role' + this.$randomCode()
      this.isLoading = true
      this._search()
    },
    _search: async function() {
      const data = {}
      if (this.key) {
        data.name = this.key
      }
      const result = await sysRole.roleTreeList(data)
      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>
