<template>
  <Modal
    class="modify-formal-grading-task"
    v-model="modalIsShow"
    title="创建正评任务"
    :mask-closable="false"
    @on-visible-change="visibleChange"
  >
    <Form
      ref="modalFormComp"
      class="modal-form"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      :label-width="120"
    >
      <FormItem prop="questionId" label="考区">
        <Select
          size="large"
          v-model="modalForm.questionId"
          @on-change="areaChange"
          placeholder="请选择考区"
        >
          <Option
            v-for="(item, index) in areas"
            :key="index"
            :value="item.questionId"
            :label="item.areaName"
          ></Option>
        </Select>
      </FormItem>
      <FormItem label="已评数量">
        <Input
          size="large"
          v-model.trim="modalForm.successCount"
          readonly
        ></Input>
      </FormItem>
      <FormItem size="large" label="待评数量">
        <Input v-model.trim="modalForm.waitCount" readonly></Input>
      </FormItem>
      <FormItem size="large" label="整体进度">
        <Input v-model.trim="modalForm.progress" readonly></Input>
      </FormItem>
      <FormItem prop="taskCount" label="分配任务数量">
        <InputNumber
          size="large"
          :min="1"
          :max="modalForm.waitCount"
          :precision="0"
          v-model.trim="modalForm.taskCount"
          style="width: 120px"
          clearable
        ></InputNumber>
      </FormItem>
    </Form>
    <div slot="footer">
      <Button shape="circle" type="primary" :disabled="isSubmit" @click="submit"
        >发布</Button
      >
      <Button shape="circle" @click="cancel">取消</Button>
    </div>
  </Modal>
</template>

<script>
import { checkMissionStatus, createGradingTask, areaStatProgress } from "@/api";
import { numberValidator } from "@/plugins/formRules";

const initModalForm = {
  questionId: null,
  totalCount: 0,
  successCount: 0,
  waitCount: 0,
  progress: 0,
  taskCount: 1
};

export default {
  name: "modify-formal-grading-task",
  props: {
    subjectId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: { ...initModalForm },
      areas: [],
      rules: {
        taskCount: numberValidator("请输入分配任务数量")
      }
    };
  },
  methods: {
    visibleChange(visible) {
      if (visible) {
        this.getArea();
      }
    },
    async getArea() {
      const data = await areaStatProgress(this.subjectId);
      this.areas = data;
      this.modalForm.questionId = data[data.length - 1].questionId;
      this.areaChange();
    },
    areaChange() {
      const curArea = this.areas.find(
        item => item.questionId === this.modalForm.questionId
      );
      this.modalForm = Object.assign({}, initModalForm, curArea);
      this.modalForm.progress += "%";
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate();
      if (!valid) return;

      if (this.isSubmit) return;

      const ids = this.subjectId.split("-");
      await checkMissionStatus({ workId: ids[0], subject: ids[1] });

      this.isSubmit = true;
      let result = true;
      await createGradingTask({
        subjectId: this.subjectId,
        taskCount: this.modalForm.taskCount,
        questionId: this.modalForm.questionId
      }).catch(() => {
        result = false;
      });

      this.isSubmit = false;
      if (!result) return;

      this.$Modal.success({ content: "评卷任务创建成功" });
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
