<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="questionId" label="考区">
        <Select
          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 v-model.trim="modalForm.successCount" readonly></Input>
      </FormItem>
      <FormItem label="待评数量">
        <Input v-model.trim="modalForm.waitCount" readonly></Input>
      </FormItem>
      <FormItem label="整体进度">
        <Input v-model.trim="modalForm.progress" 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 { 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.$Message.success("发布任务成功！");
      this.$emit("modified");
      this.cancel();
    }
  }
};
</script>
