<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 { orderTemplate } from "./components/BatchCreateModal/columns-config";
import { utils, writeFile } from 'xlsx';
import { useUserInfo } from "/@/hooks/core/useUser";

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

const { isSuperUser } = useUserInfo()

const visible = ref(false)

const contentRef = ref(null)

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(orderTemplate);
  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"
      :get-content-ref="ref => contentRef = ref"
    >
      <template #content_header>
        <ElButton type="primary" size="small"  @click="visible = true"  v-if="!isSuperUser">
          批量创建
        </ElButton>

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

    <BatchCreateModal 
      v-model:visible="visible" 
      @refresh="contentRef?.onSearch()" 
    />
  </PageAuth>
</template>
