<template>
  <div class="student-exam-detail">
    <div class="part-box-head">
      <div class="part-box-head-left"><h1>考试明细</h1></div>
    </div>
    <div class="part-filter">
      <div class="part-filter-form">
        <el-form ref="form" :model="form" :rules="rules" inline>
          <el-form-item label="批次名称" prop="examId">
            <ExamSelect v-model="form.examId" />
          </el-form-item>
          <el-form-item label="场次代码">
            <ActivitySelect
              :examId="form.examId"
              v-model="form.activityId"
              exam-required
            />
          </el-form-item>
          <el-form-item label="考场名称">
            <ExamRoomSelect :examId="form.examId" v-model="form.roomCode" />
          </el-form-item>
          <el-form-item label="科目">
            <CourseSelect
              :examId="form.examId"
              v-model="form.courseCode"
              exam-required
            />
          </el-form-item>
          <el-form-item label="交卷方式">
            <el-select
              v-model="form.finishType"
              placeholder="交卷方式"
              clearable
            >
              <el-option
                v-for="(val, key) in STUDENT_FINISH_EXAM_TYPE"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
          <el-form-item label="证件号">
            <el-input v-model.trim="form.identity"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleCurrentChange(0)"
              >查询</el-button
            >
          </el-form-item>
        </el-form>

        <div class="part-filter-form-action"></div>
      </div>
    </div>

    <el-table ref="table" :data="tableData">
      <el-table-column width="170" label="考试ID">
        <span slot-scope="scope">{{ scope.row.examRecordId }}</span>
      </el-table-column>
      <el-table-column width="150" label="批次名称">
        <span slot-scope="scope">{{ scope.row.name }}</span>
      </el-table-column>
      <el-table-column width="100" label="场次">
        <span slot-scope="scope">{{ scope.row.code }}</span>
      </el-table-column>
      <el-table-column width="150" label="姓名">
        <span slot-scope="scope">{{ scope.row.examStudentName }}</span>
      </el-table-column>
      <el-table-column width="150" label="证件号">
        <span slot-scope="scope">{{ scope.row.identity }}</span>
      </el-table-column>
      <el-table-column width="150" label="科目代码">
        <span slot-scope="scope">{{ scope.row.courseCode }}</span>
      </el-table-column>
      <el-table-column width="150" label="科目名称">
        <span slot-scope="scope">{{ scope.row.courseName }}</span>
      </el-table-column>
      <el-table-column width="150" label="考场代码">
        <span slot-scope="scope">{{ scope.row.roomCode }}</span>
      </el-table-column>
      <el-table-column width="150" label="考场名称">
        <span slot-scope="scope">{{ scope.row.roomName }}</span>
      </el-table-column>
      <el-table-column width="170" label="进入考试时间">
        <span slot-scope="scope">{{
          scope.row.firstStartTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column width="170" label="结束考试时间">
        <span slot-scope="scope">{{
          scope.row.finishTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column width="120" label="交卷方式">
        <span slot-scope="scope">{{ scope.row.finishTypeStr }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="110" fixed="right">
        <div slot-scope="scope">
          <el-button
            v-if="scope.row.finishTime"
            size="mini"
            type="primary"
            plain
            @click="toViewPaper(scope.row)"
          >
            查看原卷
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100, 200, 300]"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>

    <!-- PreviewPaperDialog -->
    <PreviewPaperDialog
      ref="PreviewPaperDialog"
      :examRecordId="curExamRecordId"
    />
  </div>
</template>

<script>
import { STUDENT_FINISH_EXAM_TYPE } from "@/constant/constants";
import { searchStudentExamDetailList } from "@/api/examwork-student";
import PreviewPaperDialog from "./PreviewPaperDialog.vue";

export default {
  name: "student-exam-detail",
  components: { PreviewPaperDialog },
  data() {
    return {
      form: {
        examId: "",
        activityId: "",
        roomCode: "",
        courseCode: "",
        name: "",
        identity: "",
        finishType: "",
      },
      rules: {
        examId: [{ required: true, message: "批次必选" }],
      },
      STUDENT_FINISH_EXAM_TYPE,
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      curExamRecordId: null,
    };
  },
  methods: {
    async searchForm() {
      const valid = await this.$refs.form.validate();
      if (!valid) return;

      const res = await searchStudentExamDetailList({
        ...this.form,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
      if (this.total > 0 && this.tableData.length === 0) {
        this.handleCurrentChange(this.currentPage - 1);
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    toViewPaper(row) {
      console.log(row);
      this.curExamRecordId = row.examRecordId;
      this.$refs.PreviewPaperDialog.openDialog();
    },
  },
};
</script>
