<template>
  <el-cascader
    :options="question_classify_list"
    :props="{
      checkStrictly: true,
      value: 'id',
      label: 'name',
      children,
      emitPath: false,
    }"
    clearable
    v-model="classify"
  >
  </el-cascader>
</template>

<script>
import { findAllExamClassifyApi } from '@/api/assessment-evaluation/exam'
export default {
  data: (_) => ({
    classify: null,
    question_classify_list: []
  }),
  mounted () {
    if (this.mountedLoad) {
      this.findAllQuestionsClassify()
    }
  },
  activated () {
    this.findAllQuestionsClassify()
  },
  props: {
    /*
     * 是否在mounted生命周期中加载
     */
    mountedLoad: {
      default: false
    },
    /*
     * 绑定v-model传入类型
     */
    value: {
      default: null
    }
  },
  watch: {
    /*
     * 监听类型被修改
     */
    value: {
      immediate: true,
      deep: true,
      handler (nv) {
        this.classify = nv
      }
    },
    /*
     * 监听类型被选择
     */
    classify: {
      immediate: false,
      deep: true,
      handler (nv) {
        this.$emit('input', nv)
      }
    }
  },
  methods: {
    /*
     * 请求所有试题类型
     */
    findAllQuestionsClassify () {
      findAllExamClassifyApi()
        .then(({ data }) => {
          let questionClassifyMap = {}
          questionClassifyMap = data.toTree()
          const list = questionClassifyMap.tree
          this.question_classify_list = list
        })
        .catch((err) => {
          console.info(err)
        })
        .finally((_) => {
          this.page_is_loading = false
        })
    }
  }
}
</script>

<style>
</style>
