<template>
  <el-dialog
    ref="dialog"
    :title="(isEdit ? '编辑' : '新增') + '考生'"
    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="examId">
          <ExamSelect v-model="form.examId" styles="width: 100%" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="场次代码" prop="examActivityId">
          <ActivitySelect
            :examId="form.examId"
            v-model="form.examActivityId"
            styles="width: 100%"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="考场名称" prop="roomCode">
          <ExamRoomSelect
            :examId="form.examId"
            v-model="form.roomCode"
            styles="width: 100%"
            @change="roomCodeChange"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="科目" prop="courseCode">
          <CourseSelect
            :examId="form.examId"
            v-model="form.courseCode"
            styles="width: 100%"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="姓名" prop="name">
          <el-input v-model.trim="form.name" maxlength="30"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="证件号" prop="identity">
          <el-input
            :disabled="isEdit"
            v-model.trim="form.identity"
            maxlength="30"
          ></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="年级">
          <el-input v-model.trim="form.grade" maxlength="30"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="教学班级">
          <el-input v-model.trim="form.classNo" maxlength="30"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="状态" prop="enable">
          <el-radio-group class="pull_right_sm" v-model="form.enable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </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 { saveExamStudent } from "@/api/examwork-examstudent";
export default {
  name: "ExamStudentManagementDialog",
  props: {
    examId: String,
    examStudent: Object,
  },
  computed: {
    isEdit() {
      return !!this.examStudent.id;
    },
  },
  data() {
    return {
      visible: false,
      form: {
        id: "",
        examId: "",
        examActivityId: "",
        roomCode: "",
        roomName: "",
        courseCode: "",
        name: "",
        identity: "",
        grade: "",
        classNo: "",
      },
      rules: {
        examId: { required: true, message: "必填" },
        examActivityId: { required: true, message: "必填" },
        roomCode: { required: true, message: "必填" },
        courseCode: { required: true, message: "必填" },
        name: { required: true, message: "必填" },
        identity: { required: true, message: "必填" },
      },
      loading: false,
    };
  },
  watch: {
    examStudent(val) {
      let tmp = { ...val };
      if (!tmp.id) {
        tmp = {
          id: "",
          examId: "",
          examActivityId: "",
          roomCode: "",
          roomName: "",
          courseCode: "",
          name: "",
          identity: "",
          grade: "",
          classNo: "",
        };
      }
      this.form = tmp;
    },
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    roomCodeChange(val) {
      this.form.roomName = val ? val.roomName : "";
    },
    async submitForm() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
      } catch (error) {
        console.log("校验失败", error);
        return;
      }
      let data = this.form;
      if (this.isEdit) {
        data = { ...data, id: this.examStudent.id };
      }
      try {
        this.loading = true;
        await saveExamStudent(data);
        this.$emit("reload");
        this.$notify({ title: "保存成功", type: "success" });
        this.closeDialog();
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style></style>
