HandoverDetailModal.tsx 4.26 KB
import { useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { UAlert, UButton, UImage, UModal } from '@uj/ui';
import { CombiForm, DataTable, type DataTableColumn, type FieldSchema } from '@/components';
import { staffHandoverApi } from '@/api';
import { errorDetail } from '@/utils/error';
import './handover.css';
type Row = Record<string, unknown> & { id: string | number };
const text = (v: unknown) => (v === undefined || v === null || v === '' ? '-' : String(v));
const detailFields = (items: Array<[string, string]>): FieldSchema<Record<string, unknown>>[] =>
  items.map(([name, label]) => ({ name, label, type: 'input', render: text }));
export default function HandoverDetailModal({
  record,
  onClose,
}: {
  record: Row | undefined;
  onClose: () => void;
}) {
  const [previewUrls, setPreviewUrls] = useState<string[]>([]);
  const [previewIndex, setPreviewIndex] = useState(0);
  const previewUrl = previewUrls[previewIndex];
  const q = useQuery({
    queryKey: ['handover-detail', record?.id],
    queryFn: () =>
      staffHandoverApi.getStaffHandoverDetail<Record<string, unknown>>({ id: record?.id }),
    enabled: Boolean(record),
  });
  const data = q.data ?? {};
  const columns = [
    ['评估单号', 'evalOrderNo'],
    ['门店', 'sellerMerchantName'],
    ['车型', 'carModel'],
    ['车牌', 'licensePlate'],
  ].map(([title, dataIndex]): DataTableColumn<Record<string, unknown>> => ({
    title,
    dataIndex,
    width: 160,
    line: 1,
    render: text,
  }));
  columns.push(
    {
      title: '当时协议状态',
      dataIndex: 'signCustodyStatus',
      width: 130,
      render: (v: unknown) => (v ? '已签署' : '未签署'),
    },
    {
      title: '凭证',
      dataIndex: 'proofUrls',
      width: 130,
      render: (v: unknown) =>
        Array.isArray(v) && v.length ? (
          <UButton
            type="link"
            onClick={() => {
              setPreviewUrls(v.map(String));
              setPreviewIndex(0);
            }}
          >
            查看凭证
          </UButton>
        ) : (
          '-'
        ),
    },
  );
  return (
    <UModal open={Boolean(record)} title="详情" width={1080} footer={null} onCancel={onClose}>
      <CombiForm
        mode="detail"
        detailColumns={3}
        labelWidth={128}
        fields={detailFields([
          ['handoverBatchNo', '交接批次号'],
          ['handoverTime', '操作时间'],
          ['createdBy', '操作人'],
        ])}
        initialValues={data}
      />
      <h3>待交接资产列表</h3>
      {q.isError ? (
        <UAlert type="error" showIcon title="详情加载失败" description={errorDetail(q.error)} />
      ) : (
        <DataTable<Record<string, unknown>>
          rowKey="evalOrderNo"
          columns={columns}
          dataSource={(data.details as Record<string, unknown>[]) ?? []}
          pagination={false}
        />
      )}
      {previewUrl ? (
        <div className="uc-handover-image-viewer" onClick={() => setPreviewUrls([])}>
          <div
            className="uc-handover-image-viewer__content"
            onClick={(event) => event.stopPropagation()}
          >
            {previewUrls.length > 1 ? (
              <UButton
                className="uc-handover-image-viewer__arrow uc-handover-image-viewer__arrow--left"
                disabled={previewIndex === 0}
                onClick={() => setPreviewIndex((x) => x - 1)}
              >
                ‹
              </UButton>
            ) : null}
            {previewUrl.split('?')[0].toLowerCase().endsWith('.pdf') ? (
              <iframe className="uc-handover-proof-preview" src={previewUrl} title="交接凭证" />
            ) : (
              <UImage
                src={previewUrl}
                preview={false}
                className="uc-handover-image-viewer__image"
              />
            )}
            {previewUrls.length > 1 ? (
              <UButton
                className="uc-handover-image-viewer__arrow uc-handover-image-viewer__arrow--right"
                disabled={previewIndex === previewUrls.length - 1}
                onClick={() => setPreviewIndex((x) => x + 1)}
              >
                ›
              </UButton>
            ) : null}
          </div>
        </div>
      ) : null}
    </UModal>
  );
}