Files
front-Censo/src/components/Equipo_Computo/Pregunta7.tsx
T

172 lines
4.6 KiB
TypeScript
Raw Normal View History

2025-11-11 17:51:13 -06:00
"use client";
2025-11-21 12:11:37 -06:00
import { useState, useEffect } from "react";
2025-11-20 18:53:55 -06:00
import Cookies from "js-cookie";
2025-11-21 12:11:37 -06:00
import axios from "axios";
2025-11-18 13:53:21 -06:00
import "./pregunta7.css";
2025-11-28 11:13:57 -06:00
import ToggleButton from "../Toggle/ToggleButton";
2025-11-11 17:51:13 -06:00
2025-11-21 12:11:37 -06:00
type AntiguedadItem = {
antiguedad: string | null;
total: number | string;
};
2025-11-20 18:53:55 -06:00
2025-11-21 12:11:37 -06:00
type RespuestaAntiguedad = {
2025-11-20 18:53:55 -06:00
impresion: AntiguedadItem[];
digitalizacion: AntiguedadItem[];
2025-11-21 12:11:37 -06:00
};
2025-11-20 18:53:55 -06:00
2025-11-18 13:53:21 -06:00
export default function Pregunta7() {
2025-11-11 17:51:13 -06:00
const [datos, setDatos] = useState([
2025-11-27 15:28:59 -06:00
{ nombre: "Impresión", valores: ["0.00", "0.00", "0.00", "0.00"] },
2025-11-21 12:11:37 -06:00
{
nombre: "Digitalización",
2025-11-28 16:19:36 -06:00
valores: ["0.00", "0.00", "0.00", "0.00"],
2025-11-21 12:11:37 -06:00
},
2025-11-11 17:51:13 -06:00
]);
2025-11-21 12:11:37 -06:00
const [loading, setLoading] = useState(true);
2025-11-20 18:53:55 -06:00
2025-11-21 12:11:37 -06:00
const api_url = process.env.NEXT_PUBLIC_API_URL;
2025-11-11 17:51:13 -06:00
2025-11-21 12:11:37 -06:00
// ------------------------------
// TRANSFORMADOR (maneja null)
// ------------------------------
const transformarDatos = (items: AntiguedadItem[]): string[] => {
let menores2 = 0;
let entre2_3 = 0;
let entre4_5 = 0;
let mayores6 = 0;
let notfound = 0;
for (const item of items) {
const total = Number(item.total) || 0;
2025-11-27 15:28:59 -06:00
const antig = item.antiguedad
? item.antiguedad.toUpperCase().trim()
: null;
2025-11-21 12:11:37 -06:00
switch (antig) {
case "MENORES DE 2":
menores2 += total;
break;
case "ENTRE 2 Y 3":
entre2_3 += total;
break;
case "ENTRE 4 Y 5":
entre4_5 += total;
break;
case "ENTRE 6 Y MAYORES":
mayores6 += total;
break;
default:
notfound += total; // incluye null
}
}
const total = menores2 + entre2_3 + entre4_5 + mayores6 + notfound;
2025-11-27 15:28:59 -06:00
if (total === 0) return ["0.00", "0.00", "0.00", "0.00"];
2025-11-21 12:11:37 -06:00
const pct = (v: number) => ((v / total) * 100).toFixed(2);
2025-11-28 16:19:36 -06:00
return [pct(menores2), pct(entre2_3), pct(entre4_5), pct(mayores6)];
2025-11-21 12:11:37 -06:00
};
2025-11-28 19:28:07 -06:00
const cargarDatos = async (baja: boolean) => {
2025-11-20 18:53:55 -06:00
const token = Cookies.get("token");
2025-11-28 19:28:07 -06:00
const body = baja ? ["BAJA"] : ["EN DESUSO", "EN USO"];
2025-11-20 18:53:55 -06:00
axios
2025-11-28 16:19:36 -06:00
.post<RespuestaAntiguedad>(
2025-11-21 12:11:37 -06:00
`${api_url}/equipos/reporte/contar_perifericos_antiguedad`,
2025-11-28 19:28:07 -06:00
body,
2025-11-21 12:11:37 -06:00
{
headers: { Authorization: `Bearer ${token}` },
2025-11-20 18:53:55 -06:00
}
2025-11-21 12:11:37 -06:00
)
.then((res) => {
const { impresion, digitalizacion } = res.data;
2025-11-20 18:53:55 -06:00
2025-11-21 12:11:37 -06:00
setDatos([
{ nombre: "Impresión", valores: transformarDatos(impresion) },
2025-11-27 15:28:59 -06:00
{
nombre: "Digitalización",
valores: transformarDatos(digitalizacion),
},
2025-11-21 12:11:37 -06:00
]);
2025-11-20 18:53:55 -06:00
})
2025-11-21 12:11:37 -06:00
.catch((err) => console.error("Error cargando datos:", err))
.finally(() => setLoading(false));
2025-11-28 19:28:07 -06:00
};
useEffect(() => {
cargarDatos(false);
2025-11-20 18:53:55 -06:00
}, []);
2025-11-21 12:11:37 -06:00
const calcularTotalFila = (valores: string[]) =>
valores.reduce((t, v) => t + Number(v), 0);
2025-11-11 17:51:13 -06:00
return (
<div className="contenedor-pregunta">
2025-11-27 15:28:59 -06:00
<div className="contenedor-censo">
Censo de equipos periféricos - Equipo de digitalización
</div>
<div className="pregunta-cuadro">
2025-11-28 16:19:36 -06:00
Antigüedad que tienen los equipos periféricos del área universitaria.
2025-11-28 19:28:07 -06:00
<ToggleButton
onChange={(estado) => {
cargarDatos(estado);
}}
/>
2025-11-18 13:53:21 -06:00
</div>
2025-11-11 17:51:13 -06:00
<div className="tabla-contenedor">
<table className="tabla">
<thead>
<tr>
2025-11-21 12:11:37 -06:00
<th className="azul-marino">% Antigüedad de los equipos</th>
2025-11-11 17:51:13 -06:00
<th className="rosa-fuerte">Menor a 2 años</th>
<th className="rosa-fuerte">Entre 2 y 3 años</th>
<th className="rosa-fuerte">Entre 4 y 5 años</th>
<th className="rosa-fuerte">Mayor a 6 años</th>
2025-11-27 15:28:59 -06:00
2025-11-11 17:51:13 -06:00
<th className="azul-marino">Total</th>
</tr>
</thead>
2025-11-21 12:11:37 -06:00
2025-11-11 17:51:13 -06:00
<tbody>
2025-11-21 12:11:37 -06:00
{datos.map((fila, idx) => {
const total = calcularTotalFila(fila.valores);
2025-11-20 18:53:55 -06:00
2025-11-11 17:51:13 -06:00
return (
2025-11-21 12:11:37 -06:00
<tr key={idx}>
2025-11-11 17:51:13 -06:00
<td>{fila.nombre}</td>
2025-11-20 18:53:55 -06:00
2025-11-21 12:11:37 -06:00
{fila.valores.map((v, c) => (
<td key={c}>
2025-11-11 17:51:13 -06:00
<div className="input-contenedor">
2025-11-21 12:11:37 -06:00
<input type="number" value={v} readOnly />
2025-11-11 17:51:13 -06:00
<span className="porcentaje">%</span>
</div>
</td>
))}
2025-11-20 18:53:55 -06:00
2025-11-27 15:28:59 -06:00
<td
className={`total-celda ${
total > 100.1 ? "total-error" : ""
}`}
>
2025-11-11 17:51:13 -06:00
{total.toFixed(2)}%
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</div>
);
}