"use client"; import React, { useState } from "react"; import { useParams, useRouter } from "next/navigation"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import Link from "next/link"; import { ArrowLeft, ClipboardPlus } from "lucide-react"; import patientServices, { PatientDetail } from "@/services/patientServices"; import consultationServices, { ConsultationItem, ConsultationResponse, } from "@/services/consultationServices"; import { httpRequest } from "@/services"; import { QUERY_KEY, TYPE_STATUS } from "@/constant/config/enum"; import { PATH } from "@/constant/config"; import CustomButton from "@/components/customs/custom-button"; import StateActive from "@/components/customs/StateActive"; import DataWrapper from "@/components/customs/DataWrapper"; import Table from "@/components/customs/custom-table"; import CustomDialog from "@/components/customs/custom-dialog"; const DetailPatient = () => { const params = useParams(); const router = useRouter(); const queryClient = useQueryClient(); const patientId = params?.id as string; // State quản lý đóng/mở Dialog xác nhận tạo phiên khám const [openCreateDialog, setOpenCreateDialog] = useState(false); /* ========================= GET DETAIL PATIENT ========================= */ const patientDetailQuery = useQuery({ queryKey: [QUERY_KEY.chi_tiet_benh_nhan, patientId], enabled: !!patientId, queryFn: async () => { const res = await httpRequest({ showMessageFailed: true, http: patientServices.detailPatient(patientId), }); return ( res || { id: "", patientCode: "", identificationNumber: "", fullName: "", dateOfBirth: "", gender: "", phone: "", email: "", address: "", emergencyContactName: null, emergencyContactPhone: null, allergyNotes: null, chronicDiseaseNotes: null, insuranceNumber: null, notes: null, isActive: false, } ); }, }); /* ========================= GET CONSULTATION HISTORY ========================= */ const consultationQuery = useQuery({ queryKey: [QUERY_KEY.table_list_consultation, patientId], enabled: !!patientId, queryFn: async () => { const res = await httpRequest({ showMessageFailed: true, http: consultationServices.getConsultations({ Page: 1, PageSize: 5, PatientId: patientId, }), }); return ( res || { items: [], page: 1, pageSize: 10, total: 0, totalPages: 0, } ); }, }); /* ========================= CREATE MUTATION ========================= */ const createConsultationMutation = useMutation({ mutationFn: async () => { return httpRequest({ showMessageFailed: true, showMessageSuccess: true, msgSuccess: "Tạo phiên khám mới thành công!", // Gọi API với payload chứa patientId của bệnh nhân hiện tại http: consultationServices.createConsultations({ patientId: patientId, }), }); }, onSuccess: (res: any) => { // Đóng dialog setOpenCreateDialog(false); // Làm mới danh sách lịch sử phiên khám của bệnh nhân này queryClient.invalidateQueries({ queryKey: [QUERY_KEY.table_list_consultation, patientId], }); // Nếu API trả về thông tin phiên khám vừa tạo (có chứa id), chuyển thẳng tới trang update if (res?.id) { router.push(`${PATH.CONSULTATION}/update/${res.id}`); } }, }); const patient = patientDetailQuery.data; const consultationData = consultationQuery.data?.items || []; /* ========================= HANDLERS DIALOG ========================= */ const handleOpenCreate = () => { setOpenCreateDialog(true); }; const handleCloseDialog = () => { setOpenCreateDialog(false); }; const handleCreateDialog = () => { createConsultationMutation.mutate(); }; if (patientDetailQuery.isLoading) { return (
Đang tải dữ liệu...
); } return (

router.back()} className="cursor-pointer" />{" "} {patient?.fullName}

} onClick={handleOpenCreate} disabled={createConsultationMutation.isPending} > Tạo phiên khám
{/* Thông tin chi tiết bệnh nhân */}

Mã bệnh nhân

{patient?.patientCode}

Họ tên

{patient?.fullName}

Mã định danh

{patient?.identificationNumber}

Ngày sinh

{patient?.dateOfBirth}

Giới tính

{patient?.gender}

Số điện thoại

{patient?.phone}

Email

{patient?.email}

Địa chỉ

{patient?.address || "-"}

Người liên hệ khẩn cấp

{patient?.emergencyContactName || "-"}

SĐT khẩn cấp

{patient?.emergencyContactPhone || "-"}

Dị ứng

{patient?.allergyNotes || "-"}

Bệnh nền

{patient?.chronicDiseaseNotes || "-"}

{/* Lịch sử phiên khám */}

Lịch sử phiên khám

Tất cả
item.id} data={consultationData} column={[ { fixedLeft: true, title: "ID PHIÊN KHÁM", render: (item: ConsultationItem) => ( {item.id} ), }, { title: "MÃ PHIÊN KHÁM", render: (item: ConsultationItem) => ( {item.sessionCode} ), }, { title: "NGÀY KHÁM", render: (item: ConsultationItem) => ( {item.visitDate} ), }, { title: "DẤU HIỆU", render: (item: ConsultationItem) => ( {item.symptomsText || "---"} ), }, { title: "CHUẨN ĐOÁN", render: (item: ConsultationItem) => ( {item.diagnosis || "---"} ), }, { title: "KẾ HOẠCH ĐIỀU TRỊ", render: (item: ConsultationItem) => ( {item.treatmentPlan || "---"} ), }, { title: "GHI CHÚ BÁC SĨ", render: (item: ConsultationItem) => ( {item.doctorNotes || "---"} ), }, { title: "TRẠNG THÁI", render: (item: ConsultationItem) => ( ), }, ]} /> {/* DIALOG XÁC NHẬN */} ); }; export default DetailPatient;