"use client"; import type React from "react"; import { useState, useEffect, useCallback } from "react"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; // [SINCRONIZAÇÃO 1] - Importando a lista de 'appointments' para a validação de conflito import { useAppointments } from "../../context/AppointmentsContext"; // Componentes de UI e Layout import PatientLayout from "@/components/patient-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 { doctorsService } from "services/doctorsApi.mjs"; // Interface para o estado local do formulário (sem alterações) interface AppointmentFormState { id: string; date: string; time: string; observations: string; } interface Doctor { id: string; full_name: string; specialty: string; phone_mobile: string; } // --- DADOS MOCKADOS (ALTERAÇÃO 1: Adicionando location e phone) --- const doctors = [ { id: "1", name: "Dr. João Silva", specialty: "Cardiologia", location: "Consultório A - 2º andar", phone: "(11) 3333-4444" }, { id: "2", name: "Dra. Maria Santos", specialty: "Dermatologia", location: "Consultório B - 1º andar", phone: "(11) 3333-5555" }, { id: "3", name: "Dr. Pedro Costa", specialty: "Ortopedia", location: "Consultório C - 3º andar", phone: "(11) 3333-6666" }, ]; const availableTimes = ["09:00", "09:30", "10:00", "10:30", "14:00", "14:30", "15:00"]; // ------------------------------------------------------------- export default function ScheduleAppointmentPage() { const router = useRouter(); const [doctors, setDoctors] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); // [SINCRONIZAÇÃO 1 - continuação] - Obtendo a lista de agendamentos existentes const { addAppointment, appointments } = useAppointments(); const [formData, setFormData] = useState({ id: "", date: "", time: "", observations: "", }); const fetchDoctors = useCallback(async () => { setLoading(true); setError(null); try { const data: Doctor[] = await doctorsService.list(); setDoctors(data || []); } catch (e: any) { console.error("Erro ao carregar lista de médicos:", e); setError("Não foi possível carregar a lista de médicos. Verifique a conexão com a API."); setDoctors([]); } finally { setLoading(false); } }, []); useEffect(() => { fetchDoctors(); }, [fetchDoctors]); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prevState => ({ ...prevState, [name]: value })); }; const handleSelectChange = (name: keyof AppointmentFormState, value: string) => { setFormData(prevState => ({ ...prevState, [name]: value })); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!formData.id || !formData.date || !formData.time) { toast.error("Por favor, preencha os campos de médico, data e horário."); return; } const selectedDoctor = doctors.find(doc => doc.id === formData.id); if (!selectedDoctor) return; // Validação de conflito (sem alterações, já estava correta) const isConflict = appointments.some( (apt) => apt.doctorName === selectedDoctor.full_name && apt.date === formData.date && apt.time === formData.time ); if (isConflict) { toast.error("Este horário já está ocupado para o médico selecionado."); return; } // [ALTERAÇÃO 2] - Utilizando os dados do médico selecionado para location e phone // e removendo os placeholders. addAppointment({ doctorName: selectedDoctor.full_name, specialty: selectedDoctor.specialty, date: formData.date, time: formData.time, observations: formData.observations, phone: selectedDoctor.phone_mobile, location: "" }); toast.success("Consulta agendada com sucesso!"); router.push('/patient/appointments'); }; // Validação de data passada (sem alterações, já estava correta) const today = new Date().toISOString().split('T')[0]; return (

Agendar Consulta

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

Dados da Consulta Preencha as informações para agendar sua consulta