<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-button @click="searchForm">查询</el-button>
      <el-button @click="importFile">导入</el-button>
      <!-- <el-button>导入</el-button> -->
      <a :href="downloadUrl" download class="mx-2">下载模板</a>
    </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.entityName }}</span>
      </el-table-column>
      <el-table-column label="上传文件名">
        <span slot-scope="scope">{{ scope.row.importFileName }}</span>
      </el-table-column>
      <el-table-column width="120" label="状态">
        <span slot-scope="scope">{{ scope.row.status }}</span>
      </el-table-column>
      <el-table-column width="100" label="异常">
        <span slot-scope="scope">{{ scope.row.summary }}</span>
      </el-table-column>
      <el-table-column width="100" label="上传时间">
        <span slot-scope="scope">{{
          scope.row.createTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="210">
        <div slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            v-if="scope.row.status === 'FINISH'"
            @click="
              download({
                id: scope.row.id,
                type: scope.row.hasResultFile === '1' ? 'RESULT' : 'IMPORTFILE',
              })
            "
          >
            下载文件
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            v-if="scope.row.hasReportFile === '1'"
            @click="download({ id: scope.row.id, type: 'REPORT' })"
          >
            导出报告
          </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>

    <ExamStudentImportDialog
      ref="theDialog"
      :examId="form.examId"
      @reload="searchForm"
    />
  </div>
</template>

<script>
import { searchTasks } from "@/api/examwork-task";
import { downloadFileURL } from "@/utils/utils";
import ExamStudentImportDialog from "./ExamStudentImportDialog";
import { EXAM_STUDENT_IMPORT_TEMPLATE_DOWNLOAD_URL } from "@/constant/constants";
import { downloadFile } from "@/api/system-info";

export default {
  name: "ExamStudentImport",
  data() {
    return {
      form: {
        examId: "",
      },
      rules: {
        examId: [{ required: true, message: "批次必选" }],
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      downloadUrl: EXAM_STUDENT_IMPORT_TEMPLATE_DOWNLOAD_URL,
    };
  },
  components: { ExamStudentImportDialog },
  methods: {
    async searchForm() {
      // try {
      //   const valid = await this.$refs.form.validate();
      //   if (!valid) return;
      // } catch (error) {
      //   console.log(error);
      //   return;
      // }
      const res = await searchTasks({
        type: "IMPORT_EXAM_STUDENT",
        entityId: this.form.examId,
        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();
    },
    async importFile() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
      } catch (error) {
        console.log(error);
        return;
      }
      this.$refs.theDialog.openDialog();
    },
    async download({ id, type }) {
      const res = await downloadFile({ id, type });
      const url = res.data.data.url;
      downloadFileURL(url);
    },
  },
};
</script>

<style></style>
