<template>
  <div class="mark-rule-set part-box">
    <Form
      ref="modalFormComp"
      :model="modalForm"
      :label-width="180"
      style="width: 400px"
    >
      <FormItem label="分数处理方式：">
        <Select v-model="modalForm.roundUp" :disabled="!modalFormCanEdit">
          <Option
            v-for="(val, key) in SCORE_HANDLE_TYPE"
            :key="key"
            :value="key * 1"
            >{{ val }}</Option
          >
        </Select>
      </FormItem>
      <FormItem label="是否显示改档及改档打分：">
        <Select
          v-model="modalForm.changeStage"
          :disabled="!modalFormCanEdit"
          placeholder="是否显示改档及改档打分"
        >
          <Option
            v-for="(val, key) in BOOLEAN_TYPE"
            :key="key"
            :value="key * 1"
            :label="val"
          ></Option>
        </Select>
      </FormItem>
      <FormItem label="阅卷员是否显示所有试卷：">
        <Select
          v-model="modalForm.scoreShowAllPaper"
          :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, updateScoreParams } from "@/api";
import { BOOLEAN_TYPE, SCORE_HANDLE_TYPE } from "@/constants/enumerate";

export default {
  name: "mark-rule-set",
  data() {
    return {
      isSubmit: false,
      workId: this.$route.params.workId,
      BOOLEAN_TYPE,
      SCORE_HANDLE_TYPE,
      initModalForm: {
        workId: "",
        roundUp: 0,
        changeStage: 0,
        scoreShowAllPaper: 0
      },
      modalFormCanEdit: false,
      modalForm: {}
    };
  },
  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() {
      if (this.isSubmit) return;
      this.isSubmit = true;
      let result = true;
      await updateScoreParams(this.modalForm).catch(() => {
        result = false;
      });
      this.isSubmit = false;

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