StoreDetailDrawer.tsx 7.3 KB
import type { ReactNode } from 'react';
import type { TableColumnsType } from 'antd';
import { Skeleton, UAlert, UButton, UDrawer, UResult, UTable, UTabs, UTag, UText } from '@uj/ui';
import type { StoreListingDetail, StoreListingItem } from '@/api';
import type { StoreDictionaries } from './dictionaries';
import { dictionaryLabel, formatPolicyValue } from './formatters';
import { ListingDetailHeader, ListingStoreInfo } from './ListingDetailOverview';
import { ListingChangeHistory } from './ListingChangeHistory';
import { ListingCostPanel } from './ListingCostPanel';
import { ApprovalPolicyPanel, ApprovalCostPanel } from './ApprovalPolicyPanels';
import {
  ApprovalDetailHeader,
  ApprovalStages,
  ApprovalAuditTrail,
} from './ApprovalDetailPresentation';

export interface StoreDetailDrawerProps {
  open: boolean;
  detail?: StoreListingDetail;
  spec?: StoreDictionaries;
  loading?: boolean;
  error?: unknown;
  mode: 'approval' | 'listing';
  initialTab?: string;
  pendingHours?: number | null;
  onClose: () => void;
  onRetry?: () => void;
  footer?: ReactNode;
}

type PolicyPrefix = 'marketFee1' | 'marketFee2' | 'sunshine1' | 'sunshine2';

function DetailContent({
  detail,
  spec,
  mode,
  initialTab,
  open,
  pendingHours,
}: Pick<StoreDetailDrawerProps, 'spec' | 'mode' | 'initialTab' | 'open' | 'pendingHours'> & {
  detail: StoreListingDetail;
}) {
  const items = (detail.items || []).map((item, index) => ({
    ...item,
    displayKey: String(item.itemId ?? index),
  }));
  const scene = dictionaryLabel(spec?.businessScenes, detail.businessScene);
  const categoryColumn: TableColumnsType<StoreListingItem>[number] = {
    title: '产品分类',
    key: 'category',
    width: 150,
    fixed: 'left',
    render: (_, item) => (
      <UTag>{dictionaryLabel(spec?.productCategories, item.productCategory)}</UTag>
    ),
  };
  const policyTable = (columns: TableColumnsType<StoreListingItem>) => (
    <UTable<StoreListingItem>
      rowKey="displayKey"
      size="small"
      pagination={false}
      columns={columns}
      dataSource={items}
      scroll={{ x: 'max-content' }}
    />
  );
  const compactPolicy = (item: StoreListingItem, prefix: PolicyPrefix) => (
    <div>
      {dictionaryLabel(spec?.calcModes, item[`${prefix}CalcMode`])} /{' '}
      {dictionaryLabel(spec?.calcWays, item[`${prefix}CalcWay`])} /{' '}
      {formatPolicyValue(item[`${prefix}Value`], item[`${prefix}Unit`] ?? undefined)}
      {(prefix === 'sunshine1' || prefix === 'sunshine2') && (
        <div>
          <UText type="secondary">
            开启线上积分:{dictionaryLabel(spec?.onlinePointFlags, item[`${prefix}OnlinePoint`])}
          </UText>
        </div>
      )}
    </div>
  );
  const approvalTabs = [
    {
      key: 'market',
      label: (
        <span>
          管理层市场费{' '}
          <span className="approval-detail-tab-count" aria-hidden="true">
            {detail.categoryCount ?? items.length}
          </span>
        </span>
      ),
      children: <ApprovalPolicyPanel detail={detail} spec={spec} kind="market" />,
    },
    {
      key: 'sunshine',
      label: '一线阳光激励',
      children: <ApprovalPolicyPanel detail={detail} spec={spec} kind="sunshine" />,
    },
    {
      key: 'costs',
      label: '运营成本',
      children: <ApprovalCostPanel detail={detail} spec={spec} />,
    },
    {
      key: 'logs',
      label: '审批留痕',
      children: <ApprovalAuditTrail detail={detail} spec={spec} pendingHours={pendingHours} />,
    },
  ];
  const listingTabs = [
    {
      key: 'info',
      label: '门店信息',
      children: <ListingStoreInfo detail={detail} spec={spec} />,
    },
    {
      key: 'products',
      label: '产品与场景',
      children: policyTable([
        categoryColumn,
        { title: '业务场景', key: 'scene', width: 200, render: () => scene },
        {
          title: '阳光激励 1 计算模式',
          key: 'mode1',
          width: 160,
          render: (_, item) => dictionaryLabel(spec?.calcModes, item.sunshine1CalcMode),
        },
        {
          title: '阳光激励 2 计算模式',
          key: 'mode2',
          width: 160,
          render: (_, item) => dictionaryLabel(spec?.calcModes, item.sunshine2CalcMode),
        },
      ]),
    },
    {
      key: 'policies',
      label: '市场与激励',
      children: policyTable([
        categoryColumn,
        ...(['marketFee1', 'marketFee2', 'sunshine1', 'sunshine2'] as const).map(
          (prefix, index) => ({
            title: ['管理层市场费 1', '管理层市场费 2', '一线阳光激励费 1', '一线阳光激励费 2'][
              index
            ],
            key: prefix,
            width: 240,
            render: (_: unknown, item: StoreListingItem) => compactPolicy(item, prefix),
          }),
        ),
      ]),
    },
    { key: 'costs', label: '运营成本', children: <ListingCostPanel detail={detail} /> },
    {
      key: 'history',
      label: '变更历史',
      children: <ListingChangeHistory history={detail.changeHistory} spec={spec} />,
    },
  ];
  const tabs = mode === 'approval' ? approvalTabs : listingTabs;
  const selectedTab = tabs.some((tab) => tab.key === initialTab) ? initialTab : tabs[0].key;
  return (
    <>
      {mode === 'approval' && <ApprovalStages detail={detail} />}
      {detail.rejectReason && (
        <UAlert
          type="warning"
          title="打回原因"
          description={detail.rejectReason}
          style={{ marginBottom: 16 }}
        />
      )}
      <UTabs
        key={`${detail.listingId}:${mode}:${initialTab}:${open}`}
        defaultActiveKey={selectedTab}
        items={tabs}
      />
    </>
  );
}

export function StoreDetailDrawer({
  open,
  detail,
  spec,
  loading,
  error,
  mode,
  initialTab,
  pendingHours,
  onClose,
  onRetry,
  footer,
}: StoreDetailDrawerProps) {
  const showCustomHeader = !!detail && !loading && !error;
  return (
    <UDrawer
      open={open}
      onClose={onClose}
      title={
        showCustomHeader ? (
          mode === 'listing' ? (
            <ListingDetailHeader detail={detail!} spec={spec} onClose={onClose} />
          ) : (
            <ApprovalDetailHeader detail={detail!} spec={spec} onClose={onClose} />
          )
        ) : mode === 'approval' ? (
          '门店审批详情'
        ) : (
          '门店详情'
        )
      }
      closable={showCustomHeader ? false : undefined}
      rootClassName={mode === 'listing' ? 'store-listing-detail' : 'store-approval-detail'}
      styles={
        mode === 'listing'
          ? { header: { padding: '16px 20px 20px' }, body: { padding: 20 } }
          : { header: { padding: '18px 22px' }, body: { padding: '20px 22px' } }
      }
      footer={footer}
    >
      {loading ? (
        <Skeleton variant="detail" />
      ) : error ? (
        <UResult
          status="error"
          title="详情加载失败"
          subTitle={error instanceof Error ? error.message : '请稍后重试'}
          extra={onRetry && <UButton onClick={onRetry}>重试</UButton>}
        />
      ) : detail ? (
        <DetailContent
          open={open}
          detail={detail}
          spec={spec}
          mode={mode}
          initialTab={initialTab}
          pendingHours={pendingHours}
        />
      ) : (
        <UResult status="info" title="暂无门店详情" />
      )}
    </UDrawer>
  );
}