<template>
  <div class="exam-room-detail">
    <div class="part-box part-box-filter">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item label="考试名称：">
          <el-select
            v-model="filter.examId"
            style="width: 193px;"
            placeholder="请选择"
            @change="examChange"
            @clear="examChange"
            clearable
          >
            <el-option
              v-for="item in exams"
              :key="item.code"
              :value="item.code"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="场次ID：" label-width="75px">
          <el-select
            v-model="filter.sceneNumberId"
            style="width: 193px;"
            placeholder="请选择"
            @change="sceneChange"
            @clear="sceneChange"
            clearable
          >
            <el-option
              v-for="item in scenes"
              :key="item.code"
              :value="item.code"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考点：" label-width="55px">
          <el-select
            v-model="filter.examSite"
            style="width: 193px;"
            placeholder="请选择"
            @change="siteChange"
            @clear="siteChange"
            clearable
          >
            <el-option
              v-for="item in sites"
              :key="item.code"
              :value="item.code"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考场：" label-width="55px">
          <el-select
            v-model="filter.examRoomId"
            style="width: 193px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in rooms"
              :key="item.code"
              :value="item.code"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学号：" label-width="55px">
          <el-input
            v-model="filter.studentCode"
            placeholder="请输入"
            style="width: 193px;"
            clearable
          ></el-input>
          <!-- <el-select
            v-model="filter.studentCode"
            style="width: 193px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in students"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="icon icon-search" @click="toPage(1)"
            >查询</el-button
          >
          <el-button @click="goback">返回</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box">
      <el-table ref="TableList" :data="studentList" border stripe>
        <el-table-column
          prop="sceneNumberId"
          label="场次ID"
          width="80"
        ></el-table-column>
        <el-table-column prop="examSite" label="考点名称"></el-table-column>
        <el-table-column prop="examName" label="考试名称"></el-table-column>
        <el-table-column prop="examRoom" label="考场名称"></el-table-column>
        <el-table-column
          prop="courseNameCode"
          label="科目名称(编码)"
          min-width="100"
        >
        </el-table-column>
        <el-table-column prop="examNumber" label="考号"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="studentCode" label="学号"></el-table-column>
        <el-table-column
          prop="gender"
          label="性别"
          width="60"
        ></el-table-column>
        <el-table-column prop="classNo" label="教学班号"></el-table-column>
        <el-table-column prop="siteNumber" label="座位号"></el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { studentDetail, examSiteRoomList } from "../api";

export default {
  name: "exam-room-student-detail",
  data() {
    return {
      filter: {
        examId: "",
        sceneNumberId: "",
        examSite: "",
        examRoomId: "",
        studentCode: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      visible: false,
      exams: [],
      scenes: [],
      sites: [],
      rooms: [],
      studentList: []
    };
  },
  created() {
    this.init();
  },
  methods: {
    async init() {
      const examRoomInfo = this.$route.params.examRoomInfo;
      if (!examRoomInfo) return;

      const [examId, sceneNumberId, examSite, examRoomId] = examRoomInfo.split(
        "-"
      );

      // review selected
      await this.getExamSiteRoomList();
      this.filter.examId = examId;
      this.examChange();
      this.filter.sceneNumberId = sceneNumberId;
      this.sceneChange();
      this.filter.examSite = examSite;
      this.siteChange();
      this.filter.examRoomId = examRoomId;

      this.getList();
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await studentDetail(datas);
      this.studentList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async getExamSiteRoomList() {
      this.exams = await examSiteRoomList();
    },
    examChange() {
      const curExam = this.exams.find(exam => exam.code === this.filter.examId);
      if (!curExam) return;
      this.scenes = curExam.sceneNumbers;
      this.filter.sceneNumberId = "";
      this.sites = [];
      this.filter.examSite = "";
      this.rooms = [];
      this.filter.examRoomId = "";
    },
    sceneChange() {
      const curScene = this.scenes.find(
        scene => scene.code === this.filter.sceneNumberId
      );
      if (!curScene) return;
      this.sites = curScene.examSites;
      this.filter.examSite = "";
      this.rooms = [];
      this.filter.examRoomId = "";
    },
    siteChange() {
      const curSite = this.sites.find(
        site => site.code === this.filter.examSite
      );
      if (!curSite) return;
      this.rooms = curSite.examRooms;
      this.filter.examRoomId = "";
    }
  },
  beforeRouteLeave(to, from, next) {
    if (to.name === "ExamRoomDetail") {
      this.$store.commit("setRestoreKeepAliveRoutes");
    }
    next();
  }
};
</script>
