"use client"; import { useState, useEffect } from "react"; import { useRouter, useParams } from "next/navigation"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Progress } from "@/components/ui/progress"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { ChevronLeft, MapPin, Calendar, Building2, User, Phone, Mail, Loader2, Camera, CheckCircle2, Clock, DollarSign, } from "lucide-react"; import { formatCurrency, formatDate, formatPercentage } from "@/lib/utils"; import { ESTADO_OBRA_LABELS, ESTADO_OBRA_COLORS, ESTADO_TAREA_LABELS, CATEGORIA_GASTO_LABELS, ESTADO_GASTO_LABELS, ESTADO_GASTO_COLORS, type EstadoObra, type EstadoTarea, type CategoriaGasto, type EstadoGasto, } from "@/types"; interface ObraDetail { id: string; nombre: string; descripcion: string | null; direccion: string; estado: EstadoObra; porcentajeAvance: number; presupuestoTotal?: number; gastoTotal?: number; fechaInicio: string | null; fechaFinPrevista: string | null; fechaFinReal: string | null; imagenPortada: string | null; supervisor?: { nombre: string; apellido: string; email: string | null }; empresa?: { nombre: string; telefono: string | null; email: string | null }; fases?: { id: string; nombre: string; descripcion: string | null; porcentajeAvance: number; tareas: { id: string; nombre: string; estado: EstadoTarea; porcentajeAvance: number; }[]; }[]; fotos?: { id: string; url: string; thumbnail: string | null; titulo: string | null; descripcion: string | null; fechaCaptura: string; fase?: { nombre: string } | null; }[]; registrosAvance?: { id: string; descripcion: string; porcentaje: number; fotos: string[]; createdAt: string; registradoPor: { nombre: string; apellido: string }; }[]; gastos?: { id: string; concepto: string; monto: number; fecha: string; categoria: CategoriaGasto; estado: EstadoGasto; }[]; permisos: { verFotos: boolean; verAvances: boolean; verGastos: boolean; verDocumentos: boolean; descargarPDF: boolean; }; } export default function PortalObraDetailPage() { const router = useRouter(); const params = useParams(); const [loading, setLoading] = useState(true); const [obra, setObra] = useState(null); const [selectedPhoto, setSelectedPhoto] = useState(null); useEffect(() => { fetchObra(); }, [params.id]); const fetchObra = async () => { try { const res = await fetch(`/api/portal/obras/${params.id}`); if (!res.ok) { if (res.status === 401) { router.push("/portal"); return; } throw new Error("Error al cargar obra"); } const data = await res.json(); setObra(data); } catch (error) { console.error("Error:", error); } finally { setLoading(false); } }; if (loading) { return (
); } if (!obra) { return (

Obra no encontrada

); } return (
{/* Header */}
Volver a mis obras

{obra.nombre}

{ESTADO_OBRA_LABELS[obra.estado]} {obra.direccion}
{formatPercentage(obra.porcentajeAvance)}

Avance total

{/* Content */}
{/* Progress Bar */} General {obra.permisos.verAvances && obra.fases && ( Avances )} {obra.permisos.verFotos && obra.fotos && ( Fotos )} {obra.permisos.verGastos && obra.gastos && ( Finanzas )} {/* General Tab */}
{/* Info del Proyecto */} Informacion del Proyecto {obra.descripcion && (

Descripcion

{obra.descripcion}

)}

Inicio

{obra.fechaInicio ? formatDate(new Date(obra.fechaInicio)) : "Por definir"}

Fin Previsto

{obra.fechaFinPrevista ? formatDate(new Date(obra.fechaFinPrevista)) : "Por definir"}

{obra.permisos.verGastos && obra.presupuestoTotal !== undefined && (

Presupuesto

{formatCurrency(obra.presupuestoTotal)}

Ejecutado

{formatCurrency(obra.gastoTotal || 0)}

)}
{/* Contacto */} Contacto {obra.empresa && (

Constructora

{obra.empresa.nombre}

{obra.empresa.telefono && (

{obra.empresa.telefono}

)} {obra.empresa.email && (

{obra.empresa.email}

)}
)} {obra.supervisor && (

Supervisor

{obra.supervisor.nombre} {obra.supervisor.apellido}

{obra.supervisor.email && (

{obra.supervisor.email}

)}
)}
{/* Ăšltimos Avances */} {obra.permisos.verAvances && obra.registrosAvance && obra.registrosAvance.length > 0 && ( Ultimos Avances
{obra.registrosAvance.slice(0, 3).map((registro) => (

{registro.descripcion}

{registro.registradoPor.nombre}{" "} {registro.registradoPor.apellido} -{" "} {formatDate(new Date(registro.createdAt))}

{formatPercentage(registro.porcentaje)}
))}
)}
{/* Avances Tab */} {obra.permisos.verAvances && obra.fases && ( {obra.fases.length === 0 ? ( No hay fases definidas para esta obra ) : ( obra.fases.map((fase) => (
{fase.nombre}
{formatPercentage(fase.porcentajeAvance)}
{fase.descripcion && ( {fase.descripcion} )}
{fase.tareas.length === 0 ? (

Sin tareas definidas

) : (
{fase.tareas.map((tarea) => (
{tarea.estado === "COMPLETADA" ? ( ) : ( )} {tarea.nombre}
{ESTADO_TAREA_LABELS[tarea.estado]}
))}
)}
)) )}
)} {/* Fotos Tab */} {obra.permisos.verFotos && obra.fotos && ( {obra.fotos.length === 0 ? (

No hay fotos disponibles

) : (
{obra.fotos.map((foto) => ( setSelectedPhoto(foto.url)} >
{foto.titulo
{foto.titulo && (

{foto.titulo}

)}

{formatDate(new Date(foto.fechaCaptura))}

{foto.fase && ( {foto.fase.nombre} )}
))}
)} {/* Modal de foto */} {selectedPhoto && (
setSelectedPhoto(null)} > Foto ampliada
)}
)} {/* Finanzas Tab */} {obra.permisos.verGastos && obra.gastos && ( {/* Resumen */}
Presupuesto
{formatCurrency(obra.presupuestoTotal || 0)}
Ejecutado
{formatCurrency(obra.gastoTotal || 0)}
Disponible
{formatCurrency( (obra.presupuestoTotal || 0) - (obra.gastoTotal || 0) )}
{/* Lista de gastos */} Ultimos Gastos {obra.gastos.length === 0 ? (

No hay gastos registrados

) : (
{obra.gastos.map((gasto) => (

{gasto.concepto}

{CATEGORIA_GASTO_LABELS[gasto.categoria]} -{" "} {formatDate(new Date(gasto.fecha))}

{formatCurrency(gasto.monto)}

{ESTADO_GASTO_LABELS[gasto.estado]}
))}
)}
)}
); }