<template>
  <div v-loading="loading" style="min-height:400px;">
    <el-form
      v-if="isRender"
      ref="questions"
      size="small"
      :model="questionForm"
    >
      <FormItem
        v-for="question of questions"
        :key="question.ReadingQuestionTrialId"
        :question="question"
        :question-form="questionForm"
        :reading-task-state="readingTaskState"
        @resetFormItemData="resetFormItemData"
      />
    </el-form>
  </div>

</template>
<script>
import { getTrialReadingQuestion } from '@/api/trials'
import FormItem from '@/views/trials/trials-panel/reading/none-dicoms/components/FormItem.vue'
export default {
  name: 'QuestionTpl',
  components: {
    FormItem
  },
  props: {
    criterionId: {
      type: String,
      required: true
    }

  },
  data() {
    return {
      loading: false,
      questionForm: {},
      isRender: false,
      readingTaskState: 0
    }
  },
  mounted() {
    this.getQuestions()
  },
  methods: {

    getQuestions() {
      this.loading = true
      var param = {
        readingQuestionCriterionTrialId: this.criterionId
      }
      getTrialReadingQuestion(param).then(res => {
        var questions = []
        res.Result.map((v) => {
          if (v.Type === 'group' && v.Childrens.length === 0) return
          questions.push(v)
          if (v.Type !== 'group' && v.Type !== 'summary') {
            this.$set(this.questionForm, v.ReadingQuestionTrialId, v.Answer)
          }
          if (v.Childrens.length > 0) {
            this.setChild(v.Childrens)
          }
        })
        this.questions = questions
        // this.questions.map(q => {
        //   if (q.Type !== 'group' && q.Type !== 'summary') {
        //     this.$set(this.questionForm, q.ReadingQuestionTrialId, q.Answer)
        //     if (q.Childrens.length > 0) {
        //       this.setChild(q.Childrens)
        //     }
        //   }
        // })

        this.isRender = true
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    setChild(obj) {
      obj.forEach(i => {
        if (i.Type !== 'group' && i.Type !== 'summary') {
          this.$set(this.questionForm, i.ReadingQuestionTrialId, i.Answer)
        }
        if (i.Childrens && i.Childrens.length > 0) {
          this.setChild(i.Childrens)
        }
      })
    },
    resetFormItemData(v) {
      this.questionForm[v] = ''
    }
  }
}
</script>
