1 Commits

Author SHA1 Message Date
frcarlos 88f2ac13d7 fechas 2025-11-04 13:22:07 -06:00
+236 -277
View File
@@ -2,312 +2,271 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { axiosInstance } from "@/api/config"; import { axiosInstance } from "@/api/config";
import { ServicioSocialConCasoEspecial, ServicioSocialResponse } from "@/types/responses"; import {
ServicioSocialConCasoEspecial,
ServicioSocialResponse,
} from "@/types/responses";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
interface Props { interface Props {
data?: ServicioSocialResponse[]; data?: ServicioSocialResponse[];
total?: number; total?: number;
onPageChange?: (newPage: number) => void; onPageChange?: (newPage: number) => void;
columnaFechaInicio?: boolean; columnaFechaInicio?: boolean;
columnaFechaFin?: boolean; columnaFechaFin?: boolean;
idTipoUsuario?: number; // 1=admin, 2=responsable, 3=alumno, 4=caso especial idTipoUsuario?: number; // 1=admin, 2=responsable, 3=alumno, 4=caso especial
columnasResponsable?: boolean; columnasResponsable?: boolean;
onRowAction?: (row: ServicioSocialResponse, path: string) => void; onRowAction?: (row: ServicioSocialResponse, path: string) => void;
columnaCuestionario?: boolean; columnaCuestionario?: boolean;
columnaCartaTermino?: boolean; columnaCartaTermino?: boolean;
columnaCuestionarioCompleto?: boolean; columnaCuestionarioCompleto?: boolean;
columnaFechaRegistro?: boolean; columnaFechaRegistro?: boolean;
} }
export default function ServicioSocialTabla({ export default function ServicioSocialTabla({
data, data,
total = 0, total = 0,
onPageChange, onPageChange,
idTipoUsuario, idTipoUsuario,
columnasResponsable = false, columnasResponsable = false,
columnaFechaInicio = false, columnaFechaInicio = true,
columnaFechaFin = false, columnaFechaFin = true,
columnaCuestionario = false, columnaCuestionario = false,
columnaCartaTermino = false, columnaCartaTermino = false,
columnaCuestionarioCompleto = false, columnaCuestionarioCompleto = false,
columnaFechaRegistro = false, columnaFechaRegistro = false,
onRowAction, onRowAction,
}: Props) { }: Props) {
const [info, setInfo] = useState<ServicioSocialResponse[]>(data || []); const [info, setInfo] = useState<ServicioSocialResponse[]>(data || []);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [currentPage, setCurrentPage] = useState(1); const [currentPage, setCurrentPage] = useState(1);
const router = useRouter(); const router = useRouter();
//const columnaFechaInicio = true; // 🔹 Cargar datos del padre si existen
//const columnaFechaFin = true; useEffect(() => {
if (data && data.length > 0) {
// 🔹 Cargar datos del padre si existen setInfo(data);
useEffect(() => {
if (data && data.length > 0) {
setInfo(data);
}
}, [data]);
// 🔹 Paginación simple
function handlePrev() {
if (currentPage > 1) {
const np = currentPage - 1;
setCurrentPage(np);
onPageChange?.(np);
}
} }
}, [data]);
function handleNext() { // 🔹 Paginación simple
const lastPage = Math.max(1, Math.ceil(total / 10)); function handlePrev() {
if (currentPage < lastPage) { if (currentPage > 1) {
const np = currentPage + 1; const np = currentPage - 1;
setCurrentPage(np); setCurrentPage(np);
onPageChange?.(np); onPageChange?.(np);
}
} }
const servicioSelected = (row: ServicioSocialResponse | ServicioSocialConCasoEspecial) => {
if (!row) return;
if (idTipoUsuario === 1) {
if (row.idServicio) {
localStorage.setItem("idServicio", String(row.idServicio));
}
if ("idCasoEspecial" in row && row.idCasoEspecial) {
localStorage.setItem("idCasoEspecial", String(row.idCasoEspecial));
}
router.push("/administrador/servicio");
} else if (
row.idServicio ||
("idCasoEspecial" in row && row.idCasoEspecial)
) {
console.log("Limpiando selección (no admin)");
}
}
/*
const servicioSelected = (row: ServicioSocialResponse | ServicioSocialConCasoEspecial) => {
if (!row) return;
if (idTipoUsuario === 1) {
if (row.idServicio) {
localStorage.setItem("idServicio", String(row.idServicio));
}
// ✅ forma segura sin any
if ("idCasoEspecial" in row && row.idCasoEspecial) {
localStorage.setItem("idCasoEspecial", String(row.idCasoEspecial));
}
router.push("/administrador/servicio");
} else if (
row.idServicio ||
("idCasoEspecial" in row && row.idCasoEspecial)
) {
console.log("Limpiando selección (no admin)");
} }
};
// Codigo original function handleNext() {
const servicioSelected = (row: ServicioSocialResponse | ServicioSocialConCasoEspecial) => { const lastPage = Math.max(1, Math.ceil(total / 10));
if (!row) return; if (currentPage < lastPage) {
const np = currentPage + 1;
setCurrentPage(np);
onPageChange?.(np);
}
}
if (idTipoUsuario === 1) { const servicioSelected = (
row: ServicioSocialResponse | ServicioSocialConCasoEspecial
) => {
if (!row) return;
if (row.idServicio) { if (idTipoUsuario === 1) {
localStorage.setItem("idServicio", String(row.idServicio)); if (row.idServicio) {
} localStorage.setItem("idServicio", String(row.idServicio));
if ((row as any).idCasoEspecial) { }
localStorage.setItem("idCasoEspecial", String((row as any).idCasoEspecial));
}
// Redirigir a la vista de detalle if ("idCasoEspecial" in row && row.idCasoEspecial) {
router.push("/administrador/servicio"); localStorage.setItem("idCasoEspecial", String(row.idCasoEspecial));
} else if (row.idServicio || (row as any).idCasoEspecial) { }
console.log("Limpiando selección (no admin)");
} router.push("/administrador/servicio");
}; } else if (
row.idServicio ||
("idCasoEspecial" in row && row.idCasoEspecial)
) {
console.log("Limpiando selección (no admin)");
}
};
*/ return (
<section>
return ( {loading ? (
<section> <div>Cargando...</div>
{loading ? ( ) : (
<div>Cargando...</div> <>
) : ( <div className="table-responsive">
<> <table className="table table-striped">
<div className="table-responsive"> <thead>
<table className="table table-striped"> <tr>
<thead> {columnaFechaRegistro && <th>Fecha Registro</th>}
<tr> <th>Número de Cuenta</th>
{columnaFechaRegistro && <th>Fecha Registro</th>} <th>Nombre</th>
<th>Número de Cuenta</th> <th>Carrera</th>
<th>Nombre</th> {columnaFechaInicio && <th>Fecha Inicio</th>}
<th>Carrera</th> {columnaFechaFin && <th>Fecha Fin</th>}
{columnaFechaInicio && <th>Fecha Inicio</th>} <th>Status</th>
{columnaFechaFin && <th>Fecha Fin</th>} {columnaCuestionarioCompleto && (
<th>Status</th> <th>Cuestionario Completo</th>
{columnaCuestionarioCompleto && <th>Cuestionario Completo</th>} )}
{columnaCartaTermino && <th>Carta Termino</th>} {columnaCartaTermino && <th>Carta Termino</th>}
{columnaCuestionario && <th>Cuestionario</th>} {columnaCuestionario && <th>Cuestionario</th>}
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{info.map((item, index) => ( {info.map((item, index) => (
<tr <tr
key={index} key={index}
style={{ style={{
cursor: idTipoUsuario === 1 ? "pointer" : "default", cursor: idTipoUsuario === 1 ? "pointer" : "default",
}} }}
onClick={() => ( onClick={() =>
idTipoUsuario === 1 ? servicioSelected(item) : undefined idTipoUsuario === 1 ? servicioSelected(item) : undefined
)}
//onClick={() => Falta corregir
//idTipoUsuario === 1 ? servicioSelected(item) : undefined
//}
>
<td>{item.Usuario?.usuario}</td>
<td>{item.Usuario?.nombre}</td>
<td>{item.Carrera?.carrera}</td>
{columnaFechaInicio && (
<td>{formatDate(item.fecha_inicio)}</td>
)}
{columnaFechaFin && <td>{formatDate(item.fecha_fin)}</td>}
<td>
{columnasResponsable && item.Status?.idStatus === 7 ? (
<button
className="btn btn-sm btn-danger"
onClick={() =>
onRowAction?.(item, "carta_aceptacion")
}
>
Carta Aceptación Rechazada
</button>
) : (
<span
className={`badge bg-${statusClass(
item.Status?.idStatus
)}`}
>
{item.Status?.status}
</span>
)}
</td>
{columnaCartaTermino && (
<td>{item.cartaTermino ? (
<span className="badge bg-success">Completado</span>
) : (
<span className="badge bg-danger">No Completado</span>
)}</td>
)}
{columnaCuestionarioCompleto && (
<td>{item.cuestionarioCompletado ? (
<span className="badge bg-success">Completado</span>
) : (
<span className="badge bg-danger">No Completado</span>
)}</td>
)}
{columnaCuestionario && (
<td>
{item.cuestionarioCompletado ? (
<span className="badge bg-success">Completado</span>
) : (
<span className="badge bg-danger">No Completado</span>
)}
</td>
)}
</tr>
))}
{info.length === 0 && (
<tr>
<td colSpan={7} className="text-center">
No hay registros
</td>
</tr>
)}
</tbody>
</table>
</div>
<div className="d-flex justify-content-between align-items-center mt-2">
<div>Total: {total}</div>
<div>
<button
className="btn btn-sm btn-outline-primary me-2"
onClick={handlePrev}
disabled={currentPage <= 1}
>
Anterior
</button>
<span>Página {currentPage}</span>
<button
className="btn btn-sm btn-outline-primary ms-2"
onClick={handleNext}
disabled={
currentPage >= Math.max(1, Math.ceil(total / 10))
} }
> >
Siguiente {/* Si el padre pidió mostrar Fecha Registro, la mostramos aquí (createdAt) */}
</button> {columnaFechaRegistro && (
</div> <td>{formatDate((item as any).createdAt)}</td>
)}
<td>{item.Usuario?.usuario}</td>
<td>{item.Usuario?.nombre}</td>
<td>{item.Carrera?.carrera}</td>
{/* Usar las propiedades en camelCase que vienen en tu JSON */}
{columnaFechaInicio && (
<td>{formatDate((item as any).fechaInicio)}</td>
)}
{columnaFechaFin && (
<td>{formatDate((item as any).fechaFin)}</td>
)}
<td>
{columnasResponsable && item.Status?.idStatus === 7 ? (
<button
className="btn btn-sm btn-danger"
onClick={() =>
onRowAction?.(item, "carta_aceptacion")
}
>
Carta Aceptación Rechazada
</button>
) : (
<span
className={`badge bg-${statusClass(
item.Status?.idStatus
)}`}
>
{item.Status?.status}
</span>
)}
</td>
{columnaCartaTermino && (
<td>
{item.cartaTermino ? (
<span className="badge bg-success">Completado</span>
) : (
<span className="badge bg-danger">No Completado</span>
)}
</td>
)}
{columnaCuestionarioCompleto && (
<td>
{item.cuestionarioCompletado ? (
<span className="badge bg-success">Completado</span>
) : (
<span className="badge bg-danger">No Completado</span>
)}
</td>
)}
{columnaCuestionario && (
<td>
{item.cuestionarioCompletado ? (
<span className="badge bg-success">Completado</span>
) : (
<span className="badge bg-danger">No Completado</span>
)}
</td>
)}
</tr>
))}
{info.length === 0 && (
<tr>
<td colSpan={7} className="text-center">
No hay registros
</td>
</tr>
)}
</tbody>
</table>
</div>
<div className="d-flex justify-content-between align-items-center mt-2">
<div>Total: {total}</div>
<div>
<button
className="btn btn-sm btn-outline-primary me-2"
onClick={handlePrev}
disabled={currentPage <= 1}
>
Anterior
</button>
<span>Página {currentPage}</span>
<button
className="btn btn-sm btn-outline-primary ms-2"
onClick={handleNext}
disabled={currentPage >= Math.max(1, Math.ceil(total / 10))}
>
Siguiente
</button>
</div> </div>
</> </div>
)} </>
</section> )}
); </section>
);
} }
function formatDate(value?: string | null): string { function formatDate(value?: string | null): string {
if (!value) return ""; if (!value) return "";
// Si ya viene como fecha ISO, forzamos a hora local sin modificar el día const d = new Date(value.includes("T") ? value : `${value}T00:00:00`);
const d = new Date(value.includes("T") ? value : `${value}T00:00:00`);
if (isNaN(d.getTime())) { if (isNaN(d.getTime())) {
console.warn("⚠️ Fecha inválida:", value); console.warn("⚠️ Fecha inválida:", value);
return ""; return "";
} }
const day = String(d.getDate()).padStart(2, "0"); const day = String(d.getDate()).padStart(2, "0");
const month = String(d.getMonth() + 1).padStart(2, "0"); const month = String(d.getMonth() + 1).padStart(2, "0");
const year = d.getFullYear(); const year = d.getFullYear();
return `${day}/${month}/${year}`; return `${day}/${month}/${year}`;
} }
function statusClass(id?: number) { function statusClass(id?: number) {
switch (id) { switch (id) {
case 1: case 1:
return "dark"; return "dark";
case 2: case 2:
return "info"; return "info";
case 3: case 3:
return "warning"; return "warning";
case 4: case 4:
return "primary"; return "primary";
case 5: case 5:
case 6: case 6:
return "success"; return "success";
case 7: case 7:
case 8: case 8:
case 9: case 9:
case 10: case 10:
return "danger"; return "danger";
default: default:
return "secondary"; return "secondary";
} }
} }