"use client"; import type React from "react"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import SecretaryLayout from "@/components/secretary-layout"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { Calendar, Clock, User } from "lucide-react"; import { patientsService } from "@/services/patientsApi.mjs"; import { doctorsService } from "@/services/doctorsApi.mjs"; // Importar o serviço de médicos import { toast } from "sonner"; const APPOINTMENTS_STORAGE_KEY = "clinic-appointments"; export default function ScheduleAppointment() { const router = useRouter(); const [patients, setPatients] = useState([]); const [doctors, setDoctors] = useState([]); // Estado para armazenar os médicos da API const [selectedPatient, setSelectedPatient] = useState(""); const [selectedDoctor, setSelectedDoctor] = useState(""); const [selectedDate, setSelectedDate] = useState(""); const [selectedTime, setSelectedTime] = useState(""); const [notes, setNotes] = useState(""); useEffect(() => { const fetchData = async () => { try { // Carrega pacientes e médicos em paralelo para melhor performance const [patientList, doctorList] = await Promise.all([ patientsService.list(), doctorsService.list() ]); setPatients(patientList); setDoctors(doctorList); } catch (error) { console.error("Falha ao buscar dados iniciais:", error); toast.error("Não foi possível carregar os dados de pacientes e médicos."); } }; fetchData(); }, []); const availableTimes = ["08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30"]; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const patientDetails = patients.find((p) => String(p.id) === selectedPatient); const doctorDetails = doctors.find((d) => String(d.id) === selectedDoctor); if (!patientDetails || !doctorDetails) { toast.error("Erro ao encontrar detalhes do paciente ou médico."); return; } const newAppointment = { id: new Date().getTime(), // ID único simples patientName: patientDetails.full_name, doctor: doctorDetails.full_name, // Usar full_name para consistência specialty: doctorDetails.specialty, date: selectedDate, time: selectedTime, status: "agendada", location: doctorDetails.location || "Consultório a definir", // Fallback phone: doctorDetails.phone || "N/A", // Fallback }; const storedAppointmentsRaw = localStorage.getItem(APPOINTMENTS_STORAGE_KEY); const currentAppointments = storedAppointmentsRaw ? JSON.parse(storedAppointmentsRaw) : []; const updatedAppointments = [...currentAppointments, newAppointment]; localStorage.setItem(APPOINTMENTS_STORAGE_KEY, JSON.stringify(updatedAppointments)); toast.success("Consulta agendada com sucesso!"); router.push("/secretary/appointments"); }; return (

Agendar Consulta

Escolha o paciente, médico, data e horário para a consulta

Dados da Consulta Preencha as informações para agendar a consulta
setSelectedDate(e.target.value)} min={new Date().toISOString().split("T")[0]} />