import { useEffect, useState, useCallback } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { Card, Descriptions, Tabs, Button, Space, Modal, Form, Input, Select, DatePicker, message, Spin, } from 'antd'; import { ArrowLeftOutlined, EditOutlined } from '@ant-design/icons'; import { patientApi } from '../../api/health/patients'; import { AuthButton } from '../../components/AuthButton'; import type { PatientDetail as PatientDetailType, UpdatePatientReq, } from '../../api/health/patients'; import { StatusTag } from './components/StatusTag'; import { VitalSignsTab } from './components/VitalSignsTab'; import { LabReportsTab } from './components/LabReportsTab'; import { HealthRecordsTab } from './components/HealthRecordsTab'; import { FollowUpTab } from './components/FollowUpTab'; import { GENDER_OPTIONS, BLOOD_TYPE_OPTIONS } from '../../constants/health'; import { useThemeMode } from '../../hooks/useThemeMode'; const GENDER_LABEL: Record = { male: '男', female: '女', other: '其他', }; export default function PatientDetail() { const { id } = useParams<{ id: string }>(); const navigate = useNavigate(); const [patient, setPatient] = useState(null); const [loading, setLoading] = useState(false); const [editModalOpen, setEditModalOpen] = useState(false); const [form] = Form.useForm(); const isDark = useThemeMode(); // --- 加载患者基本信息 --- const fetchPatient = useCallback(async () => { if (!id) return; setLoading(true); try { const data = await patientApi.get(id); setPatient(data); } catch { message.error('加载患者信息失败'); } setLoading(false); }, [id]); useEffect(() => { fetchPatient(); }, [fetchPatient]); // --- 编辑患者 --- const handleEdit = async (values: { name?: string; gender?: string; birth_date?: unknown; blood_type?: string; id_number?: string; allergy_history?: string; medical_history_summary?: string; emergency_contact_name?: string; emergency_contact_phone?: string; notes?: string; }) => { if (!patient) return; const formatted = { ...values, birth_date: values.birth_date && typeof values.birth_date === 'object' && 'format' in (values.birth_date as object) ? (values.birth_date as { format: (f: string) => string }).format('YYYY-MM-DD') : (values.birth_date as string | undefined), }; try { const req: UpdatePatientReq & { version: number } = { ...formatted, version: patient.version, }; await patientApi.update(patient.id, req); message.success('患者信息更新成功'); setEditModalOpen(false); fetchPatient(); } catch (err: unknown) { const errorMsg = (err as { response?: { data?: { message?: string } } })?.response?.data ?.message || '更新失败'; message.error(errorMsg); } }; const openEditModal = () => { if (!patient) return; form.setFieldsValue({ name: patient.name, gender: patient.gender, birth_date: patient.birth_date, blood_type: patient.blood_type, id_number: patient.id_number, allergy_history: patient.allergy_history, medical_history_summary: patient.medical_history_summary, emergency_contact_name: patient.emergency_contact_name, emergency_contact_phone: patient.emergency_contact_phone, notes: patient.notes, }); setEditModalOpen(true); }; // --- 加载状态 --- if (loading) { return (
); } if (!patient) { return (

未找到患者信息

); } // --- 主题卡片样式 --- const cardStyle = { borderRadius: 12, background: isDark ? '#111827' : '#FFFFFF', border: `1px solid ${isDark ? '#0f172a' : '#f8fafc'}`, }; return (
{/* 顶部导航 */}
{/* 患者基本信息卡片 */}
{(patient.name?.[0] || 'P').toUpperCase()}
{patient.name}
{GENDER_LABEL[patient.gender || ''] || patient.gender || '-'} {patient.birth_date || '-'} {patient.blood_type || '-'} {patient.id_number || '-'} {patient.source || '-'} {patient.created_at ? new Date(patient.created_at).toLocaleString('zh-CN') : '-'}
{patient.name} {GENDER_LABEL[patient.gender || ''] || patient.gender || '-'} {patient.birth_date || '-'} {patient.blood_type || '-'} {patient.id_number || '-'} {patient.source || '-'} {patient.allergy_history || '-'} {patient.medical_history_summary || '-'} {patient.emergency_contact_name || '-'} {patient.emergency_contact_phone || '-'} {patient.notes || '-'} ), }, { key: 'health', label: '健康数据', children: id ? ( }, { key: 'lab', label: '化验报告', children: }, { key: 'records', label: '健康档案', children: }, ]} /> ) : null, }, { key: 'followup', label: '随访记录', children: id ? : null, }, ]} /> {/* 编辑弹窗 */} setEditModalOpen(false)} onOk={() => form.submit()} width={600} >
); }