<template>
  <div v-loading="loading">
    <el-form v-if="isRender" ref="questionForm" size="small" :model="questionForm" style="width:100%;">
      <qSFormItem v-for="question of questions" :key="question.Id" :question="question" :question-form="questionForm"
        :is-audit="isAudit" @resetFormItemData="resetFormItemData" />
    </el-form>
  </div>

</template>

<script>

import { getQCQuestionAnswer } from '@/api/trials'
import qSFormItem from './qsFormItem'
export default {
  name: 'QcQuestions',
  components: {
    qSFormItem
  },
  props: {
    trialId: {
      type: String,
      required: true
    },
    subjectVisitId: {
      type: String,
      required: true
    },
    qcProcessEnum: {
      type: Number,
      required: true
    },
    currentQCEnum: {
      type: Number,
      required: true
    },
    isAudit: {
      type: Boolean,
      default: false
    },
    SecondReviewTime: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      questions: [],
      questionForm: {},
      AnswerIdObj: {},
      isRender: false
    }
  },
  watch: {
    SecondReviewTime() {
      if (this.SecondReviewTime) {
        this.getQuestions()
      }
    }
  },
  mounted() {
    this.getQuestions()
  },
  methods: {
    getQuestions() {
      this.loading = true
      var param = {
        trialId: this.trialId || this.$route.query.trialId,
        subjectVisitId: this.subjectVisitId,
        qcProcessEnum: this.qcProcessEnum,
        currentQCEnum: this.SecondReviewTime ? 3 : this.currentQCEnum
      }
      if (this.SecondReviewTime) {
        param.SecondReviewTime = this.SecondReviewTime
      }
      getQCQuestionAnswer(param).then(res => {
        res.Result.map((v) => {
          this.$set(this.questionForm, v.Id, v.Answer)
          this.$set(this.AnswerIdObj, v.Id, v.AnswerId)
          if (v.Childrens.length > 0) {
            this.setChild(v.Childrens)
          }
        })
        this.questions = res.Result

        this.isRender = true
        this.loading = false
      }).catch((err) => { console.log(err); this.loading = false })
    },
    setChild(obj) {
      obj.forEach(i => {
        this.$set(this.questionForm, i.Id, i.Answer)
        this.$set(this.AnswerIdObj, i.Id, i.AnswerId)
        if (i.Childrens && i.Childrens.length > 0) {
          this.setChild(i.Childrens)
        }
      })
    },
    resetFormItemData(v) {
      this.questionForm[v] = ''
    },
    submit() {
      return new Promise((resolve, reject) => {
        this.$refs['questionForm'].validate(valid => {
          if (!valid) {
            let message = this.$t('trials:audit:message:specifyQuestions')
            if (this.SecondReviewTime) {
              message = this.$t('trials:audit:message:specifyResetQuestions')
            }
            this.$confirm(message, {
              type: 'warning',
              showCancelButton: false,
              callback: action => { }
            })
            reject()
          } else {
            var answers = []
            for (const k in this.questionForm) {
              answers.push({ Id: k, answer: this.questionForm[k], AnswerId: this.AnswerIdObj[k] })
            }
            resolve(answers)
            // 设置当前节点的Answer
            // this.answers.forEach((item, index) => {
            //   if (item.IsShow) {
            //     this.$set(this.answers[index], 'Answer', this.questionForm[item.TrialQCQuestionConfigureId])
            //   }
            // })
            // resolve(this.answers)
          }
        })
      })
    },
    save() {
      return new Promise((resolve, reject) => {
        var answers = []
        for (const k in this.questionForm) {
          answers.push({ Id: k, answer: this.questionForm[k], AnswerId: this.AnswerIdObj[k] })
        }
        resolve(answers)
      })
    }
  }
}
</script>
