<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { usePageSearch, type PageHooks, type PageEvents } from "/@/hooks/page";
import NotOrderImportModal from "./components/NotOrderImportModal/index.vue"
import OrderImportModal from "./components/OrderImportModal/index.vue"
import { notOrderTemplate } from "./components/NotOrderImportModal/columns-config"
import { orderTemplate } from "./components/OrderImportModal/columns-config"
import { useRenderIcon } from "/@/components/ReIcon/src/hooks"
import contentConfig from "./config/content.config";
import searchConfig from "./config/search.config";
import { utils, writeFile } from "xlsx";


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

const state = reactive({ orderImportVisible: false, notOrderImportVisible: false })
const hooks: PageHooks = { pageSearchHook: () => usePageSearch(undefined, undefined, searchConfig) };

const pageContentRef = ref(null)

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

function onDownload(type = 'notOrder'){
  const data = type === 'notOrder' ? notOrderTemplate : orderTemplate
  const title = type === 'notOrder' ? '非订单商品模板.xlsx' : '订单商品模板.xlsx'
  const workBook = utils.book_new();
  const workSheet = utils.json_to_sheet(data);
  utils.book_append_sheet(workBook, workSheet, "sheet");
  writeFile(workBook, title, { bookType: "xlsx" });
}
</script>

<template>
  <PageAuth :pageName="PageName">
    <PageContainer
      :hooks="hooks"
      :events="events"
      :searchConfig="searchConfig"
      :contentConfig="contentConfig"
      :get-content-ref="ref => pageContentRef = ref"
    >
      <template #content_header>
        <ElDropdown>
          <ElButton 
            size="small" 
            type="primary"
            style="margin-right: 10px;margin-top:1px"
            :icon="useRenderIcon('arrow-up-line')"
          >商品导入
          </ElButton>
          
          <template #dropdown>
            <ElDropdownMenu>
              <ElDropdownItem @click="state.orderImportVisible = true">订单商品</ElDropdownItem>
              <ElDropdownItem @click="state.notOrderImportVisible = true">非订单商品</ElDropdownItem>
            </ElDropdownMenu>
          </template>
        </ElDropdown>

        <ElDropdown>
          <ElButton 
            size="small" 
            style="margin-right: 10px;margin-top:1px"
            :icon="useRenderIcon('arrow-down-line')"
          >商品导入模板下载</ElButton>
          
          <template #dropdown>
            <ElDropdownMenu>
              <ElDropdownItem @click="onDownload('order')">订单商品</ElDropdownItem>
              <ElDropdownItem @click="onDownload('notOrder')">非订单商品</ElDropdownItem>
            </ElDropdownMenu>
          </template>
        </ElDropdown>
      </template>
      <!-- <template #content_action>
        <ElTooltip content="取消操作" placement="top">
          <ElButton 
            :icon="useRenderIcon('delete')"
            type="danger"
            link
          />
        </ElTooltip>
      </template> -->
    </PageContainer>

    <OrderImportModal 
      v-model:visible="state.orderImportVisible"
      @refresh="pageContentRef?.onSearch()"
    />

    <NotOrderImportModal 
      v-model:visible="state.notOrderImportVisible"
      @refresh="pageContentRef?.onSearch()"
    />
  </PageAuth>
</template>
