<script setup lang="ts">
import type { DineInAreaDto } from '#/api/store-dinein';

/**
 * 文件职责：堂食管理页面主视图。
 * 1. 组合区域、桌位、堂食设置与抽屉/弹窗子组件。
 * 2. 承接门店维度切换与复制弹窗。
 */
import { Page } from '@vben/common-ui';

import { Card, Empty, message, Spin } from 'ant-design-vue';

import CopyToStoresModal from '../../shared/components/CopyToStoresModal.vue';
import StoreScopeToolbar from '../../shared/components/StoreScopeToolbar.vue';
import DineInAreaDrawer from './components/DineInAreaDrawer.vue';
import DineInAreaSection from './components/DineInAreaSection.vue';
import DineInBasicSettingsCard from './components/DineInBasicSettingsCard.vue';
import DineInBatchModal from './components/DineInBatchModal.vue';
import DineInTableDrawer from './components/DineInTableDrawer.vue';
import DineInTableGridSection from './components/DineInTableGridSection.vue';
import { useStoreDineInPage } from './composables/useStoreDineInPage';

const {
  DINE_IN_SEATS_OPTIONS,
  DINE_IN_STATUS_MAP,
  TABLE_TAG_SUGGESTIONS,
  areaDrawerTitle,
  areaForm,
  areaOptions,
  areaSubmitText,
  areas,
  basicSettings,
  batchForm,
  batchPreviewCodes,
  canOperate,
  copyCandidates,
  copyTargetStoreIds,
  filteredTables,
  handleCopyCheckAll,
  handleCopySubmit,
  handleDeleteArea,
  handleDeleteTable,
  handleSubmitArea,
  handleSubmitBatch,
  handleSubmitTable,
  hasLoadedStoreSettings,
  isAreaDrawerOpen,
  isBatchModalOpen,
  isCopyAllChecked,
  isCopyIndeterminate,
  isCopyModalOpen,
  isCopySubmitting,
  isConfigured,
  isPageLoading,
  isSavingArea,
  isSavingBasic,
  isSavingBatch,
  isSavingTable,
  isStoreLoading,
  isTableDrawerOpen,
  openAreaDrawer,
  openBatchModal,
  openCopyModal,
  openTableDrawer,
  resetBasicSettings,
  resolveStatusClassName,
  saveBasicSettings,
  selectedArea,
  selectedAreaId,
  selectedStoreId,
  selectedStoreName,
  setAreaDescription,
  setAreaDrawerOpen,
  setAreaName,
  setAreaSort,
  setBatchAreaId,
  setBatchCodePrefix,
  setBatchCount,
  setBatchModalOpen,
  setBatchSeats,
  setBatchStartNumber,
  setDefaultDiningMinutes,
  setDineInEnabled,
  setOvertimeReminderMinutes,
  setSelectedAreaId,
  setSelectedStoreId,
  setTableAreaId,
  setTableCode,
  setTableDisabled,
  setTableDrawerOpen,
  setTableSeats,
  setTableTags,
  storeOptions,
  tableDrawerTitle,
  tableForm,
  tableSubmitText,
  tables,
  toggleCopyStore,
} = useStoreDineInPage();

/** 统计指定区域桌位数。 */
function getAreaTableCount(areaId: string) {
  return tables.value.filter((item) => item.areaId === areaId).length;
}

/** 桌位状态文案映射。 */
const tableStatusLabelMap = {
  free: DINE_IN_STATUS_MAP.free.label,
  dining: DINE_IN_STATUS_MAP.dining.label,
  reserved: DINE_IN_STATUS_MAP.reserved.label,
  disabled: DINE_IN_STATUS_MAP.disabled.label,
};

/** 当前区域删除处理。 */
function onDeleteSelectedArea(area: DineInAreaDto) {
  handleDeleteArea(area);
}

/** 二维码按钮占位处理。 */
function onViewQrCode(tableCode: string) {
  message.info(`桌位 ${tableCode} 二维码功能待接入`);
}
</script>

<template>
  <Page title="堂食管理" content-class="space-y-4 page-store-dinein">
    <StoreScopeToolbar
      :selected-store-id="selectedStoreId"
      :store-options="storeOptions"
      :is-store-loading="isStoreLoading"
      :copy-disabled="
        !canOperate || !isConfigured || copyCandidates.length === 0
      "
      @update:selected-store-id="setSelectedStoreId"
      @copy="openCopyModal"
    />

    <template v-if="storeOptions.length === 0">
      <Card :bordered="false">
        <Empty description="暂无门店，请先创建门店" />
      </Card>
    </template>

    <template v-else>
      <Spin :spinning="isPageLoading">
        <Card v-if="hasLoadedStoreSettings && !isConfigured" :bordered="false">
          <Empty
            description="当前门店尚未配置堂食规则。请先填写并保存，之后可执行复制。"
          />
        </Card>

        <DineInAreaSection
          :can-operate="canOperate"
          :areas="areas"
          :selected-area-id="selectedAreaId"
          :selected-area="selectedArea"
          :is-saving="isSavingArea"
          :get-area-table-count="getAreaTableCount"
          @add="openAreaDrawer('create')"
          @edit="(area) => openAreaDrawer('edit', area)"
          @delete="onDeleteSelectedArea"
          @select-area="setSelectedAreaId"
        />

        <DineInTableGridSection
          :can-operate="canOperate"
          :has-selected-area="Boolean(selectedArea)"
          :tables="filteredTables"
          :is-saving="isSavingTable"
          :status-map="DINE_IN_STATUS_MAP"
          :resolve-status-class-name="resolveStatusClassName"
          @add="openTableDrawer('create')"
          @batch="openBatchModal"
          @edit="(table) => openTableDrawer('edit', table)"
          @delete="handleDeleteTable"
          @qrcode="(table) => onViewQrCode(table.code)"
        />

        <DineInBasicSettingsCard
          :can-operate="canOperate"
          :settings="basicSettings"
          :is-saving="isSavingBasic"
          :on-set-enabled="setDineInEnabled"
          :on-set-default-dining-minutes="setDefaultDiningMinutes"
          :on-set-overtime-reminder-minutes="setOvertimeReminderMinutes"
          @save="saveBasicSettings"
          @reset="resetBasicSettings"
        />
      </Spin>
    </template>

    <DineInAreaDrawer
      :open="isAreaDrawerOpen"
      :title="areaDrawerTitle"
      :submit-text="areaSubmitText"
      :form="areaForm"
      :is-saving="isSavingArea"
      :on-set-name="setAreaName"
      :on-set-description="setAreaDescription"
      :on-set-sort="setAreaSort"
      @update:open="setAreaDrawerOpen"
      @submit="handleSubmitArea"
    />

    <DineInTableDrawer
      :open="isTableDrawerOpen"
      :title="tableDrawerTitle"
      :submit-text="tableSubmitText"
      :form="tableForm"
      :is-saving="isSavingTable"
      :area-options="areaOptions"
      :seats-options="DINE_IN_SEATS_OPTIONS"
      :status-label-map="tableStatusLabelMap"
      :tag-suggestions="TABLE_TAG_SUGGESTIONS"
      :on-set-code="setTableCode"
      :on-set-area-id="setTableAreaId"
      :on-set-seats="setTableSeats"
      :on-set-disabled="setTableDisabled"
      :on-set-tags="setTableTags"
      @update:open="setTableDrawerOpen"
      @submit="handleSubmitTable"
    />

    <DineInBatchModal
      :open="isBatchModalOpen"
      :form="batchForm"
      :is-saving="isSavingBatch"
      :area-options="areaOptions"
      :seats-options="DINE_IN_SEATS_OPTIONS"
      :preview-codes="batchPreviewCodes"
      :on-set-area-id="setBatchAreaId"
      :on-set-code-prefix="setBatchCodePrefix"
      :on-set-start-number="setBatchStartNumber"
      :on-set-count="setBatchCount"
      :on-set-seats="setBatchSeats"
      @update:open="setBatchModalOpen"
      @submit="handleSubmitBatch"
    />

    <CopyToStoresModal
      v-model:open="isCopyModalOpen"
      title="复制堂食设置到其他门店"
      confirm-text="确认复制"
      :copy-candidates="copyCandidates"
      :target-store-ids="copyTargetStoreIds"
      :is-all-checked="isCopyAllChecked"
      :is-indeterminate="isCopyIndeterminate"
      :is-submitting="isCopySubmitting"
      :selected-store-name="selectedStoreName"
      @check-all="handleCopyCheckAll"
      @submit="handleCopySubmit"
      @toggle-store="
        ({ storeId, checked }) => toggleCopyStore(storeId, checked)
      "
    />
  </Page>
</template>

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