<template>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      title="分配表格列权限"
    >
    <div style="text-align: center;overflow-y: auto;height: 450px" v-loading="isLoading">
      <ul :id="treeId" class="ztree"/>
    </div>
    <div slot="footer">
      <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
    </div>
    </el-dialog>
</template>
<script>
/**
 * 菜单+表格+表格列Ztree
 */
import { sysTableColumn } from '../../../../api'

export default {
  data() {
    return {
      showModal: false,
      isLoading: true,
      treeId: '',
      idObj: {
        // 要提交的id数据
        id: '',
        tableColumnIds: []
      },
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '确定', type: 'primary' }
      ],
      setting: {
        data: {
          simpleData: {
            enable: true,
            idKey: 'id',
            pIdKey: 'pid'
          }
        },
        check: {
          enable: true
        },
        view: {
          showIcon: false
        },
        callback: {
          onClick: this.zTreeOnClick
        }
      },
      treeObj: {}
    }
  },
  methods: {
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '确定':
          this._ok()
          break
      }
    },
    _open(companyId) {
      // 回显选中的数据
      this.showModal = true
      this.treeId = 'col' + this.$randomCode()
      this._getTree(companyId)
      this.idObj.id = companyId
    },
    _getTree: async function(companyId) {
      this.isLoading = false
      const result = await sysTableColumn.getListTree(companyId)
      this.treeObj = $.fn.zTree.init($('#' + this.treeId), this.setting, result)
      /** 过滤掉非表格列前面的checkbox */
      // 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) {
      //       // 判断需要显示复选框的条件(有uri的)
      //       allNodes[i].nocheck = true // nocheck为true表示没有选择框
      //       this.treeObj.updateNode(allNodes[i]) // 刷新
      //     }
      //   }
      // }
      /** 过滤掉非表格列前面的checkbox */
      this._echoTree(companyId)
    },
    zTreeOnClick(event, treeId, treeNode) {
      this.treeObj.checkNode(treeNode, !treeNode.checked, true)
    },
    // 回显选中的节点树
    _echoTree: async function(companyId) {
      const result = await sysTableColumn.getByCompanyId(companyId)
      if (result.length > 0) {
        for (let i = 0; i < result.length; i++) {
          const node = this.treeObj.getNodeByParam('id', result[i], null)
          if (node) {
            this.treeObj.checkNode(node, true)
          }
        }
      }
      this.isLoading = false
    },
    _resultChange(result, msg) {
      if (result) {
        this.showModal = false
        this.$message.success(msg)
      }
      this.$refs.footerModal._hideLoading()
    },
    _cancel() {
      this.$refs.footerModal._hideLoading()
      this.showModal = false
    },
    _ok: async function() {
      const nodesList = []
      const nodes = this.treeObj.getCheckedNodes(true)
      for (let i = 0; i < nodes.length; i++) {
        if (!nodes[i].uri) {
          // 说明是表格列
          nodesList.push(nodes[i].id)
        }
      }
      this.idObj.tableColumnIds = nodesList
      const result = await sysTableColumn.companySave(this.idObj)
      this._resultChange(result, '表格分配成功!')
    }
  }
}
</script>
