<template>
  <div class="mark-rule-set part-box">
    <Form ref="modalFormComp" :model="modalForm" :label-width="180">
      <FormItem label="分数处理方式：">
        <Select v-model="modalForm.scoreRule" style="width: 200px">
          <Option value="">12</Option>
        </Select>
      </FormItem>
      <FormItem label="改档及改档打分：">
        <RadioGroup v-model="modalForm.changeGradeMarkType">
          <Radio
            v-for="(val, key) in BOOLEAN_TYPE"
            :key="key"
            :label="key"
            style="margin-right: 50px;"
            >{{ val }}</Radio
          >
        </RadioGroup>
      </FormItem>
      <FormItem 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 { markRuleDetail, saveMarkRule } from "@/api";
import { BOOLEAN_TYPE } from "@/constants/enumerate";

export default {
  name: "mark-rule-set",
  data() {
    return {
      isSubmit: false,
      workId: this.$route.params.workId,
      BOOLEAN_TYPE,
      initModalForm: {
        scoreRule: "",
        changeGradeMarkType: 0,
        isShowAllPaper: 0
      },
      modalForm: {}
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const data = await markRuleDetail(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 saveMarkRule(this.modalForm).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

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