<template>
  <div>
    <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" />
      </el-form-item>
      <el-form-item label="考场名称">
        <el-input v-model.trim="form.roomCode"></el-input>
      </el-form-item>
      <el-form-item label="科目">
        <CourseSelect :examId="form.examId" v-model="form.courseCode" />
      </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 label="年级">
        <el-input v-model.trim="form.grade"></el-input>
      </el-form-item>
      <el-form-item label="教学班级">
        <el-input v-model.trim="form.classNo"></el-input>
      </el-form-item>
      <el-button @click="searchForm">查询</el-button>
      <el-button @click="add">新增</el-button>
      <el-button @click="toggleEnableExamStudentArray({ enable: 1 })"
        >启用</el-button
      >
      <el-button @click="toggleEnableExamStudentArray({ enable: 0 })"
        >禁用</el-button
      >
      <!-- <el-button>导入</el-button> -->
    </el-form>

    <el-table ref="table" :data="tableData" stripe style="width: 100%;">
      <el-table-column type="selection" width="42" />
      <el-table-column width="100" label="ID">
        <span slot-scope="scope">{{ scope.row.id }}</span>
      </el-table-column>
      <el-table-column width="200" label="场次代码">
        <span slot-scope="scope">{{ scope.row.activityCode }}</span>
      </el-table-column>
      <el-table-column label="姓名">
        <span slot-scope="scope">{{ scope.row.name }}</span>
      </el-table-column>
      <el-table-column width="200" label="证件号">
        <span slot-scope="scope">{{ scope.row.identity }}</span>
      </el-table-column>
      <el-table-column width="200" label="科目名称">
        <span slot-scope="scope">{{ scope.row.courseName }}</span>
      </el-table-column>
      <el-table-column width="200" label="科目代码">
        <span slot-scope="scope">{{ scope.row.courseCode }}</span>
      </el-table-column>
      <el-table-column width="120" label="状态">
        <span slot-scope="scope">{{
          scope.row.enable | zeroOneEnableDisableFilter
        }}</span>
      </el-table-column>
      <el-table-column width="100" label="考场代码">
        <span slot-scope="scope">{{ scope.row.roomCode }}</span>
      </el-table-column>
      <el-table-column width="100" label="考场名称">
        <span slot-scope="scope">{{ scope.row.roomName }}</span>
      </el-table-column>
      <el-table-column width="120" label="年级">
        <span slot-scope="scope">{{ scope.row.grade }}</span>
      </el-table-column>
      <el-table-column width="170" label="教学班级">
        <span slot-scope="scope">{{ scope.row.classNo }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="210">
        <div slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="edit(scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            @click="toggleEnableExamStudent(scope.row)"
          >
            {{ scope.row.enable ? "禁用" : "启用" }}
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="page float-right">
      <el-pagination
        @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>

    <ExamStudentManagementDialog
      ref="theDialog"
      :examId="examId"
      :examStudent="selected"
      @reload="searchForm"
    />
  </div>
</template>

<script>
import {
  searchExamStudents,
  toggleEnableExamStudent,
  toggleEnableExamStudentArray,
} from "@/api/examwork-examstudent";
import ExamStudentManagementDialog from "./ExamStudentManagementDialog";

export default {
  name: "ExamStudentManagement",
  components: {
    ExamStudentManagementDialog,
  },
  computed: {
    examId() {
      return this.$route.params.examId;
    },
  },
  data() {
    return {
      form: {
        examId: "",
        activityId: "",
        roomCode: "",
        courseCode: "",
        name: "",
        identity: "",
        grade: "",
        classNo: "",
      },
      rules: {
        examId: [{ required: true, message: "批次必选" }],
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      selected: {},
    };
  },
  async created() {},
  methods: {
    async searchForm() {
      // console.log(this.form);
      const valid = await this.$refs.form.validate();
      if (!valid) return;
      const res = await searchExamStudents({
        ...this.form,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records.records;
      this.total = res.data.data.records.total;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    add() {
      this.selected = {};
      this.$refs.theDialog.openDialog();
    },
    edit(examStudent) {
      this.selected = examStudent;
      this.$refs.theDialog.openDialog();
    },
    async toggleEnableExamStudent(examStudent) {
      await toggleEnableExamStudent({
        id: examStudent.id,
        enable: examStudent.enable === 0 ? 1 : 0,
      });
      this.searchForm();
    },
    async toggleEnableExamStudentArray({ enable }) {
      let rows = this.$refs.table.selection;
      if (rows.length < 1) {
        this.$notify({
          type: "warning",
          title: "请至少选择一个考生",
        });
        return;
      }
      await toggleEnableExamStudentArray(
        rows.map((v) => ({ id: v.id, enable }))
      );
      this.searchForm();
    },
  },
};
</script>

<style></style>
