<template>
  <div class="grading-rule-set part-box">
    <Form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :label-width="200"
      style="width: 400px;"
    >
      <FormItem prop="deviation" label="仲裁档位差：">
        <InputNumber
          v-model="modalForm.deviation"
          :min="1"
          :max="100"
          :disabled="!modalFormCanEdit"
          style="width: 160px;"
        ></InputNumber>
      </FormItem>
      <FormItem
        prop="cumulativeError"
        label="打回累计误差："
        v-if="modalForm.autoCallback"
      >
        <InputNumber
          v-model="modalForm.cumulativeError"
          :min="1"
          :max="100"
          :disabled="!modalFormCanEdit"
          style="width: 160px;"
        ></InputNumber>
      </FormItem>
      <FormItem prop="examNumber" label="系统自动打回：">
        <Select
          v-model="modalForm.autoCallback"
          :disabled="!modalFormCanEdit"
          placeholder="系统自动打回"
        >
          <Option
            v-for="(val, key) in BOOLEAN_TYPE"
            :key="key"
            :value="key * 1"
            :label="val"
          ></Option>
        </Select>
      </FormItem>
      <FormItem prop="examNumber" label="是否过半定档：">
        <Select
          v-model="modalForm.majority"
          :disabled="!modalFormCanEdit"
          placeholder="是否过半定档"
        >
          <Option
            v-for="(val, key) in BOOLEAN_TYPE"
            :key="key"
            :value="key * 1"
            :label="val"
          ></Option>
        </Select>
      </FormItem>
      <FormItem prop="examNumber" label="阅卷员是否显示所有试卷：">
        <Select
          v-model="modalForm.levelShowAllPaper"
          :disabled="!modalFormCanEdit"
          placeholder="阅卷员是否显示所有试卷"
        >
          <Option
            v-for="(val, key) in BOOLEAN_TYPE"
            :key="key"
            :value="key * 1"
            :label="val"
          ></Option>
        </Select>
      </FormItem>
      <FormItem>
        <Button
          shape="circle"
          style="width: 80px;"
          @click="modalFormCanEdit = true"
          >编辑</Button
        >
        <Button
          type="primary"
          shape="circle"
          style="width: 80px;"
          :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
      },
      modalFormCanEdit: false,
      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.modalFormCanEdit = false;
      this.$Message.success("保存成功！");
    }
  }
};
</script>
