<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="场次代码">
          <ActivitySelect
            :examId="form.examId"
            v-model="form.examActivityId"
            styles="width: 100%"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="考场名称">
          <ExamRoomSelect
            :examId="form.examId"
            v-model="form.roomCode"
            styles="width: 100%"
          />
        </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="姓名">
          <el-input v-model.trim="form.name"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="证件号">
          <el-input :disabled="isEdit" v-model.trim="form.identity"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="年级">
          <el-input v-model.trim="form.grade"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="教学班级">
          <el-input v-model.trim="form.classNo"></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: "",
        courseCode: "",
        name: "",
        identity: "",
        grade: "",
        classNo: "",
      },
      rules: {
        courseCode: { required: true, message: "必填" },
      },
      loading: false,
    };
  },
  watch: {
    examStudent(val) {
      let tmp = { ...val };
      if (!tmp.id) {
        tmp = {
          id: "",
          examId: "",
          examActivityId: "",
          roomCode: "",
          courseCode: "",
          name: "",
          identity: "",
          grade: "",
          classNo: "",
        };
      }
      this.form = tmp;
    },
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    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>
