<template>
  <el-dialog
    ref="dialog"
    title="复制考试批次"
    width="450px"
    :visible.sync="visible"
    @close="closeDialog"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-position="right"
      label-width="120px"
    >
      <el-row>
        <el-form-item label="批次名称" prop="name">
          <el-input
            class="pull_length"
            v-model="form.name"
            placeholder="批次名称"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="批次编码" prop="code">
          <el-input
            class="pull_length"
            v-model="form.code"
            placeholder="批次编码"
            disabled
          />
        </el-form-item>
      </el-row>
      <el-row class="d-flex justify-content-center">
        <el-button type="primary" @click="submitForm" :loading="loading"
          >保 存</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { copyExam } from "@/api/examwork-exam";
export default {
  name: "CopyExamDialog",
  props: {
    exam: Object,
  },
  data() {
    return {
      visible: false,
      form: {
        name: "",
        code: "",
      },
      rules: {},
      loading: false,
    };
  },
  watch: {
    "form.name"(val) {
      this.form.code = val;
    },
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    async submitForm() {
      try {
        this.loading = true;
        await copyExam({
          sourceId: this.exam.id,
          code: this.form.code,
          name: this.form.name,
        });
        this.$emit("reload");
        this.$notify({ title: "复制成功", type: "success" });
        this.closeDialog();
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style></style>
