"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; type Usuario = { matricula: string; nome: string; perfil: string; perfil_display: string; }; type Solicitacao = { id: string; tipo: string; tipo_display: string; colaborador: string | null; status: string; status_display: string; criado_em: string | null; enviada_em: string | null; solicitante_nome: string | null; pode_aprovar: boolean; pode_dar_parecer: boolean; }; type DashboardData = { usuario: Usuario; total: number; pendentes: number; solicitacoes: Solicitacao[]; pagination: { page: number; per_page: number; total_pages: number; total_count: number; }; }; const STATUS_CLASSES: Record = { RASCUNHO: "bg-amber-50 text-amber-800 border-amber-200", AGUARDANDO_HEAD: "bg-amber-50 text-amber-800 border-amber-200", ENVIADA: "bg-blue-50 text-blue-800 border-blue-200", APROVADA_GG: "bg-emerald-50 text-emerald-800 border-emerald-200", APROVADA_CONTROLADORIA: "bg-emerald-50 text-emerald-800 border-emerald-200", APROVADA_DIRETORIA: "bg-emerald-50 text-emerald-800 border-emerald-200", AGUARDANDO_DIRETORIA: "bg-amber-50 text-amber-800 border-amber-200", FINALIZADA: "bg-slate-100 text-slate-700 border-slate-200", REPROVADA: "bg-red-50 text-red-800 border-red-200", }; const DJANGO_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8888"; function formatarData(iso: string | null) { if (!iso) return "—"; try { const d = new Date(iso); return d.toLocaleDateString("pt-BR", { day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit", }); } catch { return iso; } } export default function DashboardPage() { const router = useRouter(); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const [page, setPage] = useState(1); useEffect(() => { async function carregar() { setLoading(true); setError(""); try { const res = await fetch(`/api/dashboard/?page=${page}`, { credentials: "include", }); // #region agent log fetch("http://localhost:7701/ingest/5073259c-ddcc-441a-a087-e13a2cf7ac9e", { method: "POST", headers: { "Content-Type": "application/json", "X-Debug-Session-Id": "ca90b5", }, body: JSON.stringify({ sessionId: "ca90b5", runId: "login-debug-01", hypothesisId: "H1", location: "frontend/app/dashboard/page.tsx:fetch", message: "dashboard_fetch_status", data: { status: res.status, page }, timestamp: Date.now(), }), }).catch(() => {}); // #endregion if (res.status === 401) { router.push(`/tela_login?next=${encodeURIComponent("/dashboard")}`); return; } if (!res.ok) { setError("Erro ao carregar o dashboard."); return; } const json = await res.json(); setData(json); } catch { setError("Erro de conexão."); } finally { setLoading(false); } } carregar(); }, [page, router]); async function handleLogout() { try { await fetch("/api/auth/logout/", { method: "POST", credentials: "include", }); } catch { /* ignore */ } router.push("/tela_login"); } if (loading) { return (

Carregando…

); } if (error || !data) { return (

{error || "Erro ao carregar."}

); } const { usuario, total, pendentes, solicitacoes, pagination } = data; const isGestor = usuario.perfil === "GESTOR"; return (
{/* Header */}

SGMP CORP

{/* Saudação */}

SGMP - Movimentação de Pessoas

Olá, {usuario.nome}! {usuario.perfil_display}
Matrícula: {usuario.matricula}

{isGestor && (
+ Nova Solicitação
)}
{isGestor && (
💡

Lembrete Rápido

Solicitações em Rascunho só são visíveis para você. Lembre-se de clicar em{" "} "Enviar para Aprovação" na página de detalhes para iniciar o fluxo.

)} {/* Métricas */}
Total
{total}
Pendentes
{pendentes}
{/* Tabela de solicitações */}

{isGestor ? "📋 Minhas Solicitações" : "⏳ Pendentes de Aprovação"}

{!isGestor && (
ℹ️ Você está vendo solicitações com status{" "} Enviada aguardando sua análise.
)} {solicitacoes.length > 0 ? ( <>
{solicitacoes.map((s) => (

{s.tipo_display}

{s.status_display}

{s.colaborador || "N/A"}

{formatarData(s.criado_em)}

Detalhes {s.pode_dar_parecer && ( 📝 Parecer )}
{s.pode_aprovar && (

Aprovar/Reprovar na página de detalhes.

)}
))}
{solicitacoes.map((s) => ( ))}
Tipo Colaborador Status Data Ações
{s.tipo_display} {s.colaborador || ( N/A )} {s.status_display} {formatarData(s.criado_em)}
Detalhes {s.pode_dar_parecer && ( 📝 Parecer )} {s.pode_aprovar && ( (Aprovar/Reprovar na página de detalhes) )}
{pagination.total_pages > 1 && (
{pagination.page} / {pagination.total_pages}
)} ) : (

🎉 Nenhuma solicitação encontrada.

{isGestor && (

Clique em Nova Solicitação para iniciar um novo fluxo.

)}
)}
); }