<script setup lang="ts">
import { shallowRef, ref } from "vue";
import searchConfig from "./config/search.config";
import contentConfig from "./config/content.config";

import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { PageContentInstance } from "/@/components/PageContent";
import { usePageSearch, type PageHooks } from "/@/hooks/page";

import { httpRequsetExport } from "/@/utils/export";
import { isBeyondTime } from "../../_utils/isBeyondTime";

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

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

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

async function handleExportAllReport() {
  const params = pageContentRef.value.getBasicParams() || {};
  const { start_date, end_date } = params;
  const beyondTime = isBeyondTime({
    start: start_date,
    end: end_date,
    len: 30
  });

  if (beyondTime) return;

  await httpRequsetExport({
    fileType: "aplication/zip",
    name: "未签收报表.zip",
    url: "saleexportnotsignedexport",
    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>
