<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
          class-name="action-column"
          label="操作"
          width="160px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.hasReportFile"
              class="btn-primary"
              type="text"
              :disabled="loading"
              @click="toDonwloadLog(scope.row)"
              >导出日志</el-button
            >
            <el-button
              v-if="!scope.row.resetCreatePdf"
              class="btn-primary"
              type="text"
              :disabled="loading"
              @click="toDonwloadFile(scope.row)"
              >下载文件</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 { attachmentDownload } from "../../login/api";
import { downloadByUrl } from "@/plugins/download";

export default {
  name: "data-task-dialog",
  props: {
    taskType: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      modalIsShow: false,
      filter: {
        type: "",
        status: "",
        result: ""
      },
      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;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async toDonwloadLog(row) {
      if (this.loading) return;

      this.loading = true;
      const res = await attachmentDownload({
        id: row.id,
        type: "TASK_REPORT"
      }).catch(() => {});
      this.loading = false;

      if (!res) {
        this.$message.error("文件下载失败，请重新尝试！");
        return;
      }

      const url = res.url;
      if (url.endsWith(".txt")) {
        window.open(url);
        return;
      }

      downloadByUrl(url);
      this.$message.success("文件下载成功！");
    },
    async toDonwloadFile(row) {
      if (this.loading) return;

      this.loading = true;
      const type = row.hasResultFile ? "RESULT" : "IMPORT_FILE";
      const res = await attachmentDownload({
        id: row.id,
        type
      }).catch(() => {});
      this.loading = false;

      if (!res) {
        this.$message.error("文件下载失败，请重新尝试！");
        return;
      }

      downloadByUrl(res.url);
      this.$message.success("文件下载成功！");
    }
  }
};
</script>
