import { useEffect, useMemo, useState } from 'react' import { appointmentRepository } from '../repositories/appointmentRepository.js' import { patientRepository } from '../repositories/patientRepository.js' import { professionalRepository } from '../repositories/professionalRepository.js' const statusFilters = [ { label: 'Todos', value: 'Todos' }, { label: 'Confirmadas', value: 'Confirmada' }, { label: 'Em triagem', value: 'Em triagem' }, { label: 'Aguardando', value: 'Aguardando' }, ] const viewFilters = ['Dia', 'Semana', 'Mês'] export function AgendaPage({ navigate }) { const [patients, setPatients] = useState([]) const [professionals, setProfessionals] = useState([]) const queue = appointmentRepository.getPredictiveQueueSummary() const timeline = appointmentRepository.getTodayTimeline() const weekDays = appointmentRepository.getWeekDays() const [activeView, setActiveView] = useState('Dia') const [status, setStatus] = useState('Todos') const [modalOpen, setModalOpen] = useState(false) const [localAppointments, setLocalAppointments] = useState([]) const [form, setForm] = useState({ patientId: '', professionalId: '', type: 'Retorno', time: '15:30', mode: 'Teleconsulta', }) useEffect(() => { Promise.all([ patientRepository.getAll(), appointmentRepository.getAll(), professionalRepository.getAll() ]).then(([patientsData, appointmentsData, professionalsData]) => { setPatients(patientsData) setLocalAppointments(appointmentsData || []) setProfessionals(professionalsData || []) setForm((current) => ({ ...current, patientId: patientsData?.length ? patientsData[0].id : '', professionalId: professionalsData?.length ? professionalsData[0].id : '', })) }).catch(e => console.error(e)) }, []) const visibleAppointments = useMemo(() => { if (status === 'Todos') { return localAppointments } return localAppointments.filter((appointment) => appointment.status === status) }, [localAppointments, status]) function updateForm(field, value) { setForm((current) => ({ ...current, [field]: value })) } async function handleCreate(event) { event.preventDefault() // Fallback date and time const today = new Date().toISOString().split('T')[0] try { const created = await appointmentRepository.create({ patientId: form.patientId, date: today, time: form.time, type: form.type, mode: form.mode, room: form.mode === 'Teleconsulta' ? 'Virtual' : 'Consultório 1', professionalId: form.professionalId, }) setLocalAppointments((current) => [...current, created]) setModalOpen(false) } catch(err) { alert(err.message || 'Erro ao criar agendamento.') } } return (

Agenda

Organize consultas, retornos e teleatendimentos do dia.

{weekDays.map((day) => ( ))}

Terça, 07 abril

Visualização: {activeView.toLowerCase()} | {visibleAppointments.length} registros no filtro

{viewFilters.map((view) => ( ))}
{statusFilters.map((filter) => ( ))}
{visibleAppointments.length ? ( visibleAppointments.map((appointment) => ( )) ) : (

Nenhum horário encontrado

Ajuste o filtro ou crie uma consulta mockada para este período.

)}

Linha do tempo

{timeline.map((item) => ( ))}

Resumo preditivo

{queue.map((item) => (
{item.label} {item.value}
))}
setModalOpen(false)} open={modalOpen} title="Nova consulta">
updateForm('time', event.target.value)} type="time" value={form.time} />
updateForm('type', event.target.value)} value={form.type} />
) } function AgendaListItem({ appointment, navigate }) { return (

{appointment.time}

{appointment.mode}

{appointment.type} com {appointment.professional}

{appointment.room}

) } function DarkField({ children, label }) { return ( ) } function DarkModal({ children, onClose, open, title }) { if (!open) { return null } return (

{title}

{children}
) } function StatusPill({ status }) { const classes = { Confirmada: 'border-[#14532d] bg-[#052e1a] text-[#10b981]', 'Em triagem': 'border-[#78350f] bg-[#2d1e05] text-[#f59e0b]', Aguardando: 'border-[#404040] bg-[#303030] text-[#a3a3a3]', Bloqueado: 'border-[#404040] bg-[#303030] text-[#737373]', } return ( {status} ) } function queueTone(tone) { if (tone === 'red') { return 'text-[#ef4444]' } if (tone === 'amber') { return 'text-[#f59e0b]' } return 'text-[#3b82f6]' }