<template>
  <div class="app-container">
      <div class="mb-10 flex items-center">
          <div class="flex-1">
              <h2 class="page-title mb-5">下载队列</h2>
              <div>*{{$t('文件生成成功后，保留12小时，12小时后自动删除')}}</div>
          </div>
          <el-button class="" @click="handleQuery" type="primary">刷新</el-button>
      </div>
    <!--el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item :label="$t('文件名称')" prop="fileName">
        <el-input v-model="queryParams.fileName" :placeholder="$t('请输入文件名称')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{$t('搜索')}}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{$t('重置')}}</el-button>
      </el-form-item>
    </el-form-->

    <el-table v-loading="loading" :data="list">
      <el-table-column :label="$t('下载编号')" align="center" prop="id" width="100" />
      <el-table-column :label="$t('分类')" align="left" prop="type" width="120" >
          <template v-slot:default="{row}">
              <dict-tag :type="DICT_TYPE.DOWNLOAD_TYPE" :value="row.type"/>
          </template>
      </el-table-column>
      <!--<el-table-column :label="$t('业务编号')" align="center" prop="type" width="100" /-->
      <el-table-column :label="$t('下载内容')" align="left" prop="name" />
      <el-table-column :label="$t('开始下载时间')" align="center" prop="createTime"  width="150">
          <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
      </el-table-column>
      <!--<el-table-column :label="$t('结果')" align="center" prop="createTime"  width="150">
        <template slot-scope="scope">
          {{scope.row.result}}
        </template>
      </el-table-column>-->
      <el-table-column :label="$t('下载状态')" align="center" prop="createBy"  width="150">
          <template v-slot:default="{row}">
              <!--status ==2 表示已处理，配合downNum来判断是否已下载-->
              <template v-if="row.status == 2 && row.downNum > 0">{{$t('已下载')}}</template>
              <template v-else-if="row.status == 2">{{$t('待下载')}}</template>
              <dict-tag v-else :type="DICT_TYPE.DOWNLOAD_LOG_STATUS" :value="row.status"/>
          </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope" >
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDownload(scope.row)"
                     v-if="scope.row.status == 2">{{$t('下载')}}</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleRetry(scope.row)"
                     v-if="scope.row.status == 3">{{$t('重试')}}</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDel(scope.row)"
                     >{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows"
                @pagination="getList"/>

  </div>
</template>

<script>
import {deleteLog, download, downloadFileResponse, downloadPage, retry} from "@/api/system/download";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Download",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      list: [],
      queryParams: {
        page: 1,
        rows: 10
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
      parseTime,
      handleDownload(row){
          const loading = this.$loading()
          download(row.id).then(res => {
              return downloadFileResponse(res.data)
          }).then(res => {
              console.log({res})
              this.$download.download0(res.data, row.name + '.' + row.fileSuffix, res.headers['content-type'])
          }).finally(() => {
              // 刷新列表状态
              this.getList()
              loading.close()
          })
      },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      downloadPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      }).finally(() => {
        this.loading = false;
      })
    },
    handleRetry(row){
        retry(row.id).then(res => {
            this.$message.success('重试已提交')
            this.getList()
        })
    },
    handleDel(row){
      deleteLog(row.id).then(res => {
          this.$message.success('删除成功')
          this.getList()
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
