<template>
  <a-cascader v-model="selected" :style="{width:'80%'}" :options="options" :show-search="{ filter }" :display-render="displayRender" expand-trigger="hover" placeholder="请选择学科代码" @change="onChange" />
</template>
<script>
import { specList, getSpecArray } from "@/views/components/common/config"
export default {
  name: "cascaderSelect",
  data () {
    return {
      options: specList,
      selected: null,
    };
  },
  props: {
    value: {
      type: String,
      default () {
        return null
      }
    },
  },
  created () {
    if (!!this.value) {
      this.selected = getSpecArray(this.value)
      // this.$emit("change");
      // this.$emit("input", this.selected)
    }
  },
  methods: {
    onChange (value, selectedOptions) {
      this.selected = value
      if (!!this.selected && this.selected.length == 3) {
        this.$emit("input", this.selected[2])
        this.$emit("change");
      }
    },
    displayRender ({ labels }) {
      return labels[labels.length - 1];
    },
    filter (inputValue, path) {
      return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
    },
  },
  watch: {
    value: {
      handler (value) {
        if (!!value) {
          this.selected = getSpecArray(this.value)
          if (!!this.selected && this.selected.length == 3) {
            this.$emit("input", this.selected[2])
            this.$emit("change");
          }
        }
      },
    },
  }
};
</script>
