<script setup lang="ts">
import { usePageSearch, type PageHooks, type PageEvents } from "/@/hooks/page";
import contentConfig from "./config/content.config";
import searchConfig from "./config/search.config";
import { useRouter } from "vue-router";
import BatchCreateModal from "./components/BatchCreateModal/index.vue"
import { ref } from "vue";
import { batchCreateTemplate } from "./components/BatchCreateModal/columns-config";
import { utils, writeFile } from 'xlsx';



const PageName = "productManager";
const router = useRouter();


const visible = ref(false)

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

const events: PageEvents = {
  content: {
    preview: ({ id }) => router.push("/invoiceInOut/inventoryCheckDetail?id=" + id),
    create: () => router.push('/invoiceInOut/inventoryCheckDetail')
  }
};

async function onDownload(){
  const workBook = utils.book_new();
  const workSheet = utils.json_to_sheet(batchCreateTemplate);
  utils.book_append_sheet(workBook, workSheet, "sheet");
  //导出模板
  writeFile(workBook, `盘点模板.xlsx`, { bookType: "xlsx" });
  // const result = await utils.book_new()
}
</script>

<template>
  <PageAuth :pageName="PageName">
    <PageContainer
      :hooks="hooks"
      :events="events"
      :searchConfig="searchConfig"
      :contentConfig="contentConfig"
    >
      <template #content_header>
        <ElButton type="primary" size="small"  @click="visible = true" >
          批量创建
        </ElButton>

        <ElButton size="small" @click="onDownload">
          下载模板
        </ElButton>
      </template>
    </PageContainer>

    <BatchCreateModal v-model:visible="visible" />
  </PageAuth>
</template>
