'use client'; import { useState, useEffect } from 'react'; import Link from 'next/link'; import { useSession } from 'next-auth/react'; import { ArrowLeft, FileText, Download, Calendar, TrendingUp, BarChart3, PieChart, Loader2 } from 'lucide-react'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, RadarChart, PolarGrid, PolarAngleAxis, PolarRadiusAxis, Radar, AreaChart, Area } from 'recharts'; interface ProgressData { progressByCategory: Record; currentProgress: { category: string; value: number }[]; child?: { id: string; name: string }; } const categoryLabels: Record = { comunicacao: 'Comunicação', habilidades_sociais: 'Habilidades Sociais', autonomia: 'Autonomia', regulacao_emocional: 'Regulação Emocional', }; const categoryColors: Record = { comunicacao: '#3b82f6', habilidades_sociais: '#22c55e', autonomia: '#eab308', regulacao_emocional: '#a855f7', }; export default function RelatoriosPage() { const { data: session } = useSession(); const [loading, setLoading] = useState(true); const [data, setData] = useState(null); const [period, setPeriod] = useState<'7' | '30' | '90'>('30'); useEffect(() => { fetchProgress(); }, [period]); const fetchProgress = async () => { try { setLoading(true); const response = await fetch(`/api/progress?days=${period}`); if (response.ok) { const progressData = await response.json(); setData(progressData); } } catch (error) { console.error('Erro ao buscar progresso:', error); } finally { setLoading(false); } }; const generatePDF = async () => { // Importar dinamicamente para evitar SSR issues const html2canvas = (await import('html2canvas')).default; const { jsPDF } = await import('jspdf'); const content = document.getElementById('report-content'); if (!content) return; const canvas = await html2canvas(content, { scale: 2 }); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF('p', 'mm', 'a4'); const imgWidth = 210; const pageHeight = 295; const imgHeight = (canvas.height * imgWidth) / canvas.width; let heightLeft = imgHeight; let position = 0; pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight); heightLeft -= pageHeight; while (heightLeft >= 0) { position = heightLeft - imgHeight; pdf.addPage(); pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight); heightLeft -= pageHeight; } pdf.save(`relatorio-iris-${new Date().toISOString().split('T')[0]}.pdf`); }; // Preparar dados para gráficos const prepareLineChartData = () => { if (!data) return []; const allDates = new Set(); Object.values(data.progressByCategory).forEach(cat => { cat.dates.forEach(d => allDates.add(d)); }); const sortedDates = Array.from(allDates).sort(); return sortedDates.map(date => { const point: Record = { date: date.slice(5) }; // MM-DD format Object.entries(data.progressByCategory).forEach(([cat, catData]) => { const idx = catData.dates.indexOf(date); point[cat] = idx !== -1 ? catData.values[idx] : null; }); return point; }); }; const prepareRadarData = () => { if (!data) return []; return data.currentProgress.map(p => ({ category: categoryLabels[p.category] || p.category, value: p.value, fullMark: 100, })); }; const lineChartData = prepareLineChartData(); const radarData = prepareRadarData(); return (
{/* Header */}

Relatórios

{data?.child ? `Progresso de ${data.child.name}` : 'Acompanhamento de progresso'}

{/* Period selector */}
{[ { value: '7', label: '7 dias' }, { value: '30', label: '30 dias' }, { value: '90', label: '90 dias' }, ].map(p => ( ))}
{loading ? (
) : (
{/* Current Progress Cards */}
{data?.currentProgress.map(p => (
{categoryLabels[p.category]}
{p.value}%
))}
{/* Charts */}
{/* Line Chart */}

Evolução ao Longo do Tempo

{lineChartData.length > 0 ? ( {Object.keys(categoryLabels).map(cat => ( ))} ) : (
Sem dados para o período selecionado
)}
{/* Radar Chart */}

Visão Geral das Áreas

{radarData.length > 0 ? ( ) : (
Sem dados para exibir
)}
{/* Summary */}

Resumo do Período

Nos últimos {period} dias, observamos progresso em todas as áreas de desenvolvimento.

{data?.currentProgress && data.currentProgress.length > 0 && ( <>

A área com melhor desempenho é {categoryLabels[data.currentProgress.reduce((a, b) => a.value > b.value ? a : b ).category]} com {Math.max(...data.currentProgress.map(p => p.value))}% de progresso.

Recomendamos focar em {categoryLabels[data.currentProgress.reduce((a, b) => a.value < b.value ? a : b ).category]} nas próximas sessões para equilibrar o desenvolvimento.

)}
)}
); }