"use client"; import { useEffect, useState } from "react"; import DoctorLayout from "@/components/doctor-layout"; import Link from "next/link"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Eye, Edit, Calendar, Trash2 } from "lucide-react"; import { api } from "@/services/api.mjs"; import { PatientDetailsModal } from "@/components/ui/patient-details-modal"; interface Paciente { id: string; nome: string; telefone: string; cidade: string; estado: string; ultimoAtendimento?: string; proximoAtendimento?: string; email?: string; birth_date?: string; cpf?: string; blood_type?: string; weight_kg?: number; height_m?: number; street?: string; number?: string; complement?: string; neighborhood?: string; cep?: string; } export default function PacientesPage() { const [pacientes, setPacientes] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [selectedPatient, setSelectedPatient] = useState(null); const [isModalOpen, setIsModalOpen] = useState(false); const handleOpenModal = (patient: Paciente) => { setSelectedPatient(patient); setIsModalOpen(true); }; const handleCloseModal = () => { setSelectedPatient(null); setIsModalOpen(false); }; const formatDate = (dateString: string) => { if (!dateString) return ""; const date = new Date(dateString); return new Intl.DateTimeFormat('pt-BR').format(date); }; const [itemsPerPage, setItemsPerPage] = useState(5); const [currentPage, setCurrentPage] = useState(1); const indexOfLastItem = currentPage * itemsPerPage; const indexOfFirstItem = indexOfLastItem - itemsPerPage; const currentItems = pacientes.slice(indexOfFirstItem, indexOfLastItem); const paginate = (pageNumber: number) => setCurrentPage(pageNumber); useEffect(() => { async function fetchPacientes() { try { setLoading(true); setError(null); const json = await api.get("/rest/v1/patients"); const items = Array.isArray(json) ? json : (Array.isArray(json?.data) ? json.data : []); const mapped = items.map((p: any) => ({ id: String(p.id ?? ""), nome: p.full_name ?? "", telefone: p.phone_mobile ?? "", cidade: p.city ?? "", estado: p.state ?? "", ultimoAtendimento: formatDate(p.created_at) ?? "", proximoAtendimento: "", email: p.email ?? "", birth_date: p.birth_date ?? "", cpf: p.cpf ?? "", blood_type: p.blood_type ?? "", weight_kg: p.weight_kg ?? 0, height_m: p.height_m ?? 0, street: p.street ?? "", number: p.number ?? "", complement: p.complement ?? "", neighborhood: p.neighborhood ?? "", cep: p.cep ?? "", })); setPacientes(mapped); } catch (e: any) { setError(e?.message || "Erro ao carregar pacientes"); } finally { setLoading(false); } } fetchPacientes(); }, []); return (

Pacientes

Lista de pacientes vinculados

{loading ? ( ) : error ? ( ) : pacientes.length === 0 ? ( ) : ( currentItems.map((p) => ( )) )}
Nome Telefone Cidade Estado Último atendimento Próximo atendimento Ações
Carregando pacientes...
{`Erro: ${error}`}
Nenhum paciente encontrado
{p.nome} {p.telefone} {p.cidade} {p.estado} {p.ultimoAtendimento} {p.proximoAtendimento} handleOpenModal(p)}> Ver detalhes Laudos alert(`Agenda para paciente ID: ${p.id}`)}> Ver agenda { const newPacientes = pacientes.filter((pac) => pac.id !== p.id) setPacientes(newPacientes) alert(`Paciente ID: ${p.id} excluído`) }} className="text-red-600"> Excluir
{Array.from({ length: Math.ceil(pacientes.length / itemsPerPage) }, (_, i) => ( ))}
); }