<template>
  <div class="reading-rule-config-form">
    <!-- 配置信息表单 -->
    <el-form
      ref="readingRulesForm"
      v-loading="loading"
      :model="form"
      style="width:800px;"
      :rules="rules"
      label-width="260px"
      size="small"
    >
      <!-- 阅片平台 -->
      <el-form-item
        :label="$t('trials:processCfg:form:imagePlatform')"
        prop="ImagePlatform"
      >
        <el-radio-group
          v-model="form.ImagePlatform"
          :disabled="isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit']))"
        >
          <el-radio
            v-for="item of $d.ImagePlatform"
            :key="item.id"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 阅片工具 -->
      <el-form-item
        v-if="form.ImagePlatform === 3"
        :label="$t('trials:readingUnit:readingRules:title:readingTool')"
        prop="ReadingTool"
      >
        <el-radio-group
          v-model="form.ReadingTool"
          :disabled="isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit']))"
        >
          <el-radio
            v-for="item of $d.ReadingTool"
            :key="item.id"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        :label="$t('trials:processCfg:form:readingType')"
        prop="ReadingType"
      >
        <el-radio-group
          v-model="form.ReadingType"
          :disabled="isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit']))"
        >
          <el-radio
            v-for="item of $d.ReadingMethod"
            :key="item.id"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 是否阅片期 -->
      <el-form-item
        :label="$t('trials:readingUnit:readingRules:title:isReadingPeriod')"
        prop="IsReadingPeriod"
      >
        <el-radio-group
          v-model="form.IsReadingPeriod"
          :disabled="isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit']))"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="`IsReadingPeriod${item.value}`"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="$t('trials:processCfg:form:global')"
        prop="IsGlobalReading"
      >
        <el-radio-group
          v-model="form.IsGlobalReading"
          :disabled="IsMustGlobalReading || isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit']))"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="`IsGlobalReading${item.value}`"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        v-if="form.ReadingType === 2"
        :label="$t('trials:processCfg:form:ad')"
        prop="IsArbitrationReading"
      >
        <el-radio-group
          v-model="form.IsArbitrationReading"
          :disabled="isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit']))"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="`IsArbitrationReading${item.value}`"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        :label="$t('trials:processCfg:form:clinical')"
        prop="IsOncologyReading"
      >
        <el-radio-group
          v-model="form.IsOncologyReading"
          :disabled="form.IsSystemCriterion || isConfirm || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="`IsOncologyReading${item.value}`"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 任务展示方式 -->
      <el-form-item
        :label="$t('trials:readingUnit:readingRules:title:readingTaskViewInOrder')"
        prop="IsReadingTaskViewInOrder"
      >
        <el-radio-group
          v-model="form.IsReadingTaskViewInOrder"
          :disabled="isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit']))"
          @change="(v) => {
            if (v) {
              form.IsReadingShowSubjectInfo = true
              form.IsReadingShowPreviousResults = true
              IsNotReadingShowSubjectInfoChange = true
              form.IseCRFShowInDicomReading = false
            } else {
              IsNotReadingShowSubjectInfoChange = false
            }
          }"
        >
          <el-radio
            v-for="item of $d.IsReadingTaskViewInOrder"
            :key="'IsReadingTaskViewInOrder' + item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <!-- 任务组织级别 -->
      <el-form-item
        :label="$t('trials:readingUnit:readingRules:title:readingTaskViewEnum')"
        prop="ReadingTaskViewEnum"
      >
        <el-radio-group
          v-model="form.ReadingTaskViewEnum"
          :disabled="isConfirm || !isReadingTaskViewInOrder|| (!hasPermi(['trials:trials-panel:setting:reading-unit:edit']))"
        >
          <el-radio
            v-for="item of $d.ReadingTaskViewEnum"
            :key="item.id"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 影像是否有标注 -->
      <!-- <el-form-item
        label="影像是否有标注"
        prop="IsImageLabeled"
      >
        <el-radio-group
          v-model="form.IsImageLabeled"
          :disabled="isConfirm"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="'IsImageLabeled'+item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item> -->
      <!-- IR阅片页面是否显示受试者信息 -->
      <el-form-item
        :label="$t('trials:readingUnit:readingRules:title:isReadingShowSubjectInfo')"
        prop="IsReadingShowSubjectInfo"
      >
        <el-radio-group
          v-model="form.IsReadingShowSubjectInfo"
          :disabled="isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit'])) || form.IsReadingTaskViewInOrder"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="'IsReadingShowSubjectInfo'+item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- IR阅片页面是否可以查看既往任务结果 -->
      <el-form-item
        :label="$t('trials:readingUnit:readingRules:title:isReadingShowPreviousResults')"
        prop="IsReadingShowPreviousResults"
      >
        <el-radio-group
          v-model="form.IsReadingShowPreviousResults"
          :disabled="isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit'])) || form.IsReadingTaskViewInOrder"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="'IsReadingShowPreviousResults'+item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 修约小数位数 -->
      <el-form-item
        :label="$t('trials:processCfg:form:digitPlaces')"
        prop="DigitPlaces"
      >
        <el-radio-group
          v-model="form.DigitPlaces"
          :disabled="isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit']))"
        >
          <el-radio
            v-for="item of $d.DigitPlaces"
            :key="item.id"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- eCRF是否显示在影像阅片页面 -->
      <el-form-item
        :label="$t('trials:readingUnit:readingRules:title:isECRFShowInDicomReading')"
        prop="IseCRFShowInDicomReading"
      >
        <el-radio-group
          v-model="form.IseCRFShowInDicomReading"
          :disabled="form.IsSystemCriterion || isConfirm || (!hasPermi(['trials:trials-panel:setting:reading-unit:edit'])) || form.IsReadingTaskViewInOrder"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="item.label"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="!isConfirm && hasPermi(['trials:trials-panel:setting:reading-unit:edit'])">
        <!-- 保存 -->
        <el-button
          type="primary"
          @click="handleSave(true)"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </el-form>

  </div>
</template>
<script>

import { getCriterionReadingInfo, setCriterionReadingInfo } from '@/api/trials'
export default {
  name: 'ReadingRules',
  props: {
    trialReadingCriterionId: {
      type: String,
      default() {
        return ''
      }
    }
  },
  data() {
    return {
      form: {
        TrialId: '',
        ImagePlatform: null,
        ReadingTool: 0,
        ReadingTaskViewEnum: null,
        IsImageLabeled: null,
        IsReadingShowSubjectInfo: null,
        IsReadingShowPreviousResults: null,
        ReadingType: 1,
        IsGlobalReading: false,
        IsArbitrationReading: false,
        IsOncologyReading: 0,
        IsReadingTaskViewInOrder: false,
        IsReadingPeriod: false,
        // IsReadingTaskViewInOrder: null,
        ArbitrationRule: null,
        GlobalUpdateType: null,
        IsSystemCriterion: true,
        DigitPlaces: null,
        IseCRFShowInDicomReading: false
      },
      rules: {
        ImagePlatform: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        ReadingTool: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        ReadingTaskViewEnum: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        IsImageLabeled: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        IsReadingShowSubjectInfo: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        IsReadingShowPreviousResults: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        ReadingType: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        IsReadingTaskViewInOrder: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        IsGlobalReading: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        IsArbitrationReading: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        IsOncologyReading: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        DigitPlaces: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        IseCRFShowInDicomReading: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ],
        IsReadingPeriod: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        ]
        // IsReadingTaskViewInOrder: [
        //   { required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }
        // ]
      },
      loading: false,
      readingInfo: {},
      isConfirm: true,
      isReadingTaskViewInOrder: null,
      IsMustGlobalReading: false,
      IsNotReadingShowSubjectInfoChange: false
    }
  },
  mounted() {
    this.initPage()
    console.log(this.$d.GlobalAssessType)
  },
  methods: {
    initPage() {
      this.loading = true
      const trialId = this.$route.query.trialId
      getCriterionReadingInfo({ trialId, TrialReadingCriterionId: this.trialReadingCriterionId }).then(res => {
        this.loading = false
        for (const k in this.form) {
          if (res.Result.hasOwnProperty(k)) {
            this.form[k] = res.Result[k]
          }
        }
        this.isReadingTaskViewInOrder = res.Result.IsReadingTaskViewInOrder
        this.isConfirm = res.Result.IsSign
        this.IsMustGlobalReading = res.Result.IsMustGlobalReading
        this.$emit('setConfirm', res.Result.IsSign)
        this.$emit('setArbitrationReading', res.Result.IsArbitrationReading)
        this.$emit('setIsClinicalReading', res.Result.IsClinicalReading)
        this.$emit('setGlobalReading', res.Result.IsGlobalReading)
        this.$emit('setOncologyReading', res.Result.IsOncologyReading)
        this.$emit('setDigitPlaces', res.Result.DigitPlaces)
        if (res.Result.ReadingType === 1) {
          this.$emit('setArbitrationReading', false)
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // 配置信息保存
    handleSave(isPrompt = true) {
      return new Promise((resolve, reject) => {
        this.$refs['readingRulesForm'].validate((valid) => {
          if (!valid) {
            resolve(false)
          } else {
            this.loading = true
            if (this.form.ReadingType === 1) {
              this.form.IsArbitrationReading = false
            }
            // 保存配置信息
            this.form.TrialReadingCriterionId = this.trialReadingCriterionId
            setCriterionReadingInfo(this.form).then(res => {
              this.loading = false
              this.$emit('reloadArbitrationRules')
              this.$emit('setArbitrationReading', this.form.IsArbitrationReading)
              this.$emit('setIsClinicalReading', this.form.IsClinicalReading)
              this.$emit('setGlobalReading', this.form.IsGlobalReading)
              this.$emit('setOncologyReading', this.form.IsOncologyReading)
              this.$emit('setDigitPlaces', this.form.DigitPlaces)
              if (this.form.ReadingType === 1) {
                this.$emit('setArbitrationReading', false)
              }
              if (res.IsSuccess && isPrompt) {
                this.$message.success(this.$t('common:message:savedSuccessfully'))
              }
              resolve(true)
            }).catch(_ => {
              this.loading = false
              resolve(false)
            })
          }
        })
      })
    }

  }
}
</script>
<style lang="scss" scoped>
.reading-rule-config-form{
  .fontColor{
    color: red;
  }
}
</style>
