<template>
  <ht-org-post-dialog
    :single="single"
    :data="data"
    :table-columns="tableColumns"
    :pagination="pagination"
    :demensions="demensions"
    :default-demension="defaultDemension"
    :orgs="orgs"
    :load-org-tree="loadOrgTree"
    :default-expanded-keys="defaultExpandedKeys"
    @load="handleLoad"
    @changeDemension="changeDemension"
    @loadDemensions="loadDemensions"
    @loadListData="loadListData"
    @onConfirm="onConfirm"
    quick-search-props="name,code"
    ref="htOrgPostDialog"
  />
</template>
<script>
import selector from "@/api/selector.js";
import req from "@/api/sysType.js";

export default {
  name: "my-org-dialog",
  props: {
    value: Array,
    single: Boolean
  },
  data() {
    return {
      data: [],
      demensions: [],
      defaultDemension: null,
      orgs: [],
      tableColumns: [
        { prop: "name", label: "名称"},
        { prop: "code", label: "编码"},
        { prop: "pathName", label: "路径"}
      ],
      pagination: {
        page: 1,
        pageSize: 50,
        total: 0
      },
      orgLength: 0,
      defaultExpandedKeys: []
    };
  },
  mounted() {
  },
  methods: {
    loadDemensions() {
      setTimeout(() => {
        this.demensions = [
          { id: 1, demName: "行政维度" },
          { id: 2, demName: "分公司维度" }
        ];
        this.defaultDemension = 1;
      }, 1000);
    },
    showDialog() {
      this.$refs.htOrgPostDialog.showDialog();
    },
    handleLoad(param, cb) {
      this.$message("输入关键字查询组织");
      selector
        .getOrgs(param)
        .then(response => {
          this.data = response.rows;
          this.pagination.page = response.page;
          this.pagination.pageSize = response.pageSize;
          this.pagination.total = response.total;
          cb();
        })
        .catch(err => {
          cb();
        });
    },
    loadOrgTree(node, resolve) {
      // 组织节点点击事件 异步加载组织
      this.$message("组织节点点击事件 请异步加载组织");
      let _this = this;

      req.getSysTypeData({}).then(function(data) {
        // _this.orgs = data;
        _this.defaultExpandedKeys.push(node.id);
        var resultData = [{ id: node.id, name: "组织" + node.id ,isLeaf:false}];
        if (_this.orgLength > 2) {
          resultData = [];
        }
        _this.orgLength++;
        resolve(resultData);
      });
    },
    changeDemension(currentDemension) {
      // 维度改变 重新获取组织
      this.$message("维度改变 请重新获取组织");
    },
    loadListData(nodedata) {
      // 根据组织获取下级组织
      this.$message("根据组织获取下级组织");
    },
    onConfirm(selection) {
      this.$emit("onConfirm", selection);
      this.$emit("input", selection);
    }
  }
};
</script>
