<script setup lang="ts">
import { shallowRef, ref } from "vue";
import { ElMessage } from "element-plus";

import sourceSearchConfig from "./config/search.config";
import sourceContentConfig from "./config/content.config";

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

import { useBusinessSearch, useBusinessContent } from "./../../_hooks";
import { createHttpRequset } from "./../../_http/createRequset";
import { httpRequsetExport } from "/@/utils/export";
import { isBeyondTime } from "./../../_utils";

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

const { searchConfig } = useBusinessSearch({
  sourceConfig: sourceSearchConfig
});

const { contentConfig } = useBusinessContent({
  sourceConfig: sourceContentConfig,
  apis: {
    httpList: createHttpRequset("r_saleexportlist")
  }
});

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() || {};
  const { start_date, end_date } = params;

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

  const beyondTime = isBeyondTime({
    start: start_date,
    end: end_date,
    len: 10
  });

  if (beyondTime) return;

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

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