<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { message } from 'ant-design-vue';
  const config = import.meta.env;
  import {
    taskCenterQueryListApi,
    taskCenterRevokeExcelUploadApi,
  } from '/@/api/regionalSpotManage';
  import { UploadTaskType } from '../types';
  onMounted(async () => {
    getList();
  });
  const list = ref<UploadTaskType[]>([]);
  const listLoading = ref(false);
  const downLoad = (url) => {
    window.open(config.VITE_GLOB_TMALLAB_URL + '/system/excelFile/' + url, '_blank');
  };
  const downloadFail = (record) => {
    if (record.failNum === 0) return;
    let href =
      config.VITE_GLOB_TMALLAB_URL +
      '/excel/excelNew/fail/download?sysid=' +
      record.sysid +
      '&type=' +
      record.type.slice(2, 6);
    window.open(href, 'blank');
  };
  const cancel = async (record) => {
    const param = ref<{ [key: string]: any }>({
      sysid: record.sysid,
    });
    const res = await taskCenterRevokeExcelUploadApi(param.value);
    if (res.success) {
      message.success('操作成功');
      getList();
    } else {
      message.error(res.message);
    }
  };
  const getList = async () => {
    listLoading.value = true;
    const res = await taskCenterQueryListApi({ type: 0 });
    if (res.success) {
      list.value = res.data;
    } else {
      list.value = [];
    }
    listLoading.value = false;
  };
</script>

<template>
  <div>
    <a-button @click="getList" type="primary">刷新数据</a-button>
  </div>
  <a-table
    :data-source="list"
    :loading="listLoading"
    bordered
    :rowKey="(record) => record.sysid"
    style="margin-top: 15px"
  >
    <a-table-column title="操作时间" align="center" :width="100">
      <template #default="{ record }">
        {{ record.createTime }}
      </template>
    </a-table-column>
    <a-table-column title="类型" align="center" :width="100">
      <template #default="{ record }">
        {{ record.type }}
      </template>
    </a-table-column>
    <a-table-column title="状态" align="center" :width="100">
      <template #default="{ record }">
        <p>{{
          record.isRevoke
            ? '已撤销'
            : record.status == 0
            ? '待进行'
            : record.status == 1
            ? '进行中'
            : '已完成'
        }}</p>
      </template>
    </a-table-column>
    <a-table-column title="操作数据" align="center" :width="100">
      <template #default="{ record }">
        {{ record.productNum }}
      </template>
    </a-table-column>
    <a-table-column title="成功数据" align="center" :width="100">
      <template #default="{ record }">
        {{ record.successNum }}
      </template>
    </a-table-column>
    <a-table-column title="错误数据" align="center" :width="100">
      <template #default="{ record }">
        {{ record.failNum }}
      </template>
    </a-table-column>
    <a-table-column title="操作人" align="center" :width="100">
      <template #default="{ record }">
        {{ record.createName }}
      </template>
    </a-table-column>
    <a-table-column title="操作" align="center" :width="200">
      <template #default="{ record }">
        <a-button type="link" @click="downLoad(record.fileUrl)">下载源文件</a-button>
        <a-button type="link" v-if="record.failNum" @click="downloadFail(record)"
          >下载错误数据</a-button
        >
        <a-button type="link" v-if="record.status == 0 && !record.isRevoke" @click="cancel(record)"
          >撤销</a-button
        >
      </template>
    </a-table-column>
  </a-table>
</template>

<style lang="less" scoped></style>
