<template>
  <el-dialog
    ref="dialog"
    title="考试记录"
    width="600px"
    :visible.sync="visible"
    @close="closeDialog"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-position="right"
      label-width="120px"
      inline
    >
      <el-form-item label="批次名称" prop="examId">
        <ExamSelect v-model="form.examId" />
      </el-form-item>
      <el-button type="primary" @click="handleCurrentChange(0)">查询</el-button>
    </el-form>

    <el-table :data="tableData" stripe style="width: 100%;">
      <el-table-column width="100" label="姓名">
        <span slot-scope="scope">{{ scope.row.name }}</span>
      </el-table-column>
      <el-table-column width="180" label="证件号">
        <span slot-scope="scope">{{ scope.row.identity }}</span>
      </el-table-column>
      <el-table-column label="批次名称">
        <span slot-scope="scope">{{ scope.row.examName }}</span>
      </el-table-column>
      <el-table-column width="100" label="课程">
        <span slot-scope="scope">{{ scope.row.courseName }}</span>
      </el-table-column>
      <el-table-column width="100" label="状态">
        <span slot-scope="scope">{{ scope.row.status }}</span>
      </el-table-column>
    </el-table>
    <div class="page float-right">
      <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>
    <div class="my-2"></div>
  </el-dialog>
</template>

<script>
import { searchStudentExamRecord } from "@/api/examwork-student";
export default {
  name: "StudentManagementDialog",
  props: {
    student: Object,
  },
  watch: {
    student() {
      this.tableData = [];
      this.pageSize = 10;
      this.total = 0;
    },
  },
  data() {
    return {
      visible: false,
      form: {
        examId: "",
      },
      rules: {
        examId: [{ required: true, message: "批次必选" }],
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
    };
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    async searchForm() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
      } catch (error) {
        console.log(error);
        return;
      }
      const res = await searchStudentExamRecord({
        examId: this.form.examId,
        studentId: this.student.id,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
    },
  },
};
</script>

<style></style>
