<template>
  <div v-loading="loading">
    <el-form
      v-if="isRender"
      ref="questions"
      size="small"
      :model="questionForm"
    >
      <el-form-item v-if="auditState!==2 && userTypeEnumInt===14" style="text-align:right;width: 500px;">
        <!-- 保存 -->
        <!-- <el-button
          type="primary"
          size="small"
          @click="handleSave(true)"
        >
          {{ $t('common:button:save') }}
        </el-button> -->
      </el-form-item>
      <FormItem
        v-for="question of questions"
        :key="question.Id"
        :question="question"
        :question-form="questionForm"
        :audit-state="state"
        @resetFormItemData="resetFormItemData"
      />

    </el-form>
  </div>

</template>
<script>
import { saveMedicineQuestion } from '@/api/trials'
import FormItem from '@/views/trials/trials-panel/setting/medical-audit/components/FormItem'
export default {
  name: 'QuestionTpl',
  components: {
    FormItem
  },
  props: {
    questionAnswerList: {
      type: Array,
      default() {
        return []
      }
    },
    taskMedicalReviewId: {
      type: String,
      required: true
    },
    auditState: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      questionForm: {},
      isRender: false,
      trialId: '',
      readingTaskState: 0,
      userTypeEnumInt: zzSessionStorage.getItem('userTypeEnumInt') * 1,
      state: zzSessionStorage.getItem('userTypeEnumInt') * 1 === 14 ? this.auditState : 2
    }
  },
  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 {
            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 => {
              if (isPrompt) {
                this.$message.success(this.$t('common:message:savedSuccessfully'))
              }
              this.loading = false
              resolve()
            }).catch(() => {
              this.loading = false
              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()
            })
          }
        })
      })
    },
    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>
