<script setup lang="ts">
import { shallowRef, ref } from "vue";
import { ElMessage } from "element-plus";
import searchConfig from "./config/search.config";
import contentConfig from "./config/content.config";
import { httpRequsetExport } from "/@/utils/export";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { usePageSearch, type PageHooks } from "/@/hooks/page";
import { PageContentInstance } from "/@/components/PageContent";
import dayjs from "dayjs";

const pageContentRef = ref<PageContentInstance | null>(null);
const loading = shallowRef(false);

const hooks: PageHooks = {
  pageSearchHook: () => usePageSearch(undefined, undefined, searchConfig)
};

const callback = {
  onSuccess: () => (loading.value = false),
  onStart: () => (loading.value = true),
  onFail: () => (loading.value = false)
};

async function handleExportAllReport() {
  const params = pageContentRef.value.getBasicParams() || {};

  if (!params.end_date || !params.start_date) {
    ElMessage.warning("请选择导出文件的时间区间!");
    return;
  }

  const diffDays = dayjs(params.end_date).diff(params.start_date, "days");

  if (diffDays > 30) {
    ElMessage.warning("导出文件的时间区间不能超过30天");
    return;
  }

  await httpRequsetExport({
    url: "saleexportconsultinfoexport",
    fileType: "aplication/zip",
    name: "竞价单情况表.zip",
    isPurchase: true,
    ...callback,
    params
  });
}
</script>

<template>
  <PageContainer
    :hooks="hooks"
    :getContentRef="ref => (pageContentRef = ref)"
    :content-config="contentConfig"
    :search-config="searchConfig"
  >
    <template #content_header>
      <el-button
        @click="handleExportAllReport"
        size="small"
        :icon="useRenderIcon('arrow-up-line')"
        type="primary"
        >导出</el-button
      >
    </template>
  </PageContainer>
</template>
