<template>
  <div style="max-height: calc(100vh - 184px); overflow-y: auto; overflow-x: hidden">
    <byTable
      :source="sourceList.data"
      :pagination="sourceList.pagination"
      :config="config"
      :loading="loading"
      :searchConfig="searchConfig"
      highlight-current-row
      :action-list="[
        // {
        //   text: '刷新',
        //   action: () => getList(),
        // },
      ]"
      @get-list="getList">
    </byTable>
  </div>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import { ElMessage, ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();
const status = ref([
  {
    dictKey: 1,
    dictValue: "数据处理中",
  },
  {
    dictKey: 2,
    dictValue: "Excel生成中",
  },
  {
    dictKey: 3,
    dictValue: "Excel上传OBS中",
  },
  {
    dictKey: 4,
    dictValue: "完成",
  },
  {
    dictKey: 99,
    dictValue: "异常",
  },
]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    code: "",
    departmentId: "",
    beginTime: "",
    endTime: "",
    type: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "select",
      prop: "type",
      label: "业务类型",
      data: [
        {
          dictKey: "1",
          dictValue: "对账单",
        },
        {
          dictKey: "2",
          dictValue: "对账单报表",
        },
        {
          dictKey: "3",
          dictValue: "采购入库",
        },
        {
          dictKey: "4",
          dictValue: "销售出货",
        },
      ],
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "文件名称",
        prop: "excelName",
      },
    },
    {
      attrs: {
        label: "状态",
        prop: "status",
        width: 160,
      },
      render(val) {
        return proxy.dictKeyValue(val, status.value);
      },
    },
    {
      attrs: {
        label: "创建时间",
        prop: "createTime",
        width: 160,
      },
    },
    {
      attrs: {
        label: "操作",
        width: 120,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          row.status == 4
            ? {
                attrs: {
                  label: "下载",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  clickDownload(row);
                },
              }
            : {},
          row.status == 4
            ? {
                attrs: {
                  label: "删除",
                  type: "danger",
                  text: true,
                },
                el: "button",
                click() {
                  clickDelete(row);
                },
              }
            : {},
        ];
      },
    },
  ];
});
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/excelGenerateLog/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickDownload = (row) => {
  fetch(row.obsFileUrl).then((res) =>
    res.blob().then((blob) => {
      var a = document.createElement("a");
      var url = window.URL.createObjectURL(blob);
      a.href = url;
      a.download = row.excelName; // 下载名称
      a.click();
      window.URL.revokeObjectURL(url);
    })
  );
};
const clickDelete = (row) => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.post("/excelGenerateLog/delete", { id: row.id }).then(() => {
        ElMessage({ message: "删除成功", type: "success" });
        getList();
      });
    })
    .catch(() => {});
};
</script>

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