<template>
  <tree-select
    name="demo"
    :multiple="multiple"
    :clearable="clearable"
    :disabled="disabled"
    :options="options"
    :flat="multiple"
    :value-consists-of="'ALL_WITH_INDETERMINATE'"
    :flatten-search-results="true"
    :default-expand-level="1"
    zIndex="9999"
    @input="inputEvent"
    @select="selectEvent"
    @deselect="deselectEvent"
    v-model="inputValue"
    v-on="listeners"
    :always-open="alwaysOpen"
    :normalizer="normalizer"
    :noOptionsText="'暂时无数据'"
    :shouldShowCount="true"
    :noResultsText="'未匹配到相应的结果'"
    v-bind="$attrs"
  >
    <label
      slot="option-label"
      slot-scope="{ node, shouldShowCount, count, labelClassName, countClassName }"
      :class="labelClassName">
      <span v-if="group">{{ node.raw[optionLabel.id] }}/</span> {{ node.raw[optionLabel.label] }}
      <span v-if="shouldShowCount" :class="countClassName">({{ count }})</span>
      <slot name="option-label" :node="node"></slot>
    </label>
  </tree-select>
</template>

<script>
// import the component
import TreeSelect from "@riophae/vue-treeselect";
// import the styles
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "CmsFormTree",
  components: { TreeSelect },
  data() {
    let { ...listeners } = this.$listeners;
    return {
      listeners: listeners,
      options: [],
    };
  },
  computed: {
    inputValue: {
      get() {
        return this.value;
      },
      set(val) {
        console.log("===============", val);
        this.$emit("input", val);
      },
    },
  },
  activated() {

  },
  created() {
    this.$nextTick(() => {
      if (this.dataSource) {
        if (typeof this.dataSource === "string") {
        } else if (typeof this.dataSource === "function") {
          this.dataSource(this.queryParams).then((res) => {
            if (res) {
              this.options = res.data;
            }
          });
        } else if (Array.isArray(this.dataSource)) {
          this.options = this.dataSource;
          console.log('====this.options===', this.options)
        }
      }
    })
  },
  methods: {
    normalizer(node) {
      return {
        id: node[this.props.id],
        label: node[this.props.label],
        children: node[this.props.children ? this.props.children : "children"],
      };
    },
    inputEvent(value) {
      this.$emit("change", value);
    },
    selectEvent(node) {
      const matchKey = this.matchKey;
      for (let key in matchKey) {
        this.formModel[key] = node[matchKey[key]]
      }
      this.$emit("select", node);
    },
    deselectEvent(node) {
      console.log(node);
    },
  },
  props: {
    value: {
      type: [ String, Boolean, Number, Array ],
      default: undefined,
    },
    dataSource: {
      type: [ Function, String, Array ],
      default: undefined,
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    group: {
      type: Boolean,
      default: true,
    },
    props: {
      type: Object,
      default: () => {
        return {
          id: "id",
          label: "label",
          children: "children",
        };
      },
    },
    optionLabel: {
      type: Object,
      default: () => {
        return {
          id: "id",
          label: "label",
        };
      },
    },
    formModel: {
      type: Object,
      default: undefined,
    },
    queryParams: {
      type: Object,
      default: {},
    },
    matchKey: {
      type: Object,
      default: undefined,
    },
    clearable: {
      type: Boolean,
      default: true,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    alwaysOpen: {
      type: Boolean,
      default: false,
    },
    limit: {
      type: Number,
      default: undefined,
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .vue-treeselect__placeholder, .vue-treeselect__single-value {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-left: 5px;
  padding-right: 5px;
  position: absolute;
  top: 5px !important;
  right: 0;
  bottom: 0;
  left: 0;
  line-height: 32px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  pointer-events: none;
}

/deep/ .vue-treeselect__control {
  height: 40px;
  line-height: 40px;
}

/deep/ .vue-treeselect__value-container {
  height: 20px;
  line-height: 20px;
}

/deep/ .vue-treeselect__single-value {
  // top: 3px !important;
  height: 40px;
  line-height: 40px;
}

/deep/ .vue-treeselect__multi-value-item-container {
  padding-top: 2px;
  padding-bottom: 2px;
}

/deep/ .vue-treeselect__multi-value {
  height: 35px !important;
}

/deep/ .vue-treeselect__multi-value-item * {
  line-height: 28px !important;
}

/deep/ .vue-treeselect--has-value .vue-treeselect__input {
  vertical-align: middle;
  line-height: 30px !important;
  height: 30px !important;
}
</style>
