<!-- eslint-disable -->
<!-- eslint-disable vue/no-unused-vars -->
<!-- eslint-disable vue/no-template-shadow -->
/* eslint-disable */
<template>
  <el-form v-loading="loading" :label-width="'100px'">
    <!-- 仲裁对象 -->
    <el-form-item :label="$t('trials:adRules:title:arbitrationRule')">
      <el-radio-group v-model="JudgyInfo.ArbitrationRule" :disabled="isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])">
        <el-radio v-for="item of $d.ArbitrationRule" :key="item.id"
          :disabled="!JudgyInfo.IsReadingTaskViewInOrder || true" :label="item.value" @change="changeArbitrationRule">
          {{ item.label }}
        </el-radio>
      </el-radio-group>
    </el-form-item>
    <!-- 仲裁模式JudgeModeEnum 整体结果择一判定；指标组合判定-->
    <el-form-item :label="$t('trials:adRules:title:judgeMode')">
      <el-radio-group v-model="JudgyInfo.JudgeModeEnum" :disabled="isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])">
        <el-radio
          v-for="item of $d.JudgeMode"
          :key="item.id"
          :disabled="JudgyInfo.ArbitrationRule !== 1"
          :label="item.value"
        >
          {{ item.label }}
        </el-radio>
      </el-radio-group>
    </el-form-item>
    <!-- 裁判规则JudgeRuleEnum 访视一致规则；访视差异规则-->
    <el-form-item :label="$t('trials:adRules:title:judgeRule')">
      <el-radio-group v-model="JudgyInfo.JudgeRuleEnum" :disabled="isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])">
        <el-radio
          v-for="item of $d.JudgeRule"
          :key="item.id"
          :label="item.value"
        >
          {{ item.label }}
        </el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button
        v-hasPermi="['trials:trials-panel:setting:reading-unit:edit']"
        type="primary"
        size="small"
        @click="saveJugeRule"
        v-show="!isSign">
        {{ $t('common:button:save') }}
      </el-button>
    </el-form-item>
    <ArbitrationRulesByVisit 
      v-if="judgeRuleEnum === 1" 
      ref="visitRules"
      :trial-reading-criterion-id="trialReadingCriterionId"
      :isSign="isSign"
    />
    <el-collapse v-else-if="judgeRuleEnum === 0" v-model="activeNames" style="border-top:none;">
      <el-collapse-item v-for="(item, index) of QuestionList" :key="item.ReadingQuestionTrialId"
        style="position: relative;padding:0 10px;" :name="item.ReadingQuestionTrialId">
        <div slot="title">
          {{ item.PageName }}Q{{ index + 1 }}：{{ item.QuestionName }}
        </div>
        <div style="position: relative">
          <el-button v-hasPermi="['trials:trials-panel:setting:reading-unit:edit']"
            style="position: absolute; right: 0; top: 0; z-index: 10" size="mini" type="primary"
            :disabled="isSign" @click="apply(item.ReadingQuestionTrialId, index)">{{ $t('common:button:save')
            }}</el-button>
          <el-button v-hasPermi="['trials:trials-panel:setting:reading-unit:edit']"
            style="position: absolute; right: 60px; top: 0; z-index: 10" size="mini" type="primary"
            :disabled="isSign" @click="reset(item.ReadingQuestionTrialId, index)">{{ $t('common:button:reset')
            }}</el-button>
          <!-- 产生裁判阅片的条件 -->
          <el-form-item :label-width="'280px'" :label="$t('trials:adRules:title:judgeTypeEnum')" style="width: 95%">
            <el-radio-group v-model="QuestionList[index].JudgeType" @input="(val) => JudgeTypeChange(val, index)">
              <template v-for="item of $d.JudgeTypeEnum">
                <el-radio 
                  style="margin-bottom: 5px" 
                  :key="item.id" 
                  :disabled="isSign ||  !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])" 
                  :label="item.value"
                  v-if="
                    (['number', 'calculation'].includes(QuestionList[index].Type)
                      ? [1, 4, 5]
                      : [1, 2, 3]
                    ).includes(item.value)
                  "
                >
                  {{ item.label }}
                </el-radio>
              </template>
            </el-radio-group>
          </el-form-item>
        </div>
        <div v-if="QuestionList[index].JudgeType === 3" style="text-align: right; margin: 10px 0">
          <!-- 添加规则 -->
          <el-button v-hasPermi="['trials:trials-panel:setting:reading-unit:edit']" size="mini" type="primary"
            :disabled="isSign" @click="addGroup(index, null)">{{ $t('trials:adRules:button:addRule')
            }}</el-button>
        </div>
        <el-table v-if="QuestionList[index].JudgeType === 3" v-loading="loading"
          :data="QuestionList[index].AnswerGroupList" border stripe>
          <!-- 序号 -->
          <el-table-column prop="AnswerGroupA" :label="$t('trials:adRules:title:order')" width="160">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <!-- 阅片人A -->
          <el-table-column prop="AnswerGroupA" :label="$t('trials:adRules:title:answerGroupA')" min-width="100">
            <template slot-scope="scope">
              {{
                QuestionList[index].QuestionGenre === 3
                  ? scope.row.AnswerGroupA.map((v) =>
                    $fd(QuestionList[index].DictionaryCode, parseInt(v))
                  ).toString()
                  : scope.row.AnswerGroupA.toString()
              }}
            </template>
          </el-table-column>
          <!-- 阅片人B -->
          <el-table-column prop="AnswerGroupB" :label="$t('trials:adRules:title:answerGroupB')" min-width="100">
            <template slot-scope="scope">
              {{
                QuestionList[index].QuestionGenre === 3
                  ? scope.row.AnswerGroupB.map((v) =>
                    $fd(QuestionList[index].DictionaryCode, parseInt(v))
                  ).toString()
                  : scope.row.AnswerGroupB.toString()
              }}
            </template>
          </el-table-column>
          <el-table-column prop="AnswerGroupB" :label="$t('common:action:action')" min-width="80">
            <template slot-scope="scope">
              <!-- 编辑 -->
              <el-button icon="el-icon-edit" circle :title="$t('common:button:edit')" size="mini"
                :disabled="isSign" @click="addGroup(index, scope.$index)" />
              <!-- 删除 -->
              <el-button icon="el-icon-delete" circle :title="$t('common:button:delete')" size="mini"
                :disabled="isSign" @click="tagClose(index, scope.$index)" />
            </template>
          </el-table-column>
        </el-table>
        <div v-if="QuestionList[index].JudgeType === 2">
          <div style="
              background: #f6f6f6;
              border-radius: 20px;
              padding: 15px 20px;
              margin-top: 10px;
            ">
            <el-checkbox-group v-if="QuestionList[index].QuestionGenre === 3" v-model="QuestionList[index].grouping">
              <template v-for="item of $d[QuestionList[index].DictionaryCode]">
                <el-checkbox v-if="item.value !== -1" :key="item.id" :disabled="isSign" :label="item.value">{{
                  item.label }}</el-checkbox>
              </template>
            </el-checkbox-group>
            <el-checkbox-group v-else v-model="QuestionList[index].grouping">
              <el-checkbox v-for="item of item.TypeValue.split('|')" :key="item" :disabled="isSign"
                :label="item">{{
                item }}</el-checkbox>
            </el-checkbox-group>
            <div style="margin-top: 20px">
              <!-- 当前选择答案分组: -->
              {{ $t('trials:adRules:title:selectAnswerGroup') }}
              <el-tag v-if="
                QuestionList[index].grouping.length > 0 &&
                QuestionList[index].QuestionGenre !== 3
              ">{{
                  QuestionList[index].grouping.toString().replaceAll(',', '|')
                }}</el-tag>
              <el-tag v-if="
                QuestionList[index].grouping.length > 0 &&
                QuestionList[index].QuestionGenre === 3
              ">{{
                  QuestionList[index].grouping
                    .map(
                      (v) =>
                        $d[QuestionList[index].DictionaryCode].find(
                          (v1) => v1.value === v
                        ).label
                    )
                    .toString()
                    .replaceAll(',', '|')
                }}</el-tag>
              <el-button size="mini" type="primary" :disabled="isSign" @click="addGroup2(index)">
                <!-- 添加分组 -->
                {{ $t('trials:adRules:title:addGroup') }}
              </el-button>
            </div>
          </div>
          <div v-if="QuestionList[index].QuestionGenre !== 3" style="margin-top: 20px">
            {{ $t('trials:adRules:title:group') }}
            <el-tag v-for="itemA of QuestionList[index].AnswerGroup2List" :key="itemA" closable
              style="margin-right: 10px" @close="
                () => {
                  return tagClose2(index, indexA)
                }
              ">{{ itemA }}</el-tag>
          </div>
          <div v-if="QuestionList[index].QuestionGenre === 3" style="margin-top: 20px">
            <!-- 分组: -->
            {{ $t('trials:adRules:title:group') }}
            <el-tag v-for="itemA of QuestionList[index].AnswerGroup2List" :key="itemA" closable
              style="margin-right: 10px" @close="
                () => {
                  return tagClose2(index, indexA)
                }
              ">
              {{
                itemA
                  .split('|')
                  .map((v) =>
                    $fd(QuestionList[index].DictionaryCode, parseInt(v))
                  )
                  .join('|')
              }}
            </el-tag>
          </div>
        </div>
        <div v-if="
          QuestionList[index].JudgeType === 4 ||
          QuestionList[index].JudgeType === 5
        ">
          <el-form :inline="true" :model="QuestionList[index]" class="demo-form-inline" :ref="'JudgeDifferenceValue' + QuestionList[index].JudgeType + index
            " :rules="JudgeDifferenceValueQRules">
            <el-form-item :label="$t(
              `trials:trials-panel:setting:reading-unit:JudgeDifferenceType`
            )
              " prop="JudgeDifferenceType">
              <el-select v-model="QuestionList[index].JudgeDifferenceType" placeholder="请选择"
                :disabled="isSign">
                <el-option v-for="item of $d.JudgeDifferenceType" :key="item.id" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item :label="$t(
              `trials:trials-panel:setting:reading-unit:JudgeDifferenceValue${QuestionList[index].JudgeType}`
            )
              " prop="JudgeDifferenceValue">
              <div style="display: flex">
                <el-input v-model="QuestionList[index].JudgeDifferenceValue" clearable
                  :disabled="isSign"></el-input>
                <span style="margin-left: 10px" v-if="QuestionList[index].JudgeType !== 5 && QuestionList[index].Unit !== 0">
                  {{ `${QuestionList[index].Unit === 4 ? QuestionList[index].CustomUnit : $fd('ValueUnit', QuestionList[index].Unit)}`}}
                </span>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
    </el-collapse>
    <!-- 选择答案 -->
    <el-dialog v-if="QuestionVisible" :title="$t('trials:adRules:title:selectAnswer')" :visible.sync="QuestionVisible"
      width="800px" :close-on-click-modal="false" custom-class="base-dialog-wrapper" append-to-body>
      <div class="base-dialog-body">
        <!-- 阅片人A -->
        <el-form-item label-width="110px" :label="$t('trials:adRules:title:answerGroupA')">
          <el-checkbox-group v-model="QuestionList[selectIndex].groupingA">
            <template v-for="item of QuestionList[selectIndex].TypeValue.split('|') || []">
              <el-checkbox 
                v-if="QuestionList[selectIndex].QuestionGenre !== 3" 
                :key="item" 
                :label="item" 
                :disabled="QuestionList[selectIndex]?.groupingB?.includes(item) ?? false"
              >
              {{ item }}
            </el-checkbox>
            </template>
            <template v-for="item of $d[QuestionList[selectIndex].DictionaryCode]">
              <el-checkbox 
                v-if="QuestionList[selectIndex].QuestionGenre === 3 && item.value !== -1" 
                :key="item.id" 
                :label="item.value" 
                :disabled="QuestionList[selectIndex]?.groupingB?.includes(item.value) ?? false"
                >
                {{ item.label }}
              </el-checkbox>
            </template>
          </el-checkbox-group>
        </el-form-item>
        <!-- 阅片人B -->
        <el-form-item label-width="110px" :label="$t('trials:adRules:title:answerGroupB')">
          <el-checkbox-group v-model="QuestionList[selectIndex].groupingB">
            <template v-for="item of QuestionList[selectIndex].TypeValue.split('|') || []">
              <el-checkbox 
                v-if="QuestionList[selectIndex].QuestionGenre !== 3" 
                :key="item" 
                :label="item" 
                :disabled="QuestionList[selectIndex]?.groupingA?.includes(item) ?? false"
                >
                {{ item }}
              </el-checkbox>
            </template>
            <template v-for="item of $d[QuestionList[selectIndex].DictionaryCode]">
              <el-checkbox 
                v-if="QuestionList[selectIndex].QuestionGenre === 3 && item.value !== -1" 
                :key="item.id" 
                :label="item.value" 
                :disabled="QuestionList[selectIndex]?.groupingA?.includes(item.value) ?? false"
                >
                {{ item.label }}
              </el-checkbox>
            </template>
          </el-checkbox-group>
        </el-form-item>
      </div>
      <div class="base-dialog-footer" style="text-align: right; margin-top: 10px">
        <!-- 取消 -->
        <el-button v-hasPermi="['trials:trials-panel:setting:reading-unit:edit']" :disabled="btnLoading" size="small"
          type="primary" @click="
            QuestionVisible = false
            $set(QuestionList[selectIndex], 'groupingA', [])
            $set(QuestionList[selectIndex], 'groupingB', [])
            ">
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button v-hasPermi="['trials:trials-panel:setting:reading-unit:edit']" :disabled="btnLoading" size="small"
          type="primary" @click="save">
          {{ $t('common:button:save') }}
        </el-button>
      </div>
    </el-dialog>
  </el-form>
</template>
<script>
/* eslint-disable */
import {
  getTrialCriterionJudgeQuestionList,
  setTrialCriterionJudgeQuestionAnswerGroup,
} from '@/api/trials/reading'
import { setTrialJudgyInfo, getTrialJudgyInfo, setTrialJudgeRuleInfo } from '@/api/trials/setting'
import ArbitrationRulesByVisit from './ArbitrationRulesByVisit'
export default {
  components: {
    ArbitrationRulesByVisit,
  },
  props: {
    trialReadingCriterionId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      CriterionList: [],
      QuestionList: [],
      ReadingQuestionCriterionTrialId: null,
      btnLoading: false,
      loading: false,
      index: 0,
      QuestionVisible: false,
      activeNames: null,
      indexA: null,
      type: null,
      isSign: true,
      JudgyInfo: {},
      JudgeDifferenceValueQRules: {
        JudgeDifferenceType: [
          {
            required: true,
            message: this.$t(
              'trials:trials-list:setitng:JudgeDifferenceTypeRequired'
            ),
            trigger: 'blur',
          },
        ],
        JudgeDifferenceValue: [
          {
            required: true,
            message: this.$t(
              'trials:trials-list:setitng:JudgeDifferenceValueRequired'
            ),
            trigger: 'blur',
          },
          {
            pattern: /^[0-9]+(.[0-9]{2})?$/,
            message: this.$t('trials:trials-list:setitng:JudgeDifferenceValue'),
            trigger: 'blur',
          },
          {
            validator: (rule, value, callback) => {
              if (value <= 0) {
                callback(
                  new Error(
                    this.$t(
                      'trials:trials-list:setitng:JudgeDifferenceValueMin'
                    )
                  )
                )
              } else {
                callback()
              }
            },
            trigger: 'blur',
          },
        ],
      },
      judgeRuleEnum: -1,
      savedJudgyInfo: {
        JudgeModeEnum: null,
        JudgeRuleEnum: null,
      },
    }
  },
  watch: {
    async trialReadingCriterionId(v) {
      if (v === null) return
      await this.initData()
    },
  },
  async mounted() {
    await this.initData()
  },
  methods: {
    resetJudgeTypeRelatedFields(question = {}) {
      this.$set(question, 'AnswerGroup2List', [])
      this.$set(question, 'AnswerGroupList', [])
      this.$set(question, 'grouping', [])
      this.$set(question, 'groupingA', [])
      this.$set(question, 'groupingB', [])
      this.$set(question, 'JudgeDifferenceValue', 0)
      this.$set(question, 'JudgeDifferenceType', 0)
    },
    async initData() {
      await this.getTrialJudgyInfo()
      if (this.judgeRuleEnum !== 1) {
        await this.getList(false)
      }
    },
    // 修改仲裁规则
    changeArbitrationRule(value) {
      if (value !== 1) {
        this.QuestionList.forEach((item) => {
          item.JudgeDifferenceValue = 0
          item.JudgeDifferenceType = 0
        })
        if (this.$refs.visitRules) {
          this.$refs.visitRules.changeArbitrationRule(value)
        }
      }
    },
    JudgeTypeChange(value, index) {
      this.resetJudgeTypeRelatedFields(this.QuestionList[index])
      this.$nextTick(() => {
        if (value === 4 || value === 5) {
          if (this.$refs['JudgeDifferenceValue' + value + index] && this.$refs['JudgeDifferenceValue' + value + index][0]) {
            this.$refs['JudgeDifferenceValue' + value + index][0].resetFields()
            // this.QuestionList[index].JudgeDifferenceValue = 0;
          }
        }
      })
    },
    saveAllSync() {
      if (this.judgeRuleEnum === 1 && this.$refs.visitRules) {
        return Promise.resolve(true)
        // return this.$refs.visitRules.saveAllSync()
      }
      return new Promise((resolve) => {
        var arr = []
        this.QuestionList.forEach((v, i) => {
          arr.push(this.applySync(v.ReadingQuestionTrialId, i))
        })
        Promise.all(arr)
          .then(() => {
            resolve(true)
          })
          .catch(() => {
            resolve(false)
          })
      })
    },
    applySync(ReadingQuestionTrialId, index) {
      return new Promise(async (resolve, reject) => {
        if (this.QuestionList[index].JudgeType === 0) {
          reject(false)
          return
        }
        if (
          this.QuestionList[index].JudgeType === 2 &&
          this.QuestionList[index].AnswerGroup2List.length === 0
        ) {
          reject(false)
          return
        }
        if (
          this.QuestionList[index].JudgeType === 3 &&
          this.QuestionList[index].AnswerGroupList.length === 0
        ) {
          reject(false)
          return
        }
        if (
          this.QuestionList[index].JudgeType === 4 ||
          this.QuestionList[index].JudgeType === 5
        ) {
          try {
            const validate = await this.$refs[
              'JudgeDifferenceValue' +
              this.QuestionList[index].JudgeType +
              index
            ][0].validate()
            if (!validate) return reject(false)
          } catch (err) {
            return reject(false)
          }
        }
        this.btnLoading = true
        this.loading = true
        setTrialCriterionJudgeQuestionAnswerGroup({
          ReadingQuestionTrialId: ReadingQuestionTrialId,
          AnswerGroup: this.QuestionList[index].AnswerGroup2List,
          AnswerCombination: this.QuestionList[index].AnswerGroupList,
          JudgeType: this.QuestionList[index].JudgeType,
          JudgeDifferenceValue: Number(
            this.QuestionList[index].JudgeDifferenceValue
          ),
          JudgeDifferenceType: Number(
            this.QuestionList[index].JudgeDifferenceType
          ),
        })
          .then((res) => {
            resolve()
            this.btnLoading = false
            this.loading = false
          })
          .catch(() => {
            reject(false)
            this.btnLoading = false
            this.loading = false
          })
      })
    },
    setTrialJudgyInfo() {
      this.loading = true
      setTrialJudgyInfo({
        TrialId: this.$route.query.trialId,
        ArbitrationRule: this.JudgyInfo.ArbitrationRule,
        TrialReadingCriterionId: this.trialReadingCriterionId,
      })
        .then((res) => {
          this.loading = false
          this.$message.success(this.$t('common:message:savedSuccessfully')) // '保存成功'
        })
        .catch(() => {
          this.loading = false
        })
    },
    async getTrialJudgyInfo() {
      try {
        this.loading = true
        const res = await getTrialJudgyInfo({
          TrialId: this.$route.query.trialId,
          TrialReadingCriterionId: this.trialReadingCriterionId,
        })
        this.JudgyInfo = res.Result
        this.judgeRuleEnum = this.JudgyInfo.JudgeRuleEnum
        this.isSign = res.Result.IsSigned
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    },
    async saveJugeRule(showMessage = true) {
      try {
        this.loading = true
        const prevJudgeRuleEnum = this.judgeRuleEnum
        const params = {
          trialReadingCriterionId: this.trialReadingCriterionId,
          judgeModeEnum: this.JudgyInfo.JudgeModeEnum,
          judgeRuleEnum: this.JudgyInfo.JudgeRuleEnum,
        }
        const res = await setTrialJudgeRuleInfo(params)
        if (res.IsSuccess) {
          if (showMessage) {
            this.$message.success(this.$t('common:message:savedSuccessfully'))
          }
          this.judgeRuleEnum = this.JudgyInfo.JudgeRuleEnum
          if (prevJudgeRuleEnum !== this.judgeRuleEnum) {
            if (this.judgeRuleEnum === 1) {
              await this.$nextTick()
              if (this.$refs.visitRules) {
                await this.$refs.visitRules.initData()
              }
            } else {
              await this.getList(false)
            }
          }
        }
        return true
      } catch (e) {
        console.log(e)
        return false
      } finally {
        this.loading = false
      }
    },
    tagClose2(index, indexA) {
      // '删除该规则组？'
      this.$confirm(this.$t('trials:adRules:message:msg1')).then(() => {
        this.QuestionList[index].AnswerGroup2List.splice(indexA, 1)
      })
    },
    tagClose(index, indexA) {
      // '删除该规则组？'
      this.$confirm(this.$t('trials:adRules:message:msg1')).then(() => {
        this.QuestionList[index].AnswerGroupList.splice(indexA, 1)
      })
    },
    save() {
      var index = this.selectIndex
      var indexA = this.indexA
      if (this.QuestionList[index].groupingA.length === 0) {
        this.$alert(this.$t('trials:adRules:message:msg2')) // '请先选择答案A'
        return
      }
      if (this.QuestionList[index].groupingB.length === 0) {
        this.$alert(this.$t('trials:adRules:message:msg3')) // '请先选择答案B'
        return
      }
      if (this.type === 'add') {
        this.QuestionList[index].AnswerGroupList.push({
          AnswerGroupA: this.QuestionList[index].groupingA,
          AnswerGroupB: this.QuestionList[index].groupingB,
        })
      } else {
        this.$set(
          this.QuestionList[index].AnswerGroupList[indexA],
          'AnswerGroupA',
          this.QuestionList[index].groupingA
        )
        this.$set(
          this.QuestionList[index].AnswerGroupList[indexA],
          'AnswerGroupB',
          this.QuestionList[index].groupingB
        )
      }
      this.$set(this.QuestionList[index], 'groupingA', [])
      this.$set(this.QuestionList[index], 'groupingB', [])
      this.$message.success(this.$t('trials:adRules:message:msg4')) // '成功新增答案规则'
      this.QuestionVisible = false
    },
    addGroup2(index) {
      if (this.QuestionList[index].grouping.length === 0) {
        this.$alert(this.$t('trials:adRules:message:msg5')) // '请先选择答案,再添加分组'
        return
      }
      var grouping = this.QuestionList[index].grouping
        .toString()
        .replaceAll(',', '|')
      this.QuestionList[index].AnswerGroup2List.push(`|${grouping}|`)
      this.$set(this.QuestionList[index], 'grouping', [])
    },
    addGroup(index, indexA) {
      this.selectIndex = index
      this.type = 'add'
      if (indexA !== null) {
        this.indexA = indexA
        this.type = 'edit'
        this.$set(
          this.QuestionList[index],
          'groupingA',
          this.QuestionList[index].AnswerGroupList[indexA].AnswerGroupA
        )
        this.$set(
          this.QuestionList[index],
          'groupingB',
          this.QuestionList[index].AnswerGroupList[indexA].AnswerGroupB
        )
      }
      this.QuestionVisible = true
    },
    reset(ReadingQuestionTrialId, index) {
      // '确定重置当前设置的裁判任务生成规则吗？'
      this.$confirm(this.$t('trials:adRules:message:msg6')).then(() => {
        this.btnLoading = true
        this.loading = true
        setTrialCriterionJudgeQuestionAnswerGroup({
          ReadingQuestionTrialId: ReadingQuestionTrialId,
          AnswerGroup: [],
          AnswerCombination: [],
          JudgeType: 0,
          JudgeDifferenceValue: 0,
          JudgeDifferenceType: 0
        })
          .then((res) => {
            this.$set(this.QuestionList[index], 'AnswerGroup2List', [])
            this.$set(this.QuestionList[index], 'AnswerGroupList', [])
            this.$set(this.QuestionList[index], 'JudgeType', 0)
            this.$set(this.QuestionList[index], 'JudgeDifferenceValue', 0)
            this.$set(this.QuestionList[index], 'JudgeDifferenceType', 0)
            this.$message.success(this.$t('trials:adRules:message:msg7')) // '重置成功'
            this.btnLoading = false
            this.loading = false
          })
          .catch(() => {
            this.btnLoading = false
            this.loading = false
          })
      })
    },
    async apply(ReadingQuestionTrialId, index) {
      if (this.QuestionList[index].JudgeType === 0) {
        this.$alert(this.$t('trials:adRules:message:msg8')) // '请先配置规则才能应用'
        return
      }
      if (
        this.QuestionList[index].JudgeType === 2 &&
        this.QuestionList[index].AnswerGroup2List.length === 0
      ) {
        this.$alert(this.$t('trials:adRules:message:msg8')) // '请先配置规则才能应用'
        return
      }
      if (
        this.QuestionList[index].JudgeType === 3 &&
        this.QuestionList[index].AnswerGroupList.length === 0
      ) {
        this.$alert(this.$t('trials:adRules:message:msg8'))
        return
      }
      if (
        this.QuestionList[index].JudgeType === 4 ||
        this.QuestionList[index].JudgeType === 5
      ) {
        try {
          const validate = await this.$refs[
            'JudgeDifferenceValue' + this.QuestionList[index].JudgeType + index
          ][0].validate()
          if (!validate) return
        } catch (err) {
          return
        }
      }
      this.btnLoading = true
      this.loading = true
      setTrialCriterionJudgeQuestionAnswerGroup({
        ReadingQuestionTrialId: ReadingQuestionTrialId,
        AnswerGroup: this.QuestionList[index].AnswerGroup2List,
        AnswerCombination: this.QuestionList[index].AnswerGroupList,
        JudgeType: this.QuestionList[index].JudgeType,
        JudgeDifferenceValue: Number(
          this.QuestionList[index].JudgeDifferenceValue
        ),
        JudgeDifferenceType: Number(
          this.QuestionList[index].JudgeDifferenceType
        )
      })
        .then((res) => {
          this.$message.success(this.$t('trials:adRules:message:msg9')) // '应用成功'
          this.btnLoading = false
          this.loading = false
        })
        .catch(() => {
          this.btnLoading = false
          this.loading = false
        })
    },
    tabClick(v) {
      this.ReadingQuestionCriterionTrialId =
        this.CriterionList[this.index].ReadingQuestionCriterionTrialId
    },
    async getList(needGetTrialJudgyInfo = true) {
      this.loading = true
      if (needGetTrialJudgyInfo) {
        await this.getTrialJudgyInfo()
      }
      if (this.judgeRuleEnum === 1 && this.$refs.visitRules) {
        return this.$refs.visitRules.getList()
      }
      return getTrialCriterionJudgeQuestionList({
        TrialId: this.$route.query.trialId,
        TrialReadingCriterionId: this.trialReadingCriterionId,
      })
        .then((res) => {
          this.loading = false
          this.QuestionList = res.Result
          this.activeNames = this.QuestionList.map(
            (item) => item.ReadingQuestionTrialId
          )
          this.QuestionList.forEach((v) => {
            this.$set(v, 'grouping', [])
            this.$set(v, 'groupingA', [])
            this.$set(v, 'groupingB', [])
            this.$set(
              v,
              'AnswerGroupList',
              Object.assign([], v.AnswerCombination)
            )
            this.$set(v, 'AnswerGroup2List', Object.assign([], v.AnswerGroup))
          })
        })
        .catch(() => {
          this.btnLoading = false
          this.loading = false
        })
    },
  },
}
</script>
