ApprovalPolicyPanels.tsx 6.43 KB
import type { TableColumnsType } from 'antd';
import { UTable } from '@uj/ui';
import type { StoreListingDetail, StoreListingItem } from '@/api';
import type { StoreDictionaries } from './dictionaries';
import { dictionaryLabel, formatMoney, formatPolicyValue } from './formatters';
import './approvalPolicy.css';

interface PanelProps {
  detail: StoreListingDetail;
  spec?: StoreDictionaries;
}

type PolicyPrefix = 'marketFee1' | 'marketFee2' | 'sunshine1' | 'sunshine2';
type PolicyRow = StoreListingItem & { displayKey: string };

function categoryCount(detail: StoreListingDetail): number | undefined {
  if (
    typeof detail.categoryCount === 'number' &&
    Number.isSafeInteger(detail.categoryCount) &&
    detail.categoryCount >= 0
  ) {
    return detail.categoryCount;
  }
  if (!detail.items) return undefined;
  return new Set(detail.items.map((item) => item.productCategory?.trim()).filter(Boolean)).size;
}

export function ApprovalPolicyPanel({
  detail,
  spec,
  kind,
}: PanelProps & { kind: 'market' | 'sunshine' }) {
  const market = kind === 'market';
  const name = market ? '管理层市场费' : '一线阳光激励';
  const prefixes: PolicyPrefix[] = market
    ? ['marketFee1', 'marketFee2']
    : ['sunshine1', 'sunshine2'];
  const columns: TableColumnsType<PolicyRow> = [
    {
      title: '产品分类',
      key: 'category',
      width: market ? 120 : 110,
      fixed: 'left',
      render: (_, item) => (
        <span className="approval-policy-panel__category">
          {dictionaryLabel(spec?.productCategories, item.productCategory)}
        </span>
      ),
    },
    ...prefixes.flatMap((prefix, index): TableColumnsType<PolicyRow> => [
      ...(prefix === 'sunshine1' || prefix === 'sunshine2'
        ? [
            {
              title: '开启线上积分',
              key: `${prefix}OnlinePoint`,
              width: 100,
              render: (_: unknown, item: PolicyRow) =>
                dictionaryLabel(spec?.onlinePointFlags, item[`${prefix}OnlinePoint`]),
            },
          ]
        : []),
      {
        title: `${market ? '市场费' : '激励'}${index + 1}计算模式`,
        key: `${prefix}CalcMode`,
        width: market ? 110 : 100,
        render: (_, item) => dictionaryLabel(spec?.calcModes, item[`${prefix}CalcMode`]),
      },
      {
        title: '计算方式',
        key: `${prefix}CalcWay`,
        width: market ? 90 : 80,
        render: (_, item) => dictionaryLabel(spec?.calcWays, item[`${prefix}CalcWay`]),
      },
      {
        title: `${market ? '市场费' : '激励费'}${index + 1}`,
        key: `${prefix}Value`,
        width: 80,
        align: 'right',
        render: (_, item) => (
          <span className="approval-policy-panel__value">
            {formatPolicyValue(item[`${prefix}Value`], item[`${prefix}Unit`] ?? undefined)}
          </span>
        ),
      },
    ]),
  ];
  const rows = (detail.items || []).map((item, index) => ({
    ...item,
    displayKey: `${item.itemId ?? 'item'}-${index}`,
  }));
  const count = categoryCount(detail);

  return (
    <section className="approval-policy-panel" aria-label={`${name}政策`}>
      <div className="approval-policy-panel__table">
        <UTable<PolicyRow>
          rowKey="displayKey"
          size="small"
          pagination={false}
          columns={columns}
          dataSource={rows}
          scroll={{ x: 'max-content' }}
        />
      </div>
      <p className="approval-policy-panel__note">
        本页签展示本审批记录({detail.storeName || '—'} ·{' '}
        {dictionaryLabel(spec?.businessScenes, detail.businessScene)})下
        {count === undefined ? '的产品分类' : ` ${count} 个产品分类`}的{name}
        政策,仅展示,不参与逐项勾选。
      </p>
      {!market && (
        <p className="approval-policy-panel__note">
          「开启线上积分」按记录中的是否值展示,激励费按各项政策的计算方式与单位展示。
        </p>
      )}
    </section>
  );
}

interface CostRow {
  key: string;
  name: string;
  value: string;
  description: string;
  total?: boolean;
}

export function ApprovalCostPanel({ detail, spec }: PanelProps) {
  // The heading supplies the currency unit. All digits still come from the shared formatter.
  const amount = (value: StoreListingDetail['costTotal']) => formatMoney(value).replace(/^¥/, '');
  const rows: CostRow[] = [
    {
      key: 'decoration',
      name: '装修、设备总投入',
      value: amount(detail.costDecoration),
      description: '门店装修与设备投入',
    },
    {
      key: 'monthly',
      name: '当月产生(房租、水电、耗材等)',
      value: amount(detail.costMonthly),
      description: '门店当月房租、水电、耗材等费用',
    },
    {
      key: 'labor',
      name: '人工成本',
      value: amount(detail.costLabor),
      description: '门店人工成本',
    },
    {
      key: 'total',
      name: '运营成本合计',
      value: amount(detail.costTotal),
      description: '门店维度的运营成本合计',
      total: true,
    },
    {
      key: 'categories',
      name: '本场景产品分类行数',
      value: String(categoryCount(detail) ?? '—'),
      description: dictionaryLabel(spec?.businessScenes, detail.businessScene),
    },
  ];
  const columns: TableColumnsType<CostRow> = [
    {
      title: '项目',
      dataIndex: 'name',
      key: 'name',
      width: 240,
      render: (value: string) => <span className="approval-cost-panel__name">{value}</span>,
    },
    {
      title: '金额(元)',
      dataIndex: 'value',
      key: 'value',
      width: 130,
      align: 'right',
      render: (value: string, row) =>
        row.total ? <strong className="approval-cost-panel__total">{value}</strong> : value,
    },
    {
      title: '说明',
      dataIndex: 'description',
      key: 'description',
      className: 'approval-cost-panel__description',
    },
  ];

  return (
    <section className="approval-policy-panel approval-cost-panel" aria-label="运营成本明细">
      <div className="approval-policy-panel__table">
        <UTable<CostRow>
          rowKey="key"
          size="small"
          pagination={false}
          columns={columns}
          dataSource={rows}
          scroll={{ x: 'max-content' }}
        />
      </div>
      <p className="approval-policy-panel__note">
        运营成本为门店维度,不随产品分类数或业务场景数重复累计。
      </p>
    </section>
  );
}