<template>
  <el-dialog
    class="data-task-dialog page-dialog"
    :visible.sync="modalIsShow"
    title="数据任务进度"
    top="10vh"
    width="900px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="50"
          :index="indexMethod"
        ></el-table-column>
        <!-- <el-table-column prop="type" label="类别" width="140">
          <span slot-scope="scope">
            {{ scope.row.type | dataTaskTypeFilter }}
          </span>
        </el-table-column> -->
        <el-table-column prop="status" label="状态" width="100">
          <span slot-scope="scope">
            {{ scope.row.status | dataTaskStatusFilter }}
          </span>
        </el-table-column>
        <el-table-column prop="result" label="结果" width="80">
          <span slot-scope="scope">
            {{ scope.row.result | dataTaskResultFilter }}
          </span>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="170">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="createName" label="创建人"></el-table-column>
        <el-table-column prop="errorMessage" label="失败原因"></el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="160px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.reportFile"
              class="btn-primary"
              type="text"
              :disabled="loading"
              @click="toDonwload(scope.row.reportFile)"
              >导出日志</el-button
            >
            <el-button
              v-if="scope.row.resultFile"
              class="btn-primary"
              type="text"
              :disabled="loading"
              @click="toDonwload(scope.row.resultFile)"
              >下载文件</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <div slot="footer"></div>
  </el-dialog>
</template>

<script>
import { dataTaskList } from "../api";
import { downloadByUrl } from "@/plugins/download";

export default {
  name: "data-task-dialog",
  props: {
    taskType: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      modalIsShow: false,
      filter: {
        type: "", // WHU_TEACH_DATA("同步武大考务数据"),CLOUD_MARKING_STUDENT_SCORE("同步云阅卷考生成绩");
        status: "", // INIT("未开始"),RUNNING("进行中"),FINISH("已完成");
        result: "", // SUCCESS("成功"),ERROR("失败");
      },
      dataList: [],
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      loading: false,
    };
  },
  methods: {
    visibleChange() {
      this.filter.type = this.taskType;
      this.toPage(1);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await dataTaskList(datas);
      this.dataList = data.records.map((item) => {
        item.reportFile = item.txtFilePath;
        item.resultFile = item.importFilePath || item.exportFilePath || "";
        return item;
      });
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toDonwload(url) {
      if (url.endsWith(".txt")) {
        window.open(url);
        return;
      }

      downloadByUrl(url);
      this.$message.success("文件开始成功！");
    },
  },
};
</script>
