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

<script>
import { getList } 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) {
      console.log(value);
      this.$emit('input', value[1] || null);
    },

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

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