<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 { sysResource } 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 = 'report' + this.$randomCode()
      this._page()
    },
    _page: async function() {
      // buttonReport 为true 显示【是否报表】为是的按钮
      const data = {
        buttonReport: true
      }
      if (this.key) {
        data.name = this.key
      }
      const result = await sysResource.menuTreeList(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) {
      const parentAllNode = treeNode.getPath()
      let lastNode = {}
      for (let i = 0; i < parentAllNode.length; i++) {
        // 顶级父节点
        if (parentAllNode[i].pid === null) {
          lastNode = parentAllNode[i]
        }
      }

      // 获取该节点下的所有字节点（是否报表为是）
      const childrenNodesAll = treeNode.children ? treeNode.children : []
      const childrenNodesCode = []
      for (let k = 0; k < childrenNodesAll.length; k++) {
        if (childrenNodesAll[k].uri === 'reportBtn') {
          childrenNodesCode.push(childrenNodesAll[k].code)
        }
      }

      this.$emit('on-result-change', treeNode, lastNode, childrenNodesCode)
    }
  }
}
</script>
