<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 { httpRequsetExport } from "/@/utils/export";
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 { httpCompanyList } from "/@/api/dataDashboardNew/daily";

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() || {};

  console.log(params);

  if (!params.date) {
    ElMessage.warning("请选择导出文件的日期!");
    return;
  }

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

  await httpRequsetExport({
    url: "r_saleexportconsultdayexport",
    fileType: "aplication/zip",
    isPurchase: true,
    name: "日报.zip",
    ...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>
