ApprovalActionModal.tsx 5.59 KB
import { UAlert, UButton, UCheckbox, UForm, UFormItem, UInput, UModal, UTag } from '@uj/ui';
import type { ApprovalRow } from '@/api';
import type { StoreDictionaries } from '@/features/store/dictionaries';
import { dictionaryLabel, formatMoney } from '@/features/store/formatters';
import { rejectReasons } from '../tools/consts';

export type ApprovalActionType = 'pass' | 'reject' | 'batch' | 'revoke';
export interface ApprovalAction {
  type: ApprovalActionType;
  records: ApprovalRow[];
}
const titles: Record<ApprovalActionType, string> = {
  pass: '确认通过',
  reject: '打回审批',
  batch: '批量审批确认',
  revoke: '确认撤回',
};
const buttons: Record<ApprovalActionType, string> = {
  pass: '确认通过',
  reject: '确认打回',
  batch: '确认批量通过',
  revoke: '确认撤回',
};

function approvalResultMessage(records: ApprovalRow[]) {
  const hasUnknownLevel = records.some(
    ({ currentLevel, totalLevel }) =>
      typeof currentLevel !== 'number' ||
      typeof totalLevel !== 'number' ||
      !Number.isInteger(currentLevel) ||
      !Number.isInteger(totalLevel) ||
      currentLevel < 1 ||
      totalLevel < currentLevel,
  );
  if (!records.length || hasUnknownLevel) {
    return '通过当前审批节点后,系统将按记录的审批进度处理;完成全部审批节点后生效并进入门店列表。';
  }
  const finalCount = records.filter((row) => row.currentLevel === row.totalLevel).length;
  if (finalCount === records.length) {
    return '通过当前审批节点后,记录将立即生效并进入门店列表。';
  }
  if (finalCount > 0) {
    return '本次审批包含不同审批阶段的记录:终审记录通过后将立即生效并进入门店列表;其余记录将继续后续审批,全部节点通过后生效。';
  }
  return '通过当前审批节点后,记录将继续后续审批;全部节点通过后才会生效并进入门店列表。';
}

interface Props {
  action?: ApprovalAction;
  spec?: StoreDictionaries;
  pending: boolean;
  confirmed: boolean;
  reason: string;
  error?: string;
  onConfirmed: (value: boolean) => void;
  onReason: (value: string) => void;
  onCancel: () => void;
  onConfirm: () => void;
}
export function ApprovalActionModal({
  action,
  spec,
  pending,
  confirmed,
  reason,
  error,
  onConfirmed,
  onReason,
  onCancel,
  onConfirm,
}: Props) {
  const type = action?.type ?? 'pass';
  const needsConfirmation = type === 'pass' || type === 'batch';
  return (
    <UModal
      open={!!action}
      title={titles[type]}
      width={600}
      okText={buttons[type]}
      cancelText="取消"
      onCancel={onCancel}
      onOk={onConfirm}
      confirmLoading={pending}
      closable={!pending}
      maskClosable={!pending}
      keyboard={!pending}
      cancelButtonProps={{ disabled: pending }}
      okButtonProps={{
        disabled: needsConfirmation && !confirmed,
        danger: type === 'reject' || type === 'revoke',
      }}
    >
      {type === 'pass' || type === 'batch' ? (
        <UAlert
          type="info"
          showIcon
          title="审批范围为门店+业务场景下的全部产品分类"
          description={approvalResultMessage(action?.records ?? [])}
        />
      ) : type === 'revoke' ? (
        <UAlert type="warning" showIcon title="撤回后记录将退出当前审批流程。" />
      ) : null}
      <div className="store-confirm-records">
        {action?.records.map((row) => (
          <div className="store-confirm-record" key={row.listingId}>
            <strong>{row.storeName}</strong>
            <span className="store-subtext">商户号:{row.merchantNo}</span>
            <div style={{ marginTop: 8 }}>
              <UTag color="blue">{dictionaryLabel(spec?.businessScenes, row.businessScene)}</UTag>
              <span>{row.categoryCount ?? row.productCategories?.length ?? 0} 个产品分类</span>
            </div>
            {needsConfirmation && (
              <>
                <div style={{ marginTop: 8 }}>运营成本合计:{formatMoney(row.costTotal)}</div>
                <span className="store-subtext">
                  {row.productCategories
                    ?.map((category) => dictionaryLabel(spec?.productCategories, category))
                    .join('、')}
                </span>
              </>
            )}
          </div>
        ))}
      </div>
      {type === 'reject' && (
        <UForm layout="vertical">
          <UFormItem label="打回原因" required>
            <UInput.TextArea
              aria-label="打回原因"
              value={reason}
              onChange={(event) => onReason(event.target.value)}
              maxLength={200}
              showCount
              rows={4}
              placeholder="请填写打回原因(最多 200 字)"
              disabled={pending}
            />
          </UFormItem>
          <div className="store-row-actions">
            {rejectReasons.map((value) => (
              <UButton
                key={value}
                type="default"
                size="small"
                disabled={pending}
                onClick={() => onReason(value)}
              >
                {value}
              </UButton>
            ))}
          </div>
        </UForm>
      )}
      {needsConfirmation && (
        <UCheckbox
          checked={confirmed}
          disabled={pending}
          onChange={(event) => onConfirmed(event.target.checked)}
        >
          我已核对以上记录和数据,确认通过当前审批节点
        </UCheckbox>
      )}
      {error && <UAlert style={{ marginTop: 12 }} type="error" showIcon title={error} />}
    </UModal>
  );
}