<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";
import { ElMessage } from "element-plus";

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, start_arrive_date, end_arrive_date } = params;

  if (!start_arrive_date && !end_arrive_date && !start_date && !end_date) {
    ElMessage.warning("请选择至少一种导出文件的时间区间");
    return;
  }

  if (start_arrive_date && end_arrive_date) {
    const beyondTime = isBeyondTime({
      start: start_arrive_date,
      end: end_arrive_date,
      len: 30
    });
    if (beyondTime) return;
  }

  if (start_date && end_date) {
    const beyondTime = isBeyondTime({
      start: start_date,
      end: end_date,
      len: 30
    });
    if (beyondTime) return;
  }

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