<template>
  <div>
    <el-form ref="readingCriterionsForm" v-loading="loading" :model="form" :rules="rules" label-width="120px"
      size="small">
      <!-- '表单问题' -->
      <el-form-item :label="$t('trials:readingUnit:readingCriterion:title:formQs')">
        <questions-list :ref="`questionList${trialReadingCriterionId}`" v-if="form.FormType === 1"
          :trial-reading-criterion-id="trialReadingCriterionId" :list="readingInfo.TrialQuestionList"
          :trial-criterion-id="readingInfo.TrialCriterionId" :is-confirm="isConfirm"
          :is-system-criterion="isSystemCriterion" :is-from-system="readingInfo.IsFromSystem"
          :digit-places="digitPlaces" :readingTools="readingTools" :readingSegmentTools="readingSegmentTools"
          :readingVersionEnum="readingVersionEnum" @reloadArbitrationRules="reloadArbitrationRules" />
      </el-form-item>
      <el-form-item v-if="hasPermi(['trials:trials-panel:setting:reading-unit:edit'])">
        <!--        &lt;!&ndash; 保存 &ndash;&gt;-->
        <!--        <el-button-->
        <!--          v-if="!isConfirm && isAdditionalAssessment"-->
        <!--          type="primary"-->
        <!--          @click="handleSave(true)"-->
        <!--        >-->
        <!--          {{ $t('common:button:save') }}-->
        <!--        </el-button>-->
        <!-- 基础数据配置 -->
        <el-button type="primary" @click="configBaseDataVisible = true">
          {{ $t('trials:readingUnit:readingCriterion:title:baseDataCfg') }}
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 基础数据配置 -->
    <el-dialog v-if="configBaseDataVisible" :title="$t('trials:readingUnit:readingCriterion:title:baseDataCfg')"
      :visible.sync="configBaseDataVisible" :close-on-click-modal="false" :fullscreen="true" append-to-body
      custom-class="base-dialog-wrapper">
      <BaseDataConfig :trial-reading-criterion-id="trialReadingCriterionId" :is-from-system="readingInfo.IsFromSystem"
        :is-confirm="isConfirm" />
    </el-dialog>


  </div>
</template>
<script>
import { getTrialReadingCriterionInfo, setTrialReadingCriterion, getTrialCriterionAdditionalAssessmentOptionList, setTrialCriterionAdditionalAssessment } from '@/api/trials'
import PageBreakList from './PageBreakList'
import QuestionsList from './QuestionsList'
import BaseDataConfig from './BaseDataConfig'
export default {
  name: 'ReadingCriterion',
  components: { PageBreakList, QuestionsList, BaseDataConfig },
  props: {
    digitPlaces: {
      type: Number,
      default: 0
    },
    trialReadingCriterionId: {
      type: String,
      default: ''
    },
    isAdditionalAssessment: {
      type: Boolean,
      default: false
    },
    readingTools: {
      type: Array,
      default() {
        return []
      }
    },
    readingSegmentTools: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      loading: false,
      isSystemCriterion: null,
      form: {
        TrialCriterionId: '',
        FormType: null,
        DigitPlaces: null
      },
      rules: {
        FormType: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        DigitPlaces: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ]
      },
      readingInfo: {},
      isConfirm: true,
      configBaseDataVisible: false,
      additionalAssessmentOptionList: null,
      readingVersionEnum: 0
    }
  },
  mounted() {
    this.initPage()
  },
  methods: {
    initPage() {
      this.loading = true
      const trialId = this.$route.query.trialId
      getTrialCriterionAdditionalAssessmentOptionList(this.trialReadingCriterionId).then(res => {
        this.additionalAssessmentOptionList = res.Result
        if (this.additionalAssessmentOptionList.length > 0) {
          this.additionalAssessmentOptionList.forEach(v => {
            this.$set(this.form, 'AdditionalAssessmentType' + v.Id, v.IsSelected)
          })
        }
        getTrialReadingCriterionInfo({ trialId, TrialReadingCriterionId: this.trialReadingCriterionId }).then(res => {
          this.loading = false
          this.readingVersionEnum = res.OtherInfo.ReadingVersionEnum
          this.readingInfo = res.Result
          for (const k in this.form) {
            if (res.Result.hasOwnProperty(k)) {
              this.form[k] = res.Result[k]
            }
          }
          this.isConfirm = res.Result.IsSign
          this.isSystemCriterion = res.Result.IsSystemCriterion
        }).catch(() => {
          this.loading = false
        })
      }).catch(() => {
        this.loading = false
      })
    },
    // 配置信息保存
    handleSave(isPrompt = true) {
      return new Promise((resolve, reject) => {
        this.$refs['readingCriterionsForm'].validate((valid) => {
          if (!valid) {
            resolve(false)
          } else {
            this.loading = true
            setTrialCriterionAdditionalAssessment(this.additionalAssessmentOptionList).then(res => {
              this.loading = false
              this.$message.success(this.$t('common:message:savedSuccessfully'))
              resolve(true)
            }).catch(() => {
              this.loading = false
              resolve(false)
            })
            // 保存配置信息
            // if (!isPrompt) {
            //   this.form.IsSignSave = true
            // }
            // this.form.TrialReadingCriterionId = this.trialReadingCriterionId
            // setTrialReadingCriterion(this.form).then(res => {
            //   this.loading = false
            //   if (res.IsSuccess && isPrompt) {
            //     this.$message.success(this.$t('common:message:savedSuccessfully'))
            //   }
            //   this.$emit('reloadArbitrationRules')
            //   resolve(true)
            // }).catch(_ => {
            //   this.loading = false
            //   resolve(false)
            // })
          }
        })
      })
    },
    getQuestionLength() {
      return this.$refs['questionList' + this.trialReadingCriterionId].tblList.length
    },
    reloadArbitrationRules() {
      this.$emit('reloadArbitrationRules')
    },
    handleConfig() { }

  }
}
</script>
