<template>
  <div>
    <div class="head-container">
      <el-input
        v-model="deptName"
        :placeholder="searchPlaceHolder"
        clearable
        size="small"
        prefix-icon="el-icon-search"
        style="margin-bottom: 20px"
      />
    </div>
    <div class="tree-container">
      <div style="margin-bottom: 10px" v-show="showLowerCheck">
        <el-checkbox v-model="checkSub" @change="changeCheckBox"
          >关联下级</el-checkbox
        >
      </div>
      <el-tree
        :data="treeList"
        :props="defaultProps"
        :expand-on-click-node="false"
        :filter-node-method="filterNode"
        ref="tree"
        node-key="id"
        class="el-tree-ex"
        :default-expanded-keys="defaultKeys"
        :default-checked-keys="defaultKeys"
        @node-click="handleNodeClick"
        v-bind="$attrs"
      >
    </el-tree>
    </div>
  </div>
</template>

<script>
import tableListMixins from "@/mixins/tableList";
import { deptTreeSelect, handsheDeptTreeSelect } from "@/api/system/public";

export default {
  name: "orgTree",
  mixins: [tableListMixins],
  data() {
    return {
      //过滤信息
      deptName: "",
      // 机构树列表
      treeList: [],
      //默认选中节点
      defaultKeys: [],
      checkSub: true,
      defaultKey: null,
    };
  },
  props: {
    hangsheTree: {
      type: Boolean,
      default: false,
    },
    defaultProps: {
      type: Object,
      default: () => {
        return {
          children: "children",
          label: "shortName",
        };
      },
    },
    customRequest: {
      type: Function,
    },
    showLowerCheck: {
      type: Boolean,
      default: true,
    },
    searchPlaceHolder: {
      type: String,
      default: "请输入机构名称",
    },
  },
  watch: {
    // 根据名称筛选机构树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
    defaultProps(val) {
      this.defaultProps = { ...val };
    },
  },
  created() {
    this.getDeptTree();
  },
  methods: {
    /** 下穿状态改变*/
    changeCheckBox(state) {
      this.$emit("checkChange", state);
    },
    /** 查询机构下拉树结构 */
    getDeptTree() {
      let method = deptTreeSelect;
      if (this.hangsheTree) {
        method = handsheDeptTreeSelect;
      }
      if (this.customRequest) {
        method = this.customRequest;
      }
      method().then((response) => {
        this.treeList = response.data;
        this.defaultKeys.push(response.data[0].id);
        this.$emit("defaultKey", response.data[0].id);
        this.$emit("defaultOrg", response.data[0]);
        this.defaultKey = response.data[0].id;
        setTimeout(() => {
          this.$refs.tree.setCurrentKey(response.data[0].id);
        }, 100);
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data, node) {
      this.$emit("click", data, node);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-tree-ex {
  overflow: auto;
  ::v-deep .is-current > .el-tree-node__content {
    background-color: #d1e0f1 !important;
  }
}
</style>
