<template>
  <div>
    <el-popover
      popper-class="selectTree"
      v-clickoutside="replyOutside"
      placement="bottom-start"
      :width="popoverWidth"
      trigger="manual"
      v-model="isShowSelect"
      @hide="popoverHide"
    >
      <div class="pdlr_1">
        <el-input
          class="mb_5"
          v-if="filterable"
          v-model="filterText"
          placeholder="搜索"
        ></el-input>
      </div>

      <el-tree
        class="common-tree"
        :indent="multiple ? 10 : 10"
        :width="width"
        ref="tree"
        :data="treeData"
        :props="obj"
        :filter-node-method="filterNode"
        :show-checkbox="multiple"
        :node-key="obj.id"
        :check-strictly="checkStrictly"
        :default-expanded-keys="defaultKey"
        :default-expand-all="defaulexpand"
        :expand-on-click-node="multiple && expandClickNode"
        :check-on-click-node="checkClickNode"
        :highlight-current="true"
        @check-change="nodeClick"
        @node-click="nodeClick"
      ></el-tree>

      <el-select
        slot="reference"
        ref="select"
        :size="size"
        v-model="returnDataKeys"
        :multiple="multiple"
        :clearable="clearable"
        :collapse-tags="collapseTags"
        @click.native="selectClick"
        @remove-tag="removeTag"
        @clear="clean"
        class="tree-select"
        :placeholder="placeholder"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <div class="mt_5" v-if="selectAll">
        <el-checkbox
          v-model="checkedAll"
          :indeterminate="returnDataKeys.length == 0 ? false : !checkedAll"
          @change="selectedAll"
          >全选</el-checkbox
        >
      </div>
    </el-popover>
  </div>
</template>

<script>
//点击外部关闭下拉框
import Clickoutside from "element-ui/src/utils/clickoutside";
import { StaffDepartmentListApi } from "@/api/staffApi.js";
export default {
  directives: { Clickoutside },
  props: {
    //全选
    selectAll: {
      type: Boolean,
      default() {
        return false;
      },
    },
    // 树结构数据
    data: {
      type: Array,
      default() {
        return [];
      },
    },
    obj: {
      type: Object,
      required: false,
      default: () => {
        return {
          id: "", // id
          label: "", // 显示名称
          children: "", //子级字段名
        };
      },
    },
    //配置是否可以搜索
    filterable: {
      type: Boolean,
      default() {
        return false;
      },
    },
    //输入框占位值
    placeholder: {
      type: String,
      default() {
        return "请选择";
      },
    },
    //配置是否可多选
    multiple: {
      type: Boolean,
      default() {
        return false;
      },
    },
    renderAfterExpand: {
      type: Boolean,
      default() {
        return true;
      },
    },
    // 配置是否可清空选择
    clearable: {
      type: Boolean,
      default() {
        return false;
      },
    },
    // 配置多选时是否将选中值按文字的形式展示
    collapseTags: {
      type: Boolean,
      default() {
        return false;
      },
    },
    // 显示复选框情况下，是否严格遵循父子不互相关联
    checkStrictly: {
      type: Boolean,
      default() {
        return false;
      },
    },
    radioStrictly: {
      type: Boolean,
      default() {
        return false;
      },
    },
    //多选是设置点击节点是否可以选中
    checkClickNode: {
      type: Boolean,
      default() {
        return true;
      },
    },
    //多选时：点击节点展开还是点三角标
    expandClickNode: {
      type: Boolean,
      default() {
        return false;
      },
    },
    //默认展开
    defaulexpand: {
      type: Boolean,
      default() {
        return true;
      },
    },
    // 默认选中的节点key
    defaultKey: {
      type: [Number, String, Array, Object],
      default() {
        return [];
      },
    },
    size: {
      type: String,
      default() {
        return "small";
      },
    },
    width: {
      type: String,
      default() {
        return "100%";
      },
    },
    height: {
      type: String,
      default() {
        return "300px";
      },
    },
    curValue: {
      default() {
        return "";
      },
    },
    notParentcheckStrictlyChildcheckStrictly: {
      //子选中，孙跟着选中，但父不选中--》1代表用此方案，0代表不用此方案
      default() {
        return 0;
      },
    },
  },

  //上面是父组件可传入参数
  data() {
    return {
      popoverWidth: "0px", //下拉框大小
      isShowSelect: false, // 是否显示树状选择器
      options: [], //select option选项
      returnDatas: [], //返回给父组件数组对象
      returnDataKeys: [], //返回父组件数组主键值
      filterText: "", //筛选绑定值
      checkedAll: false, //是否有全选按钮(只有在多选下才能配置)
      treeArrId: [],
      DepartDataArr: [],
    };
  },

  mounted() {
    // var that = this; //this的指向问题
    // document.addEventListener("click", function (e) {
    //   console.log(e);
    //   that.isShowSelect = false; //这里that代表组件，this代表document
    // });
    this.$nextTick(() => {
      if (this.curValue) {
        if (
          typeof this.curValue != "string" &&
          typeof this.curValue != "number"
        ) {
          this.setKeys(this.curValue);
        } else {
          this.setKey(this.curValue);
        }
      }
    });
  },

  computed: {
    treeData() {
      // 若非树状结构，则转化为树状结构数据
      if (this.obj.children != "") {
        // return JSON.stringify(this.data).indexOf(this.obj.children) !== -1
        //   ? this.data
        //   : this.switchTree();
        return this.data;
      } else {
        return this.data;
      }
    },
  },
  watch: {
    returnDataKeys(newValue, oldValue) {
      if (newValue.length === this.treeArrId.length) {
        this.checkedAll = true;
      } else {
        this.checkedAll = false;
      }
    },
    curValue(val) {
      //引用组件里的:curValue="defaulData" 属性就是当前展示勾选的数据Id。回显数据时给这个属性赋值就行了
      this.$nextTick(() => {
        if (val) {
          if (typeof val != "string" && typeof val != "number") {
            this.setKeys(val);
          } else {
            this.setKey(val);
          }
        }
      });
    },

    filterText(val) {
      this.$refs.tree.filter(val);
    },
    // eslint-disable-next-line no-unused-vars
    isShowSelect(val) {
      // 隐藏select自带的下拉框
      this.$refs.select.blur();
    },
    treeData() {
      //监听tree数据
      this.$nextTick(() => {
        this.init();
      });
    },
  },
  created() {
    this.treeArrId = [];
    this.originTree(this.treeData);
  },
  methods: {
    originTree(treeData) {
      treeData.forEach((item) => {
        this.treeArrId.push(item[this.obj.id]);
        if (item.children && item.children.length > 0) {
          this.originTree(item.children);
        }
      });
    },
    // 点击外部关闭
    replyOutside(v) {
      let e = v || window.event;
      let elem = e.target;
      if (
        e.target.parentNode.querySelector(".selectTree *") ||
        e.target.parentNode.querySelector(".selectTree")
      ) {
        //querySelector() 方法返回文档中匹配指定 CSS 选择器的一个元素
        return;
      } else if (this.isShowSelect) {
        this.isShowSelect = false;
      }
    },
    filterNode(value, data) {
      if (!value) return true;
      return data[this.obj.label].indexOf(value) !== -1;
    },

    init() {
      // debugger
      if (this.defaultKey != undefined && this.defaultKey.length > 0) {
        if (this.multiple) {
          // 多选
          if (
            Object.prototype.toString.call(this.defaultKey).indexOf("Array") !=
            -1
          ) {
            if (
              Object.prototype.toString
                .call(this.defaultKey[0])
                .indexOf("Object") != -1
            ) {
              //对象
              this.setDatas(this.defaultKey);
            } else if (
              Object.prototype.toString
                .call(this.defaultKey[0])
                .indexOf("Number") != -1 ||
              Object.prototype.toString
                .call(this.defaultKey[0])
                .indexOf("String") != -1
            ) {
              this.setKeys(this.defaultKey);
            } else {
              console.log("多选：传入参数类型不匹配");
              return;
            }
          } else {
            console.log("多选：传入参数类型不匹配");
            return;
          }
        } else {
          // 单选
          if (
            Object.prototype.toString.call(this.defaultKey).indexOf("Number") !=
              -1 ||
            Object.prototype.toString.call(this.defaultKey).indexOf("String") !=
              -1 ||
            Object.prototype.toString.call(this.defaultKey).indexOf("Object") !=
              -1
          ) {
            this.setKey(this.defaultKey);
          } else {
            console.log("单选：传入参数类型不匹配");
            return;
          }
        }
      }
    },
    //下拉框select点击[入口]
    selectClick() {
      this.DepartPoptableDatalist(); //点击选择框查询部门列表
      this.$nextTick(function () {
        //设置下拉框自适应宽度
        this.popoverWidth = this.$refs.select.$el.clientWidth;
      });
      //显示下拉框
      return (this.isShowSelect = !this.isShowSelect);
    },
    DepartPoptableDatalist() {
      if(this.data && this.data.length){
        this.DepartDataArr = this.data;
        this.$emit("selectClickT", this.DepartDataArr);
        this.sortTableData(this.DepartDataArr);
      }else{
        //点击选择框查询部门列表
        let obj = { domainId: this.$route.query.domainId }; //获取url域domainId
        StaffDepartmentListApi(obj).then((response) => {
          if (response.data) {
            this.DepartDataArr = response.data;
            this.$emit("selectClickT", this.DepartDataArr);
            this.sortTableData(this.DepartDataArr);
          }
        });
      }
    },
    sortTableData(arr) {
      //递归排序
      if (arr?.length) {
        arr.forEach((item) => {
          item.displayOrderNumber = item.displayOrder;
          if (item.children?.length) {
            this.sortTableData(item.children);
          }
        });
      }
      console.log(this.tableData);
    },
    //单选: 树点击方法
    nodeClick(data, node) {
      //data为obj当前部门数据以及children数据，node为boolean值，选中为true，取消为false
      //  debugger
      if (!this.multiple) {
        this.isShowSelect = false;
        //单选父子节点不关联
        if (this.radioStrictly) {
          this.setKey(node.key);
        } else {
          //   if (node.level != 1) {
          //     this.setKey(node.key);
          //   } else if (node.level == 1 && this.obj.children == "") {
          //     this.setKey(node.key);
          //   }
          if (node.level) {
            this.setKey(node.key);
          }
        }
      } else {
        //多选
        debugger;
        //新增加复选框的方案--//子选中，孙跟着选中，但父不选中--》notParentcheckStrictlyChildcheckStrictly:1代表用此方案，0代表不用此方案
        if (this.notParentcheckStrictlyChildcheckStrictly == 1) {
          //子选中--孙孙跟着选中，但父不选中
          Array.isArray(data.children) &&
            data.children.forEach((item) => {
              this.$refs.tree.setChecked(item, node);
              if (node) {
                //如果node为true选中
                this.$set(item, "disabled", true);
              } else {
                //否则不选中
                this.$set(item, "disabled", false);
              }
              this.nodeClick(item, node); //递归循环遍历children
            });
        }

        var checkedKeys = this.$refs.tree.getCheckedKeys(); // 所有被选中的节点的 key 所组成的数组数据

        checkedKeys = checkedKeys.filter(function (s) {
          if (typeof s == "String") {
            return s && s.trim();
          } else {
            return s;
          }
        });
        var t = [];
        checkedKeys = this.unique(checkedKeys);
        this.options = checkedKeys.map((item) => {
          if (item !== undefined) {
            var node = this.$refs.tree.getNode(item); // 所有被选中的节点对应的node
            t.push(node.data);
          }
          //设置option选项
          return { label: node.label, value: node.key };
        });

        this.returnDataKeys = this.options.map((item) => {
          return item.value;
        });
        this.returnDatas = t;
        this.popoverHide();
      }
    },
    unique(arr) {
      //去重
      return arr.filter(function (item, index, arr) {
        return arr.indexOf(item, 0) === index;
      });
    },
    //单选:清空选中
    clean() {
      this.$refs.tree.setCurrentKey(null); //清除树选中key
      this.returnDatas = null;
      this.returnDataKeys = "";
      this.popoverHide();
    },
    //单选:设置、初始化值 key
    setKey(thisKey) {
      this.$refs.tree.setCurrentKey(thisKey);
      var node = this.$refs.tree.getNode(thisKey);
      if (node) {
        this.setData(node.data);
      }
    },
    //单选：设置、初始化对象
    setData(data) {
      this.options = [];
      this.options.push({
        label: data[this.obj.label],
        value: data[this.obj.id],
      });
      this.returnDatas = data;
      this.returnDataKeys = data[this.obj.id];
    },
    //多选:设置、初始化值 keys
    setKeys(thisKeys) {
      // debugger
      this.$refs.tree.setCheckedKeys(thisKeys);
      this.returnDataKeys = thisKeys;
      var t = [];
      thisKeys.map((item) => {
        //设置option选项
        var node = this.$refs.tree.getNode(item); // 所有被选中的节点对应的node
        t.push(node.data);
        return { label: node.label, value: node.key };
      });
      this.returnDatas = t;
      this.popoverHide();
    },
    //多选:设置、初始化对象
    setDatas(data) {
      this.$refs.tree.setCheckedNodes(data);
      this.returnDatas = data;
      var t = [];
      data.map((item) => {
        //设置option选项
        t.push(item[this.obj.id]);
      });
      this.returnDataKeys = t;
      this.popoverHide();
    },
    // 多选,删除任一select选项的回调
    removeTag(val) {
      this.$refs.tree.setChecked(val, false); //设置为未选中
      var node = this.$refs.tree.getNode(val); //获取节点
      if (!this.checkStrictly && node.childNodes.length > 0) {
        this.treeToList(node).map((item) => {
          if (item.childNodes.length <= 0) {
            this.$refs.tree.setChecked(item, false);
          }
        });
      }
      this.nodeClick();
      this.popoverHide();
    },
    //下拉框关闭执行
    popoverHide() {
      this.$emit("getValue", this.returnDataKeys, this.returnDatas);
    },
    // 多选，清空所有勾选
    clearSelectedNodes() {
      var checkedKeys = this.$refs.tree.getCheckedKeys(); // 所有被选中的节点的 key 所组成的数组数据
      for (let i = 0; i < checkedKeys.length; i++) {
        this.$refs.tree.setChecked(checkedKeys[i], false);
      }
    },
    //多选  全选
    selectedAll(val) {
      if (val) {
        this.setKeys(this.treeArrId);
      } else {
        this.clearSelectedNodes();
      }
    },
    //树形转为集合
    treeToList(tree) {
      var queen = [];
      var out = [];
      queen = queen.concat(tree);
      while (queen.length) {
        var first = queen.shift();
        if (first.childNodes) {
          queen = queen.concat(first.childNodes);
        }
        out.push(first);
      }
      return out;
    },
    switchTree() {
      return this.buildTree(this.data, this.defaultValue);
    },
    // 将一维的扁平数组转换为多层级对象
    buildTree(data, id) {
      const fa = (id) => {
        const temp = [];
        for (let i = 0; i < data.length; i++) {
          const n = data[i];
          if (n[this.obj.pid] === id) {
            n[this.obj.children] = fa(n[this.obj.id]);
            temp.push(n);
          }
        }
        return temp;
      };
      return fa(id);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
.common-tree {
  overflow: auto;
  max-height: 300px;
  height: auto;
}
.common-tree ::v-deep .el-tree-node__expand-icon.is-leaf {
  display: none !important;
}
// .common-tree ::v-deep .el-tree-node__content > .el-tree-node__expand-icon {
//   padding: 0;
//   margin-left: 6px;
//   margin-right: 0px;
// }
.el-checkbox {
  margin-left: 10px !important;
}
.el-row {
  padding-top: 0px !important;
}
</style>
