import { formatDisplayOrDash } from '@/utils/datetime-display' import { type PaymentFlowStep, type PaymentFlowStepKey, type PaymentFlowStepState } from '@/utils/clinical-payment-flow-steps' import { transferTypeLabel } from '@/utils/protocol-payment-labels' export type { PaymentFlowStep, PaymentFlowStepKey, PaymentFlowStepState } function dash(v: unknown) { return formatDisplayOrDash(v) } function formatAmount(v: unknown) { if (v == null || v === '') return '—' const n = Number(v) if (Number.isNaN(n)) return '—' return `¥${n.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}` } function resolveAttachmentUrls( detail: Record, urlField: string, filesField: string ): string { const urls = String(detail[urlField] || '') if (urls.trim()) return urls const files = detail[filesField] if (!Array.isArray(files)) return '' return files .map((f) => String((f as Record).fileUrl || '').trim()) .filter(Boolean) .join(',') } function hasReceiptSubmit(detail: Record) { const status = String(detail.payStatus || '') return ['jfqrz', 'yjf', 'tfqrz', 'ytf'].includes(status) } function hasFinanceReceipt(detail: Record) { const status = String(detail.payStatus || '') return ['yjf', 'tfqrz', 'ytf'].includes(status) } function hasRefundSubmit(detail: Record) { const status = String(detail.payStatus || '') return ['tfqrz', 'ytf'].includes(status) } function hasFinanceRefund(detail: Record) { const status = String(detail.payStatus || '') return status === 'ytf' } function resolveStepState( key: PaymentFlowStepKey, detail: Record, activeStepKey?: PaymentFlowStepKey ): PaymentFlowStepState { if (activeStepKey === key) return 'current' const doneMap: Record = { 'receipt-submit': hasReceiptSubmit(detail), 'finance-receipt': hasFinanceReceipt(detail), 'refund-submit': hasRefundSubmit(detail), 'finance-refund': hasFinanceRefund(detail) } if (doneMap[key]) return 'done' return 'pending' } function receiptSubmitTime(detail: Record) { const status = String(detail.payStatus || '') if (status === 'jfqrz') return dash(detail.updateTime) if (hasFinanceReceipt(detail) && detail.transferTime) return dash(detail.transferTime) return '—' } function financeReceiptTime(detail: Record) { const status = String(detail.payStatus || '') if (status === 'jfqrz' || status === 'dfj') return '—' return dash(detail.financeConfirmTime || detail.updateTime) } function refundSubmitTime(detail: Record) { const status = String(detail.payStatus || '') if (!['tfqrz', 'ytf'].includes(status)) return '—' if (status === 'tfqrz') return dash(detail.updateTime) return dash(detail.refundApplyTime || detail.updateTime) } function refundFinanceTime(detail: Record) { const status = String(detail.payStatus || '') if (status !== 'ytf') return '—' return dash(detail.refundFinanceConfirmTime || detail.updateTime) } export function buildProtocolPaymentFlowSteps( detail: Record | null | undefined, activeStepKey?: PaymentFlowStepKey ): PaymentFlowStep[] { const d = detail || {} const transferUrls = resolveAttachmentUrls(d, 'transferAttachments', 'attachments') const financeUrls = resolveAttachmentUrls( d, 'financeConfirmAttachments', 'financeConfirmAttachmentFiles' ) const refundUrls = resolveAttachmentUrls(d, 'refundAttachments', 'refundAttachmentFiles') const refundFinanceUrls = resolveAttachmentUrls( d, 'refundFinanceConfirmAttachments', 'refundFinanceConfirmAttachmentFiles' ) return [ { key: 'receipt-submit', title: '缴费确认信息', state: resolveStepState('receipt-submit', d, activeStepKey), time: receiptSubmitTime(d), fields: [ { label: '转账方式', value: transferTypeLabel(String(d.transferType || '')) || '—' }, { label: '实缴金额(元)', value: formatAmount(d.transferAmount) }, { label: '转账时间', value: dash(d.transferTime) }, { label: '备注', value: dash(d.remark), multiline: true }, { label: '转账凭证', value: '', slot: 'attachments', attachmentUrls: transferUrls } ] }, { key: 'finance-receipt', title: '缴费财务确认信息', state: resolveStepState('finance-receipt', d, activeStepKey), time: financeReceiptTime(d), fields: [ { label: '财务备注', value: dash(d.financeRemark), multiline: true }, { label: '财务确认附件', value: '', slot: 'attachments', attachmentUrls: financeUrls } ] }, { key: 'refund-submit', title: '退费确认信息', state: resolveStepState('refund-submit', d, activeStepKey), time: refundSubmitTime(d), fields: [ { label: '退费金额(元)', value: formatAmount(d.refundAmount) }, { label: '退费备注', value: dash(d.refundRemark), multiline: true }, { label: '退费附件', value: '', slot: 'attachments', attachmentUrls: refundUrls } ] }, { key: 'finance-refund', title: '退费财务确认信息', state: resolveStepState('finance-refund', d, activeStepKey), time: refundFinanceTime(d), fields: [ { label: '财务备注', value: dash(d.refundFinanceRemark), multiline: true }, { label: '财务确认附件', value: '', slot: 'attachments', attachmentUrls: refundFinanceUrls } ] } ] }