<template>
  <div class="import-export-task">
    <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="type">
            <TaskSelect v-model="form.type" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleCurrentChange(0)"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>

    <el-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="150" label="任务名称">
        <span slot-scope="scope">{{ scope.row.type | taskName }}</span>
      </el-table-column>
      <el-table-column width="150" 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 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' &&
              (scope.row.hasResultFile === '1' ||
                scope.row.importFileName !== null)
            "
            @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="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>
  </div>
</template>

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

export default {
  name: "ImportExportTask",
  data() {
    return {
      form: {
        type: "",
      },
      rules: {},
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
    };
  },
  created() {
    this.searchForm();
  },
  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: this.form.type,
        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 download({ id, type }) {
      const res = await downloadFile({ id, type });
      const url = res.data.data.url;
      downloadFileURL(url);
    },
  },
  filters: {
    taskName(code) {
      return IMPORT_EXPORT_TASKS.find((v) => v.code == code)?.name;
    },
  },
};
</script>

<style></style>
