<template>
  <ht-post-selector
    v-model="value"
    :validate="validate"
    :name="name"
    :placeholder="placeholder"
    :permission="permission"
    :single="single"
    :config="config"
    :data="data"
    :table-columns="tableColumns"
    :pagination="pagination"
    :demensions="demensions"
    :defaultDemension="defaultDemension"
    :orgs="orgs"
    :load-org-tree="loadOrgTree"
    @load="handleLoad"
    @valueChange="valueChange"
    @loadDemensions="loadDemensions"
    @changeDemension="changeDemension"
    @loadPosts="loadPosts"
    select-label="name"
    quick-search-props="name,code"
  />
</template>
<script>
import selector from "@/api/selector.js";
import req from "@/api/sysType.js";

export default {
  name: "my-post-selector",
  props: {
    validate: [String, Object],
    value: String,
    name: String,
    placeholder: String,
    permission: String,
    single: Boolean,
    config: Object
  },
  data() {
    return {
      data: [],
      demensions: [],
      defaultDemension:null,
      orgs: [],
      tableColumns: [
        { prop: "name", label: "名称", width: "120" },
        { prop: "code", label: "编码", width: "120" },
        { prop: "orgName", label: "所属组织", width: "260" }
      ],
      pagination: {
        page: 1,
        pageSize: 50,
        total: 0
      },
      orgLength: 0
    };
  },
  methods: {
    loadDemensions() {
      setTimeout(() => {
        this.demensions = [
          { id: 1, demName: "行政维度" },
          { id: 2, demName: "分公司维度" }
        ];
        this.defaultDemension = 1;
      }, 1000);
    },
    handleLoad(param, cb) {
      this.$message("输入关键字查询岗位");
      selector
        .getPosts(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();
        });
    },
    valueChange(value) {
      this.$emit("input", value);
    },
    loadOrgTree(node, resolve) {
      // 组织节点点击事件 异步加载组织
      this.$message("组织节点点击事件 请异步加载组织");
      let _this = this;

      req.getSysTypeData({}).then(function(data) {
        // _this.orgs = data;
        var resultData = [{ id: node.id, name: "组织" + node.id }];
        if (_this.orgLength > 2) {
          resultData = [];
        }
        _this.orgLength++;
        resolve(resultData);
      });
    },
    changeDemension(currentDemension) {
      // 维度改变 重新获取组织
      this.$message("维度改变 请重新获取组织");
    },
    loadPosts(org) {
      // 根据组织获取用户
      this.$message("加载岗位");
    }
  }
};
</script>
