<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="考场">
          <ExamRoomSelect
            :examId="user.examId"
            v-model="form.roomCode"
            style="width: 100%;"
          ></ExamRoomSelect>
        </el-form-item>
        <el-form-item label="监考老师">
          <InvigilatorSelect
            v-model="form.userIds"
            style="width: 100%;"
            multiple
          />
        </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 { saveInvigilator } from "@/api/examwork-invigilate";
export default {
  name: "InvigilateManagementDialog",
  props: {
    user: Object,
  },
  watch: {
    user: {
      immediate: true,
      handler(val) {
        this.form.roomCode = val.roomCode;
        const u = val.userId || "";
        this.form.userIds = u.split(",").filter((v) => v);
      },
    },
  },
  data() {
    return {
      visible: false,
      form: {
        roomCode: "",
        userIds: [],
      },
      rules: {},
      loading: false,
    };
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    async submitForm() {
      let data = this.form;
      try {
        this.loading = true;
        await saveInvigilator({ examId: this.user.examId, ...data });
        this.$emit("reload");
        this.$notify({ title: "保存成功", type: "success" });
        this.closeDialog();
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style></style>
