<template>
  <div class="ReexamApply">
    <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="FilterForm" label-position="left" inline>
          <el-form-item>
            <el-select
              v-model="filter.examId"
              placeholder="请选择批次"
              @change="examChange"
            >
              <el-option
                v-for="item in examBatchs"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="filter.roomCode"
              placeholder="请选择考场"
              clearable
            >
              <el-option
                v-for="item in examRooms"
                :key="item.id"
                :value="item.roomCode"
                :label="item.roomName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="filter.courseCode"
              placeholder="请选择科目"
              clearable
            >
              <el-option
                v-for="item in examCourses"
                :key="item.courseCode"
                :value="item.courseCode"
                :label="item.courseName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="filter.name"
              placeholder="姓名"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="filter.identity"
              placeholder="证件号"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="toPage(1)">查询</el-button>
          </el-form-item>
        </el-form>

        <div class="part-filter-form-action">
          <el-button type="primary" icon="icon icon-seal" @click="batchAction"
            >批量设置重考</el-button
          >
        </div>
      </div>
    </div>

    <el-table
      ref="TableList"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column prop="roomName" label="考场名称（代码）">
        <span slot-scope="scope"
          >{{ scope.row.roomName }}({{ scope.row.roomCode }})</span
        >
      </el-table-column>
      <el-table-column prop="identity" label="证件号"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="courseName" label="科目名称"></el-table-column>
      <el-table-column prop="courseCode" label="科目代码"></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            class="btn-table-icon"
            type="primary"
            icon="icon icon-seal"
            @click="toEdit(scope.row)"
            v-if="!scope.row.reexamCount"
            >重考</el-button
          >
          <el-button :disabled="scope.row.reexamCount" v-else>
            已申请
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        layout="prev, pager, next,total,sizes,jumper"
        :current-page="current"
        :total="total"
        :page-size.sync="size"
        @size-change="toPage(1)"
        @current-change="toPage"
      >
      </el-pagination>
    </div>

    <apply-reexam-dialog
      :students="selectStudents"
      :data-list="dataList"
      @modified="applyFinish"
      ref="ApplyReexamDialog"
    ></apply-reexam-dialog>
  </div>
</template>

<script>
import {
  examBatchList,
  examActivityRoomList,
  reexamApplyList,
} from "@/api/invigilation";
import ApplyReexamDialog from "./ApplyReexamDialog";
import { mapState, mapActions, mapMutations } from "vuex";

export default {
  name: "ReexamApply",
  components: { ApplyReexamDialog },
  data() {
    return {
      filter: {
        examId: null,
        roomCode: null,
        courseCode: null,
        name: "",
        identity: "",
      },
      current: 1,
      total: 0,
      size: 10,
      examBatchs: [],
      examActivities: [],
      examRooms: [],
      examCourses: [],
      dataList: [],
      selectStudents: [],
      multipleSelection: [],
    };
  },
  computed: {
    ...mapState("invigilation", ["selectedExamId"]),
  },
  mounted() {
    this.initData();
  },
  methods: {
    ...mapActions("invigilation", ["updateSelectedExamId"]),
    ...mapMutations("invigilation", ["setSelectedExamId"]),
    async initData() {
      await this.getExamBatchList();
      if (!this.examBatchs.length) return;
      this.updateSelectedExamId({
        exams: this.examBatchs,
        selectedExamId: this.selectedExamId,
      });
      this.filter.examId = this.selectedExamId;
      this.toPage(1);
      this.getExamActivityRoomList();
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };

      const res = await reexamApplyList(datas);

      this.dataList = res.data.data.records;
      this.total = res.data.data.total;
    },
    toPage(page) {
      this.setSelectedExamId(this.filter.examId);
      this.current = page;
      this.getList();
    },
    async getExamBatchList() {
      const user = this.$store.state.user;
      const userId = user.roleCodes.includes("INVIGILATE") ? user.id : null;
      const res = await examBatchList(userId);
      this.examBatchs = res.data.data;
    },
    async getExamActivityRoomList() {
      if (!this.filter.examId) return;
      const res = await examActivityRoomList(this.filter.examId);
      this.examActivities = res.data.data.examActivitys;
      this.examRooms = res.data.data.examRooms;
      this.examCourses = res.data.data.examCourses;
    },
    examChange() {
      this.filter.examActivityId = null;
      this.filter.roomCode = null;
      this.filter.courseCode = null;
      this.getExamActivityRoomList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    batchAction() {
      if (!this.multipleSelection.length) {
        this.$message.error("请先选择数据！");
        return;
      }
      this.selectStudents = this.multipleSelection;
      this.$refs.ApplyReexamDialog.open();
    },
    toEdit(row) {
      this.selectStudents = [row];
      this.$refs.ApplyReexamDialog.open();
    },
    applyFinish() {
      this.selectStudents = [];
      this.multipleSelection = [];
      this.getList();
    },
  },
};
</script>
