<template>
  <Modal
    class="modify-formal-grading-task"
    v-model="modalIsShow"
    title="创建正评任务"
    :mask-closable="false"
    @on-visible-change="visibleChange"
  >
    <Form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      :label-width="120"
    >
      <FormItem prop="areaCode" label="考区">
        <Select v-model="modalForm.areaCode" placeholder="请选择考区">
          <Option value=""></Option>
        </Select>
      </FormItem>
      <FormItem label="已评数量">
        <Input v-model.trim="modalForm.gradedCount" readonly></Input>
      </FormItem>
      <FormItem label="待评数量">
        <Input v-model.trim="modalForm.waitCount" readonly></Input>
      </FormItem>
      <FormItem label="整体进度">
        <Input v-model.trim="modalForm.gradedProgress" readonly></Input>
      </FormItem>
      <FormItem prop="taskCount" label="分配任务数量">
        <InputNumber
          :min="1"
          :max="modalForm.waitCount"
          :precision="0"
          v-model.trim="modalForm.taskCount"
          style="width: 120px"
          clearable
        ></InputNumber>
      </FormItem>
    </Form>
    <div slot="footer">
      <Button type="text" @click="cancel">取消</Button>
      <Button type="primary" :disabled="isSubmit" @click="submit"
        >发布评卷任务</Button
      >
    </div>
  </Modal>
</template>

<script>
import { createTryGradingTask } from "@/api";
// import { password } from "@/plugins/formRules";

const initModalForm = {
  areaCode: "",
  taskCount: null
};

export default {
  name: "modify-formal-grading-task",
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      rules: {}
    };
  },
  methods: {
    visibleChange(visible) {
      if (visible) {
        this.modalForm = { ...initModalForm };
      }
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate();
      if (!valid) return;

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

      if (!data) return;

      this.isSubmit = false;
      this.$Message.success("发布任务成功！");
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
