<template>
  <el-dialog
    :visible.sync="modalIsShow"
    title="设置评卷数"
    top="10vh"
    width="448px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <el-form ref="modalFormComp" :model="modalForm" :rules="rules">
      <el-form-item prop="topCount" label="任务数：">
        <el-input-number
          style="width: 125px"
          v-model="modalForm.topCount"
          :min="1"
          :max="9999999"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :loading="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { markMarkerSetTaskCount } from "../../api";

export default {
  name: "modify-marker-task-count",
  props: {
    ids: {
      type: Array,
      default() {
        return [];
      },
    },
    topCount: {
      type: Number,
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      rules: {
        topCount: [
          {
            required: true,
            message: "请输入任务数量",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm.topCount = this.topCount || undefined;
      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;

      const datas = { ...this.modalForm, markUserGroupIds: this.ids };
      const res = await markMarkerSetTaskCount(datas).catch(() => {});
      this.isSubmit = false;
      if (!res) return;
      this.$message.success("设置成功！");
      this.cancel();
      this.$emit("modified");
    },
  },
};
</script>
