<template>
  <div>
    <el-form :model="form" inline>
      <!-- <el-form-item v-if="$store.state.user.orgId === null" label="机构">
        <OrgSelect v-model="form.orgId"></OrgSelect>
      </el-form-item> -->
      <el-form-item label="考场">
        <ExamRoomSelect v-model="form.roomCode"></ExamRoomSelect>
      </el-form-item>
      <el-form-item label="监考老师">
        <InvigilatorSelect v-model="form.userId"></InvigilatorSelect>
      </el-form-item>
      <el-button @click="searchForm">查询</el-button>
      <el-button @click="importDialog">导入考场设置</el-button>
      <el-button @click="exportInvigilate">导出考场安排</el-button>
      <a :href="downloadUrl" download class="mx-2">下载导入模板</a>
      <!-- <el-button>导入</el-button> -->
    </el-form>

    <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 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">
        <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="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>

    <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: {
        roomCode: "",
        userId: "",
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      selectedUser: {},
      downloadUrl: INVIGILATOR_IMPORT_TEMPLATE_DOWNLOAD_URL,
    };
  },
  async created() {},
  methods: {
    async searchForm() {
      const res = await searchInvigilators({
        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();
    },
    importDialog() {
      this.$refs.theDialog2.openDialog();
    },
    exportInvigilate() {
      exportInvigilate();
      this.$notify({ title: "导入任务已成功启动", type: "success" });
    },
  },
};
</script>

<style></style>
