<template>
  <el-form
    v-if="questions.length>0"
    ref="globalReviewForm"
    v-loading="loading"
    :model="form"
    size="small"
    label-width="150"
  >
    <div class="base-dialog-body">
      <!-- 是否同意访视整体评估 GlobalAnswerType:2-->
      <!-- :label="agreeOrNot.QuestionName" -->
      <el-form-item

        :label="$t('trials:globalReview:table:isAgreeEvaluationRes')"
      >
        <!-- <el-switch
          v-model="form[agreeOrNot.GlobalAnswerType]"
          active-value="1"
          inactive-value="0"
        /> -->
        <el-radio-group v-model="form[agreeOrNot.GlobalAnswerType]" @change="handleAgreeOrNotChange(agreeOrNot.GlobalAnswerType)">
          <el-radio label="1">{{ $fd('ReadingYesOrNo',1) }}</el-radio>
          <el-radio label="0">{{ $fd('ReadingYesOrNo',0) }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="parseInt(form[agreeOrNot.GlobalAnswerType]) === 0">
        <el-form-item
          v-for="qs in questions"
          v-show="((isBaseline && qs.GlobalReadingShowType === 1) || (!isBaseline && qs.GlobalReadingShowType === 2 ) || qs.GlobalReadingShowType === 0)"
          :key="`${qs.QuestionId}${qs.QuestionName}`"
          :label="qs.QuestionName"
          :prop="!qs.QuestionId?String(qs.GlobalAnswerType):qs.QuestionId"
          :rules="[
            { required:parseInt(form[agreeOrNot.GlobalAnswerType]) !== 1 && ((isBaseline && qs.GlobalReadingShowType === 1) || (!isBaseline && qs.GlobalReadingShowType === 2 ) || qs.GlobalReadingShowType === 0),message: $t('common:ruleMessage:specify'), trigger: ['change']},
          ]"
        >
          <!-- 裁判问题 GlobalAnswerType:0 -->
          <template v-if="qs.GlobalAnswerType === 0 ">
            <!-- 下拉框 -->
            <el-select
              v-if="qs.Type==='select'"
              v-model="form[!qs.QuestionId?qs.GlobalAnswerType:qs.QuestionId]"
              style="width:100%;"
              :disabled="parseInt(form[agreeOrNot.GlobalAnswerType]) === 1"
            >
              <template v-if="qs.TypeValue">
                <el-option
                  v-for="val in qs.TypeValue.split('|')"
                  :key="val"
                  :label="val"
                  :value="val"
                  :disabled="getBeforeAnswer(qs.QuestionName) === val"
                />
              </template>
              <template v-if="qs.DictionaryCode">
                <el-option
                  v-for="item of $d[qs.DictionaryCode]"
                  v-if="qs.QuestionType !== 13 || item.value !== -1"
                  :key="item.id"
                  :value="item.value"
                  :label="item.label"
                  :disabled="getBeforeAnswer(qs.QuestionName) === item.value"
                />
              </template>

            </el-select>
            <!-- 单选 -->
            <el-radio-group
              v-if="qs.Type==='radio'"
              v-model="form[!qs.QuestionId?qs.GlobalAnswerType:qs.QuestionId]"
              :disabled="parseInt(form[agreeOrNot.GlobalAnswerType]) === 1"
            >
              <el-radio
                v-for="val in qs.TypeValue.split('|')"
                :key="val"
                :label="val"
                :disabled="getBeforeAnswer(qs.QuestionName) === val"
              >
                {{ val }}
              </el-radio>
            </el-radio-group>
            <template v-if="qs.Type==='calculation'">
              <el-input v-if="qs.DictionaryCode === '' && qs.ValueType === null" v-model="form[!qs.QuestionId?qs.GlobalAnswerType:qs.QuestionId]" :disabled="parseInt(form[agreeOrNot.GlobalAnswerType]) === 1" style="width:100%;" />
              <el-select v-else-if="qs.DictionaryCode" v-model="form[!qs.QuestionId?qs.GlobalAnswerType:qs.QuestionId]" :disabled="parseInt(form[agreeOrNot.GlobalAnswerType]) === 1" style="width:100%;">
                <el-option v-for="item of $d[qs.DictionaryCode]" :key="item.id" :disabled="getBeforeAnswer(qs.QuestionName) === item.value" :value="item.value" :label="item.label">{{ item.label }}</el-option>
              </el-select>
              <el-input-number v-else-if="qs.ValueType !== null" v-model="form[!qs.QuestionId?qs.GlobalAnswerType:qs.QuestionId]" :disabled="parseInt(form[agreeOrNot.GlobalAnswerType]) === 1" style="width:100%;" />
            </template>

          </template>

          <!-- 评估更新类型 GlobalAnswerType:3 -->
          <template v-if="qs.GlobalAnswerType === 3">
            <!-- 下拉框 -->
            <el-select
              v-model="form[!qs.QuestionId?qs.GlobalAnswerType:qs.QuestionId]"
              style="width:100%;"
              :disabled="parseInt(form[agreeOrNot.GlobalAnswerType]) === 1"
            >
              <el-option
                v-for="val in assessTypes"
                :key="val.Code"
                :label="val.ValueCN"
                :value="val.Code"
                :disabled="getBeforeAnswer(qs.QuestionName) === val.Code"
              />
            </el-select>
          </template>
          <!-- 全局阅片备注 GlobalAnswerType:1 -->
          <el-input
            v-if="qs.GlobalAnswerType === 1"
            v-model="form[!qs.QuestionId?qs.GlobalAnswerType:qs.QuestionId]"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            :disabled="parseInt(form[agreeOrNot.GlobalAnswerType]) === 1"
          />
        </el-form-item>
      </template>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item style="text-align:right;">
        <el-button size="small" type="primary" @click="handleClose">
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button size="small" type="primary" @click="handleSave">
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { saveGlobalReadingInfo } from '@/api/trials'
export default {
  name: 'EditGlobalForm',
  props: {
    globalTaskId: {
      type: String,
      required: true
    },
    visitTaskId: {
      type: String,
      required: true
    },
    questionList: {
      type: Array,
      default() {
        return []
      }
    },
    assessTypeList: {
      type: Array,
      default() {
        return []
      }
    },
    subjectId: {
      type: String,
      required: true
    },
    trialId: {
      type: String,
      required: true
    },
    beforeQuestionList: {
      type: Array,
      default() {
        return []
      }
    },
    globalUpdateType: {
      type: Array,
      default() {
        return []
      }
    },
    agreeOrNot: {
      type: Object,
      default() {
        return {}
      }
    },
    isBaseline: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      questions: [],
      form: {},
      loading: false,
      assessTypes: []
    }
  },
  mounted() {
    console.log(2222)
    this.assessTypes = this.assessTypeList.filter(i => i.IsBaseLineUse === this.isBaseline)
    this.questionList.map(v => {
      var answer = ''
      if (v.DictionaryCode) {
        answer = v.Answer ? parseInt(v.Answer) : null
      } else {
        answer = v.Answer
      }
      if (!v.QuestionId) {
        this.$set(this.form, v.GlobalAnswerType, answer)
      } else {
        this.$set(this.form, v.QuestionId, answer)
      }
    })
    this.questions = this.questionList
    this.$set(this.form, this.agreeOrNot.GlobalAnswerType, this.agreeOrNot.Answer)
  },
  methods: {
    handleSave() {
      this.$refs.globalReviewForm.validate(valid => {
        if (!valid) return
        this.loading = true
        var questionList = []
        var reg = new RegExp(/^[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}$/)
        for (const i in this.form) {
          if (reg.test(i)) {
            questionList.push({
              questionId: i,
              visitTaskId: this.visitTaskId,
              answer: this.form[i]
            })
          } else {
            questionList.push({
              questionId: '',
              visitTaskId: this.visitTaskId,
              globalAnswerType: parseInt(i),
              answer: this.form[i]
            })
          }
        }
        var param = {
          globalTaskId: this.globalTaskId,
          subjectId: this.subjectId,
          trialId: this.trialId,
          questionList: questionList
        }
        saveGlobalReadingInfo(param).then(res => {
          this.loading = false
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.$emit('getList')
          this.$emit('close')
        }).catch(() => {
          this.loading = false
        })
      })
    },
    handleClose() {
      this.$emit('close')
    },
    handleAgreeOrNotChange(k) {
      var agreeOrNot = parseInt(this.form[k])
      if (agreeOrNot === 1) {
        for (const i in this.form) {
          if (i !== String(k)) {
            this.form[i] = ''
          }
        }
      }
    },
    getBeforeAnswer(qs) {
      var i = this.beforeQuestionList.findIndex(item => item.QuestionName === qs)
      if (i > -1 && this.beforeQuestionList[i].Answer) {
        var answer = null
        if (this.beforeQuestionList[i].DictionaryCode) {
          answer = parseInt(this.beforeQuestionList[i].Answer)
        } else {
          answer = this.beforeQuestionList[i].Answer
        }
        return answer
      } else {
        return ''
      }
    }

  }
}
</script>
