<script setup lang="ts">
/**
 * 文件职责：财务中心经营报表页面入口编排。
 */
import { Page } from '@vben/common-ui';

import { Empty } from 'ant-design-vue';

import ReportBatchExportModal from './components/ReportBatchExportModal.vue';
import ReportPreviewDrawer from './components/ReportPreviewDrawer.vue';
import ReportTableCard from './components/ReportTableCard.vue';
import ReportToolbar from './components/ReportToolbar.vue';
import { useFinanceReportPage } from './composables/useFinanceReportPage';

const {
  canExport,
  canView,
  downloadPreviewExcel,
  downloadPreviewPdf,
  handleConfirmBatchExport,
  handlePageChange,
  isBatchExportModalOpen,
  isBatchExporting,
  isDownloadingExcel,
  isDownloadingPdf,
  isListLoading,
  isPreviewDrawerOpen,
  isPreviewLoading,
  isStoreLoading,
  openBatchExportModal,
  openPreview,
  pagination,
  periodType,
  previewDetail,
  rows,
  selectedStoreId,
  selectedStoreName,
  setBatchExportModalOpen,
  setPeriodType,
  setPreviewDrawerOpen,
  setSelectedStoreId,
  storeOptions,
} = useFinanceReportPage();
</script>

<template>
  <Page title="经营报表" content-class="page-finance-report">
    <div class="frp-page">
      <template v-if="canView">
        <ReportToolbar
          :period-type="periodType"
          :selected-store-id="selectedStoreId"
          :store-options="storeOptions"
          :is-store-loading="isStoreLoading"
          :can-export="canExport"
          :is-batch-exporting="isBatchExporting"
          @update:period-type="setPeriodType"
          @update:selected-store-id="setSelectedStoreId"
          @batch-export="openBatchExportModal"
        />

        <ReportTableCard
          :rows="rows"
          :loading="isListLoading"
          :pagination="pagination"
          :can-export="canExport"
          @page-change="handlePageChange"
          @preview="openPreview"
          @download-pdf="downloadPreviewPdf"
          @download-excel="downloadPreviewExcel"
        />
      </template>

      <Empty v-else description="暂无经营报表页面访问权限" />
    </div>

    <ReportPreviewDrawer
      :open="isPreviewDrawerOpen"
      :loading="isPreviewLoading"
      :detail="previewDetail"
      :can-export="canExport"
      :is-downloading-pdf="isDownloadingPdf"
      :is-downloading-excel="isDownloadingExcel"
      @close="setPreviewDrawerOpen(false)"
      @download-pdf="downloadPreviewPdf"
      @download-excel="downloadPreviewExcel"
    />

    <ReportBatchExportModal
      :open="isBatchExportModalOpen"
      :submitting="isBatchExporting"
      :selected-store-name="selectedStoreName"
      :period-type="periodType"
      :pagination="pagination"
      :current-page-count="rows.length"
      @close="setBatchExportModalOpen(false)"
      @confirm="handleConfirmBatchExport"
    />
  </Page>
</template>

<style lang="less">
@import './styles/index.less';
</style>
