<template>
  <el-dialog
    class="exam-batch-dialog"
    :visible.sync="dialogVisible"
    width="600px"
    title="请选择考试批次"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    append-to-body
  >
    <el-radio-group size="medium" v-model="examBatchId">
      <el-radio v-for="item in examBatchs" :key="item.id" :label="item.id">{{
        item.label
      }}</el-radio>
    </el-radio-group>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel" plain>取消</el-button>
      <el-button type="primary" @click="confirm">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { examMonitorBatchList } from "@/api/invigilation";
import { dateFormatForAPI } from "@/utils/utils";
import { mapState, mapMutations } from "vuex";

export default {
  name: "exam-batch-dialog",
  props: {
    initExamid: String,
  },
  data() {
    return {
      dialogVisible: false,
      examBatchId: null,
      examBatchs: [],
      userId: this.$store.state.user.id,
    };
  },
  computed: {
    ...mapState("invigilation", ["selectedExamId"]),
  },
  mounted() {
    this.getExamList();
  },
  methods: {
    ...mapMutations("invigilation", ["setSelectedExamId"]),
    async getExamList() {
      const res = await examMonitorBatchList({
        type: "monitor",
        userId: this.userId,
        pageNumber: 1,
        pageSize: 100,
      });
      const dnow = Date.now();
      this.examBatchs = res.data.data.records.map((item) => {
        return {
          ...item,
          label: `${item.name}【${dateFormatForAPI(
            item.startTime
          )} - ${dateFormatForAPI(item.endTime)}】`,
          isExaming: dnow > item.startTime && dnow < item.endTime,
        };
      });
      // 选择优先级：initExamid > selectedExamId > 第一个正在考试的考试 > 列表第一个考试
      let selectedExam = null;
      if (this.initExamid) {
        selectedExam = this.examBatchs.find(
          (item) => item.id === this.initExamid
        );
      } else if (this.selectedExamId) {
        selectedExam = this.examBatchs.find(
          (item) => item.id === this.selectedExamId
        );
      } else {
        // 优先取当前第一个正在考试的考试
        selectedExam = this.examBatchs.find((item) => item.isExaming);
      }
      // 默认记录没有时，取记录第一条数据
      selectedExam = selectedExam || this.examBatchs[0];

      this.examBatchId = selectedExam && selectedExam.id;
      this.setSelectedExamId(this.examBatchId);

      this.$emit("confirm", selectedExam);
    },
    cancel() {
      this.dialogVisible = false;
    },
    open() {
      this.dialogVisible = true;
    },
    confirm() {
      if (!this.examBatchId) {
        this.$message.error("请选择考试批次！");
        return;
      }
      this.setSelectedExamId(this.examBatchId);
      const exam = this.examBatchs.find((item) => item.id === this.examBatchId);
      const dnow = Date.now();
      exam.isExaming = dnow > exam.startTime && dnow < exam.endTime;
      this.$emit("confirm", exam);
      this.cancel();
    },
  },
};
</script>
