<template>
  <div class="exam-student-management">
    <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" />
          </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" />
          </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-form-item label="相片">
            <BasePhotoStateSelect v-model="form.hasPhoto" />
          </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">
          <el-button type="primary" icon="icon icon-add" @click="add"
            >新增</el-button
          >
          <el-button
            type="success"
            @click="toggleEnableExamStudentArray({ enable: 1 })"
            >启用</el-button
          >
          <el-button
            type="danger"
            @click="toggleEnableExamStudentArray({ enable: 0 })"
            >禁用</el-button
          >
          <el-button
            type="primary"
            icon="icon icon-upload"
            @click="exportExamStudent"
            >导出</el-button
          >
        </div>
      </div>
    </div>

    <el-table ref="table" :data="tableData">
      <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="260" fixed="right">
        <div slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            @click="openBasePhotoDialog(scope.row.basePhotoUrl)"
            v-if="scope.row.basePhotoUrl"
          >
            相片
          </el-button>
          <span v-else style="width: 66px; display: inline-block;">无相片</span>
          <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 | booleanEnableDisableFilter }}
          </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>

    <el-dialog
      title="底照"
      :visible.sync="basePhotoDialogVisible"
      destroy-on-close
    >
      <img :src="selectedBasePhoto" style="width: 400px;" />
    </el-dialog>

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

<script>
import {
  exportExamStudent,
  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: "",
        hasPhoto: null,
      },
      rules: {
        examId: [{ required: true, message: "批次必选" }],
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      selected: {},
      selectedBasePhoto: null,
      basePhotoDialogVisible: false,
    };
  },
  async created() {},
  methods: {
    async searchForm() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
      } catch (error) {
        console.log(error);
        return;
      }
      const res = await searchExamStudents({
        ...this.form,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records;
      this.total = res.data.data.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.$notify({ title: "操作成功", type: "success" });
      this.searchForm();
    },
    openBasePhotoDialog(url) {
      this.selectedBasePhoto = url;
      this.basePhotoDialogVisible = true;
    },
    async exportExamStudent() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
      } catch (error) {
        console.log(error);
        return;
      }
      await exportExamStudent(this.form);
      this.$notify({ title: "导出任务已成功启动", type: "success" });
    },
  },
};
</script>

<style></style>
