<template>
  <div class="invigilate-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="考场">
            <ExamRoomSelect
              :examId="form.examId"
              v-model="form.roomCode"
            ></ExamRoomSelect>
          </el-form-item>
          <el-form-item label="监考老师">
            <InvigilatorSelect v-model="form.userId"></InvigilatorSelect>
          </el-form-item>
          <el-button type="primary" @click="handleCurrentChange(0)"
            >查询</el-button
          >
        </el-form>
        <div class="part-filter-form-action">
          <el-button
            type="primary"
            icon="icon icon-upload"
            @click="importDialog"
            >导入考场设置</el-button
          >
          <el-button
            type="primary"
            icon="icon icon-upload"
            @click="exportInvigilate"
            >导出考场安排</el-button
          >
          <el-button type="text">
            <a :href="downloadUrl" download class="mx-2">下载导入模板</a>
          </el-button>
          <!-- <el-button>导入</el-button> -->
        </div>
      </div>
    </div>

    <el-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 label="批次ID">
        <span slot-scope="scope">{{ scope.row.examId }}</span>
      </el-table-column>
      <el-table-column label="批次名称">
        <span slot-scope="scope">{{ scope.row.examName }}</span>
      </el-table-column>
      <el-table-column width="200" label="考场编码">
        <span slot-scope="scope">{{ scope.row.roomCode }}</span>
      </el-table-column>
      <el-table-column label="考场名称">
        <span slot-scope="scope">{{ scope.row.roomName }}</span>
      </el-table-column>
      <el-table-column width="220" label="监考老师">
        <span slot-scope="scope">{{ scope.row.name }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="210" fixed="right">
        <div slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="edit(scope.row)">
            编辑
          </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>

    <InvigilateManagementDialog
      ref="theDialog"
      :user="selectedUser"
      @reload="searchForm"
    />

    <InvigilateImportDialog
      ref="theDialog2"
      :examId="form.examId"
      @reload="searchForm"
    />
  </div>
</template>

<script>
import InvigilateManagementDialog from "./InvigilateManagementDialog";
import { searchInvigilators } from "@/api/examwork-invigilate";
import { INVIGILATOR_IMPORT_TEMPLATE_DOWNLOAD_URL } from "@/constant/constants";
import InvigilateImportDialog from "./InvigilateImportDialog";
import { exportInvigilate } from "@/api/examwork-task";

export default {
  name: "InvigilateManagement",
  components: {
    InvigilateImportDialog,
    InvigilateManagementDialog,
  },
  data() {
    return {
      form: {
        examId: "",
        roomCode: "",
        userId: "",
      },
      rules: {
        examId: [{ required: true, message: "批次必选" }],
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      selectedUser: {},
      downloadUrl: INVIGILATOR_IMPORT_TEMPLATE_DOWNLOAD_URL,
    };
  },
  // async created() {
  //   this.searchForm();
  // },
  methods: {
    async searchForm() {
      try {
        await this.$refs.form.validate();
      } catch (error) {
        console.log(error);
        return;
      }
      const res = await searchInvigilators({
        examId: this.form.examId,
        userId: this.form.userId,
        roomCode: this.form.roomCode,
        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();
    },
    edit(user) {
      this.selectedUser = user;
      this.$refs.theDialog.openDialog();
    },
    async importDialog() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
      } catch (error) {
        console.log(error);
        return;
      }
      this.$refs.theDialog2.openDialog();
    },
    async exportInvigilate() {
      try {
        await this.$refs.form.validate();
      } catch (error) {
        console.log(error);
        return;
      }
      exportInvigilate({
        examId: this.form.examId,
        userId: this.form.userId,
        roomCode: this.form.roomCode,
      });
      this.$notify({ title: "导入任务已成功启动", type: "success" });
    },
  },
};
</script>

<style></style>
