PaymentRecordModal.tsx 3.7 KB
import { postServiceOrderRefundHistory } from '@/services';
import { Button, Empty, Image, Modal } from 'antd';
import React, { useEffect, useState } from 'react';

interface PaymentRecordModalProps {
  visible: boolean;
  mainOrderId: number;
  onClose: () => void;
}

interface PaymentRecord {
  id: number;
  mainOrderId: number;
  refundMoney: number;
  createTime: string;
  annex: string;
  isAudit: number;
  isDel: number;
  updateTime: string | null;
  paymentReceiptAnnexPartial: string | null;
}

const PaymentRecordModal: React.FC<PaymentRecordModalProps> = ({
  visible,
  mainOrderId,
  onClose,
}) => {
  const [paymentRecords, setPaymentRecords] = useState<PaymentRecord[]>([]);

  const fetchPaymentRecords = async () => {
    if (!mainOrderId) return;

    try {
      const response = await postServiceOrderRefundHistory({
        data: { mainOrderId },
      });

      if (response.result === 0 && response.data) {
        setPaymentRecords(response.data);
      } else {
        setPaymentRecords([]);
      }
    } catch (error) {
      console.error('Failed to fetch payment records:', error);
      setPaymentRecords([]);
    }
  };

  useEffect(() => {
    if (visible && mainOrderId) {
      fetchPaymentRecords();
    }
  }, [visible, mainOrderId]);

  const parseAnnex = (annexString: string): string[] => {
    try {
      const parsed = JSON.parse(annexString);
      return parsed.map((item: { url: string }) => item.url);
    } catch (e) {
      console.error('Failed to parse annex:', e);
      return [];
    }
  };

  return (
    <Modal
      title="付款记录"
      open={visible}
      onCancel={onClose}
      footer={[
        <Button key="close" onClick={onClose}>
          关闭
        </Button>,
      ]}
      width={500}
    >
      <div className="payment-record-container">
        {paymentRecords.length > 0 ? (
          paymentRecords.map((record, index) => {
            const imageUrls = record.annex ? parseAnnex(record.annex) : [];

            return (
              <div key={record.id} className="payment-record-item">
                <div className="payment-record-header">
                  {index < paymentRecords.length - 1 && (
                    <div className="payment-record-line" />
                  )}
                  <div className="payment-record-circle" />
                </div>
                <div className="payment-record-content">
                  <div className="payment-record-info">
                    <p>
                      <strong>提交时间:</strong> {record.createTime}
                    </p>
                    <p>
                      <strong>付款金额:</strong>{' '}
                      {record.refundMoney.toFixed(2)}
                    </p>
                    {imageUrls.length > 0 && (
                      <p>
                        <strong>付款凭证:</strong>
                      </p>
                    )}
                  </div>
                  {imageUrls.length > 0 && (
                    <div className="payment-record-images">
                      <Image.PreviewGroup>
                        {imageUrls.map((url, imgIndex) => (
                          <Image
                            key={imgIndex}
                            width={120}
                            src={url}
                            className="payment-record-image"
                          />
                        ))}
                      </Image.PreviewGroup>
                    </div>
                  )}
                </div>
              </div>
            );
          })
        ) : (
          <Empty description="暂无付款记录" className="payment-record-empty" />
        )}
      </div>
    </Modal>
  );
};

export default PaymentRecordModal;