<script setup lang="ts">
import { shallowRef, ref } from "vue";
import { ElMessage } from "element-plus";
import sourceContentConfig from "./config/content.config";
import { httpRequsetExport } from "/@/utils/export";
import sourceSearchConfig from "./config/search.config";
import { usePageSearch, type PageHooks } from "/@/hooks/page";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { PageContentInstance } from "/@/components/PageContent";
import { httpCompanyList } from "/@/api/dataDashboardNew/bidOrder";
import { useBusinessSearch, useBusinessContent } from "./../../_hooks";
import dayjs from "dayjs";

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

const lockKey = "relaComNo";
const { searchConfig } = useBusinessSearch({
  sourceConfig: sourceSearchConfig
});
const { contentConfig } = useBusinessContent({
  sourceConfig: sourceContentConfig,
  apis: { httpList: httpCompanyList }
});

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

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

async function handleExportAllReport() {
  const params = pageContentRef.value.getBasicParams() || {};
  if (!params.start_date && !params.end_date) {
    ElMessage.warning("请选择导出文件的时间区间!");
    return;
  }

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

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

  if (!params[lockKey]) {
    ElMessage.warning("请选择业务公司");
    return;
  }

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

<template>
  <PageContainer
    :hooks="hooks"
    :lockKey="lockKey"
    :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>
