<template>
  <div class="grading-rule-set part-box">
    <Form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :label-width="200"
    >
      <FormItem prop="deviation" label="仲裁档位差：">
        <InputNumber
          v-model="modalForm.deviation"
          :min="1"
          :max="100"
          style="width: 160px;"
        ></InputNumber>
      </FormItem>
      <FormItem prop="cumulativeError" label="打回累计误差：">
        <InputNumber
          v-model="modalForm.cumulativeError"
          :min="1"
          :max="100"
          style="width: 160px;"
        ></InputNumber>
      </FormItem>
      <FormItem prop="examNumber" label="系统自动打回：">
        <RadioGroup v-model="modalForm.autoCallback">
          <Radio
            v-for="(val, key) in BOOLEAN_TYPE"
            :key="key"
            :label="key * 1"
            style="margin-right: 50px;"
            >{{ val }}</Radio
          >
        </RadioGroup>
      </FormItem>
      <FormItem prop="examNumber" label="是否过半定档：">
        <RadioGroup v-model="modalForm.majority">
          <Radio
            v-for="(val, key) in BOOLEAN_TYPE"
            :key="key"
            :label="key * 1"
            style="margin-right: 50px;"
            >{{ val }}</Radio
          >
        </RadioGroup>
      </FormItem>
      <FormItem prop="examNumber" label="阅卷员是否显示所有试卷：">
        <RadioGroup v-model="modalForm.levelShowAllPaper">
          <Radio
            v-for="(val, key) in BOOLEAN_TYPE"
            :key="key"
            :label="key * 1"
            style="margin-right: 50px;"
            >{{ val }}</Radio
          >
        </RadioGroup>
      </FormItem>
      <FormItem>
        <Button type="primary" :disabled="isSubmit" @click="submit"
          >保存</Button
        >
      </FormItem>
    </Form>
  </div>
</template>

<script>
import { getParamsSet, updateLevelParams } from "@/api";
import { BOOLEAN_TYPE } from "@/constants/enumerate";
import { numberValidator } from "@/plugins/formRules";

export default {
  name: "grading-rule-set",
  data() {
    return {
      isSubmit: false,
      workId: this.$route.params.workId,
      BOOLEAN_TYPE,
      initModalForm: {
        workId: "",
        deviation: 3,
        cumulativeError: null,
        autoCallback: 0,
        majority: 0,
        levelShowAllPaper: 0
      },
      modalForm: {},
      rules: {
        deviation: numberValidator("请输入打回累计误差"),
        cumulativeError: numberValidator("请输入仲裁档位差")
      }
    };
  },
  mounted() {
    this.modalForm = { ...this.initModalForm };
    this.getParamsSetInfo();
  },
  methods: {
    async getParamsSetInfo() {
      const data = await getParamsSet(this.workId);
      this.modalForm = this.$objAssign(this.modalForm, data);
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate();
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      let result = true;
      await updateLevelParams(this.modalForm).catch(() => {
        result = false;
      });
      this.isSubmit = false;

      if (!result) return;
      this.$Message.success("保存成功！");
    }
  }
};
</script>
