<template>
  <div v-loading="loading" class="issues-form">
    <el-form
      v-if="isRender"
      ref="questions"
      size="small"
      :model="questionForm"
      label-position="left"
      label-width="800px"
    >
      <FormItem
        v-for="question of questions"
        :key="question.Id"
        :question="question"
        :question-form="questionForm"
        :audit-state="auditState"
        :is-send-message="isSendMessage"
        @resetFormItemData="resetFormItemData"
      />

    </el-form>
  </div>

</template>
<script>
import { saveMedicineQuestion } from '@/api/trials'
import FormItem from './FormItem'
export default {
  name: 'QuestionTpl',
  components: {
    FormItem
  },
  props: {
    questionAnswerList: {
      type: Array,
      default() {
        return []
      }
    },
    taskMedicalReviewId: {
      type: String,
      required: true
    },
    auditState: {
      type: Number,
      required: true
    },
    isSendMessage: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      questionForm: {},
      isRender: false,
      trialId: '',
      readingTaskState: 0,
      userTypeEnumInt: zzSessionStorage.getItem('userTypeEnumInt') * 1
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getQuestions()
  },
  methods: {
    handleSave(isPrompt) {
      return new Promise((resolve, reject) => {
        this.$refs['questions'].validate((valid) => {
          if (!valid) {
            reject()
          } else {
            var answers = []
            for (const k in this.questionForm) {
              answers.push({ id: k, answer: this.questionForm[k] })
            }
            var params = {
              visitTaskId: this.visitTaskId,
              taskMedicalReviewId: this.taskMedicalReviewId,
              questionAnswerList: answers

            }
            saveMedicineQuestion(params).then(res => {
              if (isPrompt) {
                this.$message.success(this.$t('common:message:savedSuccessfully'))
              }
              resolve()
            }).catch(() => {
              reject()
            })
          }
        })
      })
    },
    save() {
      return new Promise((resolve, reject) => {
        this.$refs['questions'].validate((valid) => {
          if (!valid) {
            resolve(false)
          } else {
            this.loading = true
            var answers = []
            for (const k in this.questionForm) {
              answers.push({ id: k, answer: this.questionForm[k] })
            }
            var params = {
              visitTaskId: this.visitTaskId,
              taskMedicalReviewId: this.taskMedicalReviewId,
              questionAnswerList: answers

            }
            saveMedicineQuestion(params).then(res => {
              this.loading = false
              resolve(true)
            }).catch(() => {
              this.loading = false
              reject()
            })
          }
        })
      })
    },
    validForm() {
      return new Promise((resolve) => {
        this.$refs['questions'].validate((valid) => {
          resolve(valid)
        })
      })
    },
    getQuestions() {
      this.loading = true
      var questions = []
      this.questionAnswerList.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.Id, v.Answer ? v.Answer : '')
          if (v.Childrens.length > 0) {
            this.setChild(v.Childrens)
          }
        }
      })
      this.questions = questions
      this.isRender = true
      this.loading = false
    },
    setChild(obj) {
      obj.forEach(i => {
        this.$set(this.questionForm, i.Id, i.Answer)
        if (i.Childrens && i.Childrens.length > 0) {
          this.setChild(i.Childrens)
        }
      })
    },
    resetFormItemData(v) {
      this.questionForm[v] = null
    }
  }
}
</script>
<style lang="scss" scoped>
.issues-form{
  ::v-deep .el-radio-group{
    width:300px;
  }
  ::v-deep .el-textarea{
    width:300px;
  }
  ::v-deep .el-input{
    width:300px;
  }
  ::v-deep .el-select{
    width:300px;
  }

}
</style>
