<template>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      title="选择报表按钮"
    >
    <div>
      <!--<Form onsubmit="return false">-->
        <!--<Form-item>-->
          <!--<Input-->
            <!--v-model="key"-->
            <!--style="width: 460px;"-->
            <!--type="text"-->
            <!--placeholder="请输入名称,回车查询"-->
            <!--icon="ios-search-strong"-->
            <!--@on-enter="_search"-->
          <!--/>-->
        <!--</Form-item>-->
      <!--</Form>-->
    </div>
    <div style="text-align: center;overflow-y: auto;height: 450px">
      <ul :id="treeId" class="ztree" v-show="isTree" v-loading="isLoading"></ul>
    </div>
    <div slot="footer" class="btn-width">
      <el-button style="margin-left: 8px" @click="_cancel">
        取消
      </el-button>
      <el-button type="primary" @click="_ok">
        确定
      </el-button>
    </div>
    </el-dialog>
</template>
<script>
/**
 * 选择报表按钮（是否报表为是的按钮）tree
 */
import { sysResource } from '../../../../api'
export default {
  data() {
    return {
      showModal: false,
      selNotes: [],
      isLoading: true,
      isTree: false,
      treeId: '',
      key: '',
      treeObj: {},
      setting: {
        data: {
          simpleData: {
            enable: true,
            idKey: 'id',
            pIdKey: 'pid'
          }
        },
        check: {
          enable: true,
          chkStyle: 'radio',
          radioType: 'all'
        },
        view: {
          showIcon: false
        },
        callback: {
          onClick: this.zTreeOnClick
        }
      }
    }
  },
  methods: {
    _open() {
      this.showModal = true
      this.isLoading = true
      this.isTree = false
      this.treeId = 'selBtnReport' + this.$randomCode()
      this.key = ''
      this._search()
      this.selNotes = []
    },
    _search: async function() {
      // buttonReport 为true 显示【是否报表】为是的按钮
      const data = {
        buttonReport: true
      }
      if (this.key) {
        data.name = this.key
      }
      const result = await sysResource.menuTreeList(data)
      this.treeObj = $.fn.zTree.init($('#' + this.treeId), this.setting, result)
      /** 过滤掉非表格列前面的radio */
      const node = this.treeObj.getNodes() // 获取根节点
      const allNodes = this.treeObj.transformToArray(node) // 遍历所有节点
      if (allNodes.length > 0) {
        for (let i = 0; i < allNodes.length; i++) {
          if (allNodes[i].uri !== 'reportBtn') {
            // 判断需要显示单选框的条件(uri不为reportBtn)
            allNodes[i].nocheck = true // nocheck为true表示没有选择框
            this.treeObj.updateNode(allNodes[i]) // 刷新
          }
        }
      }
      /** 过滤掉非表格列前面的checkbox */
      setTimeout(() => {
        this.isLoading = false
        this.isTree = true
        // treeObj.expandAll(true);
      }, 300)
    },
    zTreeOnClick(event, treeId, treeNode) {
      this.treeObj.checkNode(treeNode, !treeNode.checked, true)
    },
    _cancel() {
      this.showModal = false
    },
    _ok() {
      this.selNotes = this.treeObj.getCheckedNodes(true)
      if (this.selNotes.length === 0) {
        this.$message.warning('请选中所属按钮！')
      } else {
        const parentAllNode = this.selNotes[0].getPath()
        let lastNode = {}
        for (let i = 0; i < parentAllNode.length; i++) {
          // 顶级父节点
          if (parentAllNode[i].pid === null) {
            lastNode = parentAllNode[i]
          }
        }
        this.$emit('on-result-change', this.selNotes[0], lastNode)
        this.showModal = false
      }
    }
  }
}
</script>
