ApprovalChainPreview.tsx 6.17 KB
import { InfoCircleFilled, CheckCircleFilled } from '@ant-design/icons';
import { UCard } from '@uj/ui';
import { useAppStore } from '@/store/useAppStore';

export type ChainNodeStatus = 'done' | 'active' | 'pending';

export interface ChainNode {
  status: ChainNodeStatus;
  title: string;
  subtitle: string;
  time?: string;
}

const DEFAULT_SUBMIT_TIME = '2026-09-20 15:12';

interface NodeIconProps {
  status: ChainNodeStatus;
  index: number;
}

function NodeIcon({ status, index }: NodeIconProps) {
  if (status === 'done') {
    return (
      <div
        style={{
          width: 32,
          height: 32,
          borderRadius: '50%',
          background: '#16A34A',
          display: 'inline-flex',
          alignItems: 'center',
          justifyContent: 'center',
          color: '#fff',
          flex: 'none',
        }}
      >
        <CheckCircleFilled style={{ fontSize: 18 }} />
      </div>
    );
  }
  if (status === 'active') {
    return (
      <div
        style={{
          width: 32,
          height: 32,
          borderRadius: '50%',
          background: '#2B5CE6',
          display: 'inline-flex',
          alignItems: 'center',
          justifyContent: 'center',
          color: '#fff',
          fontWeight: 600,
          fontSize: 13,
          flex: 'none',
          boxShadow: '0 0 0 4px #EEF2FF',
        }}
      >
        {index + 1}
      </div>
    );
  }
  return (
    <div
      style={{
        width: 32,
        height: 32,
        borderRadius: '50%',
        background: '#fff',
        border: '2px solid #D1D5DB',
        display: 'inline-flex',
        alignItems: 'center',
        justifyContent: 'center',
        color: '#9CA3AF',
        flex: 'none',
      }}
    >
      <CheckCircleFilled style={{ fontSize: 16 }} />
    </div>
  );
}

const lineColor = (status: ChainNodeStatus) => (status === 'done' ? '#16A34A' : '#E5E7EB');

function NodeLine({ status }: { status: ChainNodeStatus }) {
  return (
    <div
      style={{
        flex: 1,
        height: 2,
        background: lineColor(status),
        alignSelf: 'flex-start',
        marginTop: 15,
      }}
    />
  );
}

export interface ApprovalChainPreviewProps {
  nodes?: ChainNode[];
  /** (可选)覆盖默认的提交人名称。不传则取当前登录用户名 */
  submitterName?: string;
  /** (可选)覆盖默认的提交时间,格式 YYYY-MM-DD HH:mm */
  submitTime?: string;
}

/**
 * 生成默认 4 节点审批链:
 * - 第 1 个节点「提交」:副标题 = 当前登录用户名(外部 prop 覆盖优先);时间 = submitTime 或 DEFAULT_SUBMIT_TIME
 * - 第 2 个节点「优加商务经理」:当前审批节点(active)
 * - 第 3 个节点「优加财务经理」:下一审批节点(pending)
 * - 第 4 个节点「生效」:写入门店列表
 */
function buildDefaultNodes(submitter: string, time: string): ChainNode[] {
  return [
    {
      status: 'done',
      title: '提交',
      subtitle: submitter,
      time,
    },
    {
      status: 'active',
      title: '优加商务经理',
      subtitle: '当前审批节点',
    },
    {
      status: 'pending',
      title: '优加财务经理',
      subtitle: '下一审批节点',
    },
    {
      status: 'pending',
      title: '生效',
      subtitle: '写入门店列表',
    },
  ];
}

export function ApprovalChainPreview({
  nodes,
  submitterName,
  submitTime,
}: ApprovalChainPreviewProps) {
  const { currentUser } = useAppStore();
  const finalSubmitter = submitterName ?? currentUser.name ?? '管理员';
  const finalTime = submitTime ?? DEFAULT_SUBMIT_TIME;

  const finalNodes: ChainNode[] = nodes ?? buildDefaultNodes(finalSubmitter, finalTime);

  return (
    <UCard
      styles={{ body: { padding: '20px 24px 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>
      }
    >
      <div style={{ display: 'flex', padding: '20px 8px 8px' }}>
        {finalNodes.map((n, i) => {
          const prev = finalNodes[i - 1];
          const hasLeft = i > 0;
          const hasRight = i < finalNodes.length - 1;
          return (
            <div
              key={i}
              style={{
                flex: 1,
                display: 'flex',
                alignItems: 'flex-start',
              }}
            >
              <div style={{ flex: 1 }}>{hasLeft && <NodeLine status={prev.status} />}</div>
              <div
                style={{
                  display: 'flex',
                  flexDirection: 'column',
                  alignItems: 'center',
                  flex: 'none',
                }}
              >
                <NodeIcon status={n.status} index={i} />
                <div
                  style={{
                    marginTop: 14,
                    fontSize: 14,
                    fontWeight: 600,
                    color:
                      n.status === 'active'
                        ? '#2B5CE6'
                        : n.status === 'done'
                          ? '#16A34A'
                          : '#6B7280',
                    textAlign: 'center',
                  }}
                >
                  {n.title}
                </div>
                <div
                  style={{
                    marginTop: 4,
                    fontSize: 12,
                    color: '#9CA3AF',
                    textAlign: 'center',
                  }}
                >
                  {n.subtitle}
                </div>
                {n.time && (
                  <div
                    style={{
                      marginTop: 4,
                      fontSize: 11,
                      color: '#C7CDD6',
                      textAlign: 'center',
                    }}
                  >
                    {n.time}
                  </div>
                )}
              </div>
              <div style={{ flex: 1 }}>{hasRight && <NodeLine status={n.status} />}</div>
            </div>
          );
        })}
      </div>
    </UCard>
  );
}