<template>
  <!-- <div class="part-box is-filter">
    <a-space class="filter-line" :size="12" wrap> -->
  <!-- <a-select
        v-model="searchModel.type"
        :allow-clear="true"
        :options="taskList"
        popup-container="body"
      >
        <template #prefix>任务类型</template>
      </a-select> -->
  <!-- <a-select
        v-model="searchModel.status"
        :allow-clear="true"
        :options="[
          { value: 'SUCCESS', label: '成功' },
          { value: 'ERROR', label: '失败' },
        ]"
        popup-container="body"
      >
        <template #prefix>任务状态</template>
      </a-select> -->
  <!-- <a-button type="primary" @click="toPage(1)">查询</a-button> -->
  <!-- </a-space>
  </div> -->
  <div class="part-box">
    <a-table
      class="page-table"
      :columns="columns"
      :data="dataList"
      :pagination="false"
      :scroll="{ x: 1200 }"
      :bordered="false"
    >
      <template #type="{ record }">{{ taskTypeEnum[record.type] }}</template>
      <template #status="{ record }">{{
        taskStatusEnum[record.status]
      }}</template>
      <template #result="{ record }">{{
        taskResultEnum[record.result]
      }}</template>
      <template #createTime="{ record }">
        {{ timestampFilter(record?.createTime) }}
      </template>
      <template #action="{ record }">
        <a-button
          v-if="record.summary"
          type="text"
          class="btn-primary"
          @click="view(record.summary)"
          >查看</a-button
        >
        <a-button
          v-if="record.exportFilePath"
          type="text"
          class="btn-primary"
          @click="download(record)"
          >下载</a-button
        >
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
  import { reactive, ref, computed, onMounted, watch } from 'vue';
  import { Message, TableColumnData, Modal } from '@arco-design/web-vue';
  import { getMyTasks, getTaskTypes } from '@/api/order';
  import { TaskItem } from '@/api/types/order';
  import useTable from '@/hooks/table';
  import { useAppStore } from '@/store';
  import { modalConfirm } from '@/utils/arco';
  import { downloadByCrossUrl } from '@/utils';
  import { timestampFilter } from '@/utils/filter';

  defineOptions({
    name: 'MyTask',
  });

  const taskList = ref([]);
  const taskTypeEnum = reactive<any>({});
  const taskStatusEnum = reactive<any>({
    INIT: '初始化',
    RUNNING: '进行中',
    FINISH: '已完成',
  });
  const taskResultEnum = reactive<any>({
    SUCCESS: '成功',
    ERROR: '失败',
  });
  onMounted(() => {
    getTaskTypes().then((res: any) => {
      taskList.value = (res || []).map((item: any) => ({
        label: item.name,
        value: item.type,
      }));
      taskList.value.forEach((item: any) => {
        taskTypeEnum[item.value] = item.label;
      });
    });
  });
  const appStore = useAppStore();
  appStore.setInfo({ breadcrumbs: ['系统设置', '我的任务'] });

  const searchModel = reactive({
    type: '',
    // status: '',
  });
  const computedModel = computed(() => {
    return { type: searchModel.type ? searchModel.type : null };
  });
  const columns = computed<TableColumnData[]>(() => {
    return [
      {
        title: '任务类型',
        dataIndex: 'type',
        slotName: 'type',
      },
      {
        title: '任务状态',
        dataIndex: 'status',
        slotName: 'status',
      },
      {
        title: '任务结果',
        dataIndex: 'result',
        slotName: 'result',
      },
      {
        title: '创建人',
        dataIndex: 'operator',
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        slotName: 'createTime',
      },
      {
        title: '操作',
        slotName: 'action',
        width: 180,
        fixed: 'right',
        cellClass: 'action-column',
      },
    ];
  });
  const { dataList, pagination, toPage, getList } = useTable<any>(
    getMyTasks,
    computedModel,
    true
  );

  const download = (record: any) => {
    downloadByCrossUrl(record.exportFilePath, `${Date.now()}`);
  };

  const view = (summary: string) => {
    Modal.info({
      title: '系统提示',
      content: summary,
    });
  };
</script>
