<template>
  <el-cascader
    :value="vModel"
    :options="options"
    :clearable="true"
    v-bind="$attrs"
    @change="onChange"
  ></el-cascader>
  <!-- :show-all-levels="false" -->
</template>

<script>
import { getPage } from '@/api/help/type';

const resetList = (arr = []) => {
  const reuslt = arr.map(x => ({
    value: x.id,
    label: x.name,
    children: x.children && x.children.length > 0 ? resetList(x.children) : null
  }));
  return reuslt;
};

const checkList = (list = []) => {
  list = list.reduce((init, item) => {
    if (item.children) {
      init.push(item);
    }
    return init;
  }, []);
  return list;
};

export default {
  name: 'IssueTypeSelect',

  props: {
    value: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      vModel: this.value,
      options: []
    };
  },

  watch: {
    value: {
      handler(val) {
        this.vModel = val;
      },
      immediate: true
    }
  },

  mounted() {
    this.loadData();
  },

  methods: {
    onChange(value) {
      this.$emit('input', value[value.length - 1] || null);
    },

    async loadData() {
      const { success, data, msg } = await getPage({
        data: {
          isShow: null
        },
        limit: 999999,
        start: 1
      });
      if (success) {
        let result = resetList(data.data);
        this.options = checkList(result);
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
