<script setup lang="ts">
import { ref } from "vue";
import contentConfig from "./config/content.config";
import searchConfig from "./config/search.config";
import { usePageSearch, type PageHooks, type PageEvents } from "/@/hooks/page";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import InvoiceModal from "./components/invoice-modal.vue";
import { useRouter } from "vue-router";
import { httpRequsetExport } from "/@/utils/export";
import { useCompany } from "/@/hooks/core/useCompany";
import { PageContent } from "/@/components/PageContent";
import ExeclUpoad from "./components/execl-files-upload/index.vue";
import { usePermission } from "/@/hooks/core/usePermission";
import { useUserInfo } from "/@/hooks/core/useUser";

const PageName = "supplyTicketReturn";
const baseUrl = "/supply/supplyTicketReturnDetail";
const invStatus = ["4", "10"];
const loading = ref(false);

// { code: "032", name: "导出对账开票数据" },
// { code: "033", name: "批量创建开票数据" }
const { hasPermissionWithCode } = usePermission(PageName);
const { isSuperUser } = useUserInfo();

const { push } = useRouter();
const invoiceModalRef = ref<InstanceType<typeof InvoiceModal>>(null);
const execelUploadRef = ref<InstanceType<typeof ExeclUpoad>>(null);
const pageContentRef = ref<InstanceType<typeof PageContent>>(null);

const { currentCompany } = useCompany();

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

const events: PageEvents = {
  content: {
    preview: ({ hpNo }) => push(`${baseUrl}?id=${hpNo}`),
    create: () => push(baseUrl)
  }
};

async function handleDownload() {
  await httpRequsetExport({
    url: "ile",
    name: "开票数据表",
    onStart: () => (loading.value = true),
    onSuccess: () => (loading.value = false),
    onFail: () => (loading.value = false),
    params: {
      supplierNo: currentCompany.value.companyNo,
      ...(pageContentRef.value as any).getBasicParams()
    }
  });
}
</script>

<template>
  <PageAuth :pageName="PageName">
    <PageContainer
      :hooks="hooks"
      :events="events"
      :contentConfig="contentConfig"
      :search-config="searchConfig"
      :get-content-ref="(ref:any) => pageContentRef = ref"
    >
      <template #content_header>
        <ElButton
          size="small"
          v-if="hasPermissionWithCode('032')"
          :icon="useRenderIcon('arrow-down-line')"
          :loading="loading"
          @click="() => handleDownload()"
          >导出对账开票数据</ElButton
        >

        <ElButton
          size="small"
          v-if="hasPermissionWithCode('033') && !isSuperUser"
          :icon="useRenderIcon('arrow-up-line')"
          @click="() => execelUploadRef.onDisplay()"
          >批量创建开票数据</ElButton
        >
      </template>

      <template #content_action="row">
        <ElButton
          v-if="invStatus.includes(String(row.status))"
          link
          type="primary"
          :icon="useRenderIcon('scaletooriginal')"
          @click="() => invoiceModalRef.onDisplay(row)"
        />
      </template>
    </PageContainer>

    <InvoiceModal ref="invoiceModalRef" />
    <ExeclUpoad
      ref="execelUploadRef"
      @on-success="() => (pageContentRef as any).onSearch()"
    />
  </PageAuth>
</template>

<style lang="scss" scoped>
:deep(.el-upload-list__item) {
  display: none !important;
}
</style>
