SubmitConfirmCard.tsx 6.9 KB
import { useMemo } from 'react';
import { InfoCircleFilled, ExclamationCircleOutlined } from '@ant-design/icons';
import { UCard, UCheckbox, UTag } from '@uj/ui';
import type { StoreImportCheckVO } from '@/api/modules/store';
import { useAppStore } from '@/store/useAppStore';

export interface SubmitConfirmInfo {
  storeName: string;
  storeMerchantNo: string;
  categoryCount: number;
  sceneCount: number;
  totalRows: number;
  operatingCost: string;
  operatingCostDetail: string;
  approvalChain: string;
  approvalChainTag: string;
  approvalChainDesc: string;
  expectedApprover: string;
}

const labelStyle: React.CSSProperties = {
  width: 120,
  flex: 'none',
  fontSize: 13,
  color: '#8A94A6',
};

const valueStyle: React.CSSProperties = {
  fontSize: 13,
  color: '#1A202C',
  fontWeight: 500,
};

const descStyle: React.CSSProperties = {
  fontSize: 12,
  color: '#9CA3AF',
  marginLeft: 6,
};

function buildInfoFromCheckResult(vo: StoreImportCheckVO): SubmitConfirmInfo {
  const preview = vo.previewRows ?? [];
  const first = preview[0];
  const deco = Math.round(first?.costDecoration ?? 0);
  const monthly = Math.round(first?.costMonthly ?? 0);
  const labor = Math.round(first?.costLabor ?? 0);
  const total = deco + monthly + labor;
  const approvalRole = vo.nextRoleName ?? '-';
  return {
    storeName: vo.storeName ?? '-',
    storeMerchantNo: vo.merchantNo ?? '-',
    categoryCount: vo.distinctProductCategoryCount ?? 6,
    sceneCount: vo.distinctBusinessSceneCount ?? 2,
    totalRows: vo.productCategoryRowCount ?? vo.totalRows ?? 12,
    operatingCost: total > 0 ? `¥ ${total.toLocaleString('en-US')}` : '-',
    operatingCostDetail: `门店维度字段(装修设备 ${deco} + 当月产生 ${monthly} + 人工成本 ${labor})`,
    approvalChain: approvalRole,
    approvalChainTag: '两级审批',
    approvalChainDesc: '提交后直接进入两级审批,无中间节点',
    expectedApprover: `${approvalRole}`,
  };
}

export interface SubmitConfirmCardProps {
  info?: SubmitConfirmInfo;
  checkResult?: StoreImportCheckVO;
  checked?: boolean;
  onCheckedChange?: (checked: boolean) => void;
}

export function SubmitConfirmCard({
  info,
  checkResult,
  checked = true,
  onCheckedChange,
}: SubmitConfirmCardProps) {
  const { currentUser } = useAppStore();
  const userName = currentUser.name ?? '管理员';

  const fallbackInfo: SubmitConfirmInfo = useMemo(
    () => buildInfoFromCheckResult(checkResult ?? ({} as StoreImportCheckVO)),
    [checkResult, userName],
  );

  const finalInfo: SubmitConfirmInfo = info ?? fallbackInfo;

  /**
   * 红框两处「共 N 行」的取值:
   * - 只要存在校验失败(failRows>0)→ 本次提交的都是通过行 → 显示 passRows(通过行数,例如 11)
   * - 全通过(failRows=0)→ 显示 productCategoryRowCount / totalRows(总解析行数,例如 12)
   * - 不再依赖 onlyPass 字段(父组件可能不传),直接按业务结果决定
   */
  const displayRows =
    checkResult &&
    typeof checkResult.failRows === 'number' &&
    checkResult.failRows > 0 &&
    typeof checkResult.passRows === 'number'
      ? checkResult.passRows
      : finalInfo.totalRows;

  const infoRows: Array<{
    label: string;
    value: React.ReactNode;
    desc?: string;
    extra?: React.ReactNode;
  }> = [
    {
      label: '门店名称',
      value: finalInfo.storeName,
    },
    {
      label: '门店商户号',
      value: finalInfo.storeMerchantNo,
    },
    {
      label: '产品分类',
      value: `${finalInfo.categoryCount} 个`,
      desc: `(${finalInfo.sceneCount} 个业务场景,共 ${displayRows} 行)`,
    },
    {
      label: '运营成本合计',
      value: <span style={{ color: '#2B5CE6', fontWeight: 600 }}>{finalInfo.operatingCost}</span>,
      desc: finalInfo.operatingCostDetail,
    },
    {
      label: '触发审批链',
      value: finalInfo.approvalChain,
      extra: (
        <UTag color="processing" bordered={false} style={{ marginLeft: 6 }}>
          {finalInfo.approvalChainTag}
        </UTag>
      ),
      desc: finalInfo.approvalChainDesc,
    },
    {
      label: '预计审批人',
      value: finalInfo.expectedApprover,
    },
  ];

  return (
    <UCard
      styles={{ body: { padding: '20px 24px' } }}
      style={{ marginBottom: 20, border: '1px solid #E5E7EB', borderRadius: 10 }}
      title={
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <InfoCircleFilled style={{ color: '#2B5CE6', fontSize: 14 }} />
          <span style={{ fontSize: 15, fontWeight: 600 }}>提交确认</span>
        </div>
      }
    >
      {checkResult && (checkResult.failRows ?? 0) > 0 && (
        <div
          style={{
            padding: '10px 14px',
            borderRadius: 8,
            background: '#FFF7ED',
            border: '1px solid #FED7AA',
            display: 'flex',
            alignItems: 'flex-start',
            gap: 8,
            marginBottom: 16,
          }}
        >
          <ExclamationCircleOutlined
            style={{ color: '#EA580C', fontSize: 16, marginTop: 1, flex: 'none' }}
          />
          <div style={{ flex: 1, fontSize: 12, lineHeight: '20px' }}>
            <span style={{ color: '#9A3412', fontWeight: 600 }}>
              本次存在校验失败数据:共 {checkResult.failRows} 行失败。
            </span>
            <span style={{ color: '#7C2D12', marginLeft: 4 }}>
              本次将只提交「通过 {checkResult.passRows ?? 0} 行(listingIds 共{' '}
              {checkResult.listingIds?.length ?? 0}{' '}
              条)」进入审批,失败行需要您下载明细后修正再上传。
            </span>
          </div>
        </div>
      )}

      <div
        style={{
          padding: '14px 16px',
          background: '#F9FAFB',
          borderRadius: 8,
          marginBottom: 18,
        }}
      >
        <UCheckbox
          checked={checked}
          onChange={(e) => onCheckedChange?.(e.target.checked)}
          style={{ fontSize: 13, fontWeight: 500 }}
        >
          <span style={{ color: '#374151' }}>
            我已确认该门店 {finalInfo.sceneCount} 个业务场景共 {displayRows} 行数据准确无误
          </span>
        </UCheckbox>
        <div style={{ fontSize: 12, color: '#9CA3AF', marginTop: 6, paddingLeft: 24 }}>
          提交后如需调整,请在该门店「待审批」状态下撤回。
        </div>
      </div>

      <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
        {infoRows.map((r) => (
          <div key={r.label} style={{ display: 'flex', alignItems: 'flex-start' }}>
            <span style={labelStyle}>{r.label}</span>
            <div style={{ flex: 1, display: 'flex', alignItems: 'center', flexWrap: 'wrap' }}>
              <span style={valueStyle}>{r.value}</span>
              {r.extra}
              {r.desc && <span style={descStyle}>{r.desc}</span>}
            </div>
          </div>
        ))}
      </div>
    </UCard>
  );
}