<template>
  <!--组织弹出树-->
  <el-dialog
    v-dialogDrag
    :visible.sync="showModal"
    title="选择上级组织"
  >
    <div>
      <Form onsubmit="return false">
        <Form-item>
          <el-input
            v-model="key"
            style="width: 460px;"
            type="text"
            placeholder="请输入组织名称,回车查询"
            @keyup.enter.native="_search"
          />
        </Form-item>
      </Form>
    </div>
    <div style="text-align: center;overflow-y: auto;height: 450px">
      <!--加载中-->
      <ul
        :id="treeId"
        v-loading="isLoading"
        class="ztree"
      />
    </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>
import { sysOrg } from '../../../api'
export default {
  data() {
    return {
      showModal: false,
      key: '',
      isLoading: true,
      treeId: '',
      selNotes: [],
      treeObj: {},
      setting: {
        data: {
          simpleData: {
            enable: true,
            idKey: 'id',
            pIdKey: 'pid'
          }
        },
        check: {
          enable: true,
          chkStyle: 'radio',
          radioType: 'all'
        },
        callback: {
          onClick: this.zTreeOnClick //  单击节点
        }
      }
    }
  },
  methods: {
    _open() {
      this.showModal = true
      this.key = ''
      this.treeId = 'org' + this.$randomCode()
      this._search()
    },
    _search: async function() {
      const obj = {}
      if (this.key) {
        obj.keyword = this.key
      }
      const result = await sysOrg.orgTreeList(obj)
      this.treeObj = $.fn.zTree.init($('#' + this.treeId), this.setting, result)
      setTimeout(() => {
        this.isLoading = false
        // 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)
      this.$emit('on-result-change', this.selNotes[0]) // 向父及传递数据
      this.showModal = false
    }
  }
}
</script>
