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

<script>
import { gradingRuleDetail, saveGradingRule } from "@/api";
import { BOOLEAN_TYPE } from "@/constants/enumerate";

export default {
  name: "grading-rule-set",
  data() {
    return {
      isSubmit: false,
      workId: this.$route.params.workId,
      BOOLEAN_TYPE,
      initModalForm: {
        arbitrateGradeIntervel: null,
        refuseTotalCount: null,
        autoRefuse: 0,
        isOverHalfConfirm: 0,
        isShowAllPaper: 0
      },
      modalForm: {},
      rules: {}
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const data = await gradingRuleDetail(this.workId);
      this.modalForm = Object.assign(data, this.initModalForm);
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate();
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await saveGradingRule(this.modalForm).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

      this.isSubmit = false;
      this.$Message.success(this.title + "成功！");
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
