2025-09-26 02:43:46 -06:00
|
|
|
"use client";
|
2026-01-13 19:08:23 -06:00
|
|
|
import { envConfig } from "@/app/lib/config";
|
2025-10-14 11:48:43 -06:00
|
|
|
import axios from "axios";
|
2026-01-13 19:08:23 -06:00
|
|
|
import { useEffect, useState } from "react";
|
2026-02-18 19:01:31 -06:00
|
|
|
import Swal from "sweetalert2";
|
2025-09-10 13:09:26 -06:00
|
|
|
|
2026-01-21 11:58:32 -06:00
|
|
|
interface Programa {
|
|
|
|
|
id_programa: number;
|
|
|
|
|
programa: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 18:27:33 -06:00
|
|
|
export default function ProgramSelector({
|
|
|
|
|
ubicacion,
|
|
|
|
|
id,
|
|
|
|
|
}: {
|
|
|
|
|
ubicacion?: string | null;
|
|
|
|
|
id?: number | null;
|
|
|
|
|
}) {
|
2026-01-21 11:58:32 -06:00
|
|
|
const [programas, setProgramas] = useState<Programa[]>([]);
|
|
|
|
|
const [checkboxes, setCheckboxes] = useState<Record<number, boolean>>({});
|
2026-01-13 19:08:23 -06:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-01-21 11:58:32 -06:00
|
|
|
const fetchData = async () => {
|
2026-01-27 18:27:33 -06:00
|
|
|
const [programasRes] = await Promise.all([
|
2026-01-21 11:58:32 -06:00
|
|
|
axios.get(`${envConfig.apiUrl}/programa`),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
setProgramas(programasRes.data);
|
|
|
|
|
|
|
|
|
|
const initialCheckboxes: Record<number, boolean> = {};
|
|
|
|
|
programasRes.data.forEach((p: Programa) => {
|
|
|
|
|
initialCheckboxes[p.id_programa] = false;
|
|
|
|
|
});
|
|
|
|
|
setCheckboxes(initialCheckboxes);
|
2026-01-13 19:08:23 -06:00
|
|
|
};
|
2026-01-21 11:58:32 -06:00
|
|
|
|
|
|
|
|
fetchData();
|
2026-01-13 19:08:23 -06:00
|
|
|
}, []);
|
2025-09-05 20:54:29 -04:00
|
|
|
|
2026-01-21 11:58:32 -06:00
|
|
|
const handleTogglePrograma = (id: number) => {
|
|
|
|
|
setCheckboxes((prev) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
[id]: !prev[id],
|
|
|
|
|
}));
|
2025-09-26 02:43:46 -06:00
|
|
|
};
|
|
|
|
|
|
2026-01-29 11:19:54 -06:00
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
2025-09-26 02:43:46 -06:00
|
|
|
e.preventDefault();
|
2026-01-21 11:58:32 -06:00
|
|
|
|
|
|
|
|
const programasSeleccionados = Object.keys(checkboxes)
|
|
|
|
|
.filter((id) => checkboxes[Number(id)])
|
|
|
|
|
.map(Number);
|
|
|
|
|
|
2026-01-29 11:19:54 -06:00
|
|
|
try {
|
|
|
|
|
if (ubicacion && ubicacion !== "0") {
|
|
|
|
|
await axios.patch(
|
|
|
|
|
`${envConfig.apiUrl}/programa-equipo/equipo/${ubicacion}`,
|
|
|
|
|
{
|
|
|
|
|
programas: programasSeleccionados,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-02-20 14:33:08 -05:00
|
|
|
|
|
|
|
|
Swal.fire({
|
|
|
|
|
title: "Programas actualizados en la máquina!",
|
|
|
|
|
icon: "success",
|
|
|
|
|
draggable: true
|
|
|
|
|
});
|
2026-01-29 11:19:54 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
|
await axios.patch(`${envConfig.apiUrl}/programa-equipo/sala`, {
|
|
|
|
|
sala: id,
|
|
|
|
|
programas: programasSeleccionados,
|
|
|
|
|
});
|
2026-01-28 19:06:35 -06:00
|
|
|
|
2026-02-20 14:33:08 -05:00
|
|
|
|
|
|
|
|
Swal.fire({
|
|
|
|
|
title: "Programas actualizados en la sala",
|
|
|
|
|
icon: "success",
|
|
|
|
|
draggable: true
|
|
|
|
|
});
|
2026-01-29 11:19:54 -06:00
|
|
|
return;
|
|
|
|
|
}
|
2026-01-28 19:06:35 -06:00
|
|
|
|
2026-02-20 14:33:08 -05:00
|
|
|
Swal.fire({
|
|
|
|
|
title: "No se pudo determinar el destino!",
|
|
|
|
|
icon: "error",
|
|
|
|
|
draggable: true
|
|
|
|
|
});
|
2026-01-29 11:19:54 -06:00
|
|
|
} catch (error) {
|
2026-02-20 14:33:08 -05:00
|
|
|
|
|
|
|
|
|
2026-02-18 19:01:31 -06:00
|
|
|
Swal.fire({
|
|
|
|
|
title: "Error al guardar cambios!",
|
2026-02-20 14:33:08 -05:00
|
|
|
icon: "error",
|
2026-02-18 19:01:31 -06:00
|
|
|
draggable: true,
|
|
|
|
|
});
|
2026-01-29 11:19:54 -06:00
|
|
|
}
|
2025-09-26 02:43:46 -06:00
|
|
|
};
|
2025-10-08 11:40:11 -06:00
|
|
|
|
2026-01-27 18:27:33 -06:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!programas.length) return;
|
2026-01-28 16:40:39 -06:00
|
|
|
if (ubicacion === null && id === null) return;
|
2026-01-27 18:27:33 -06:00
|
|
|
|
|
|
|
|
const buildCheckboxes = async () => {
|
|
|
|
|
const baseCheckboxes: Record<number, boolean> = {};
|
|
|
|
|
programas.forEach((p) => {
|
|
|
|
|
baseCheckboxes[p.id_programa] = false;
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-27 18:33:17 -06:00
|
|
|
try {
|
|
|
|
|
let res;
|
|
|
|
|
|
2026-01-28 16:40:39 -06:00
|
|
|
if (ubicacion && ubicacion !== "0") {
|
2026-01-27 18:33:17 -06:00
|
|
|
res = await axios.get(
|
2026-01-27 18:27:33 -06:00
|
|
|
`${envConfig.apiUrl}/programa-equipo/${ubicacion}`,
|
|
|
|
|
);
|
2026-01-27 18:33:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (id) {
|
2026-01-28 16:40:39 -06:00
|
|
|
res = await axios.get(
|
|
|
|
|
`${envConfig.apiUrl}/area-ubicacion/programs/${id}`,
|
|
|
|
|
);
|
2026-01-27 18:33:17 -06:00
|
|
|
}
|
2026-01-27 18:27:33 -06:00
|
|
|
|
2026-01-27 18:33:17 -06:00
|
|
|
res?.data.forEach((item: any) => {
|
|
|
|
|
baseCheckboxes[item.id_programa] = true;
|
|
|
|
|
});
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
if (axios.isAxiosError(error)) {
|
|
|
|
|
if (error.response?.status === 404) {
|
2026-02-20 14:33:08 -05:00
|
|
|
|
|
|
|
|
Swal.fire({
|
|
|
|
|
title: "Sin programas asignados!",
|
|
|
|
|
icon: "error",
|
|
|
|
|
draggable: true
|
|
|
|
|
});
|
2026-01-27 18:27:33 -06:00
|
|
|
} else {
|
2026-02-20 14:33:08 -05:00
|
|
|
|
|
|
|
|
Swal.fire({
|
|
|
|
|
title: "Error al cargar programas!",
|
|
|
|
|
icon: "error",
|
|
|
|
|
draggable: true
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-27 18:27:33 -06:00
|
|
|
}
|
2026-01-27 18:33:17 -06:00
|
|
|
} else {
|
2026-02-20 14:33:08 -05:00
|
|
|
|
|
|
|
|
Swal.fire({
|
|
|
|
|
title: "Error inesperado!",
|
|
|
|
|
icon: "error",
|
|
|
|
|
draggable: true
|
|
|
|
|
});
|
2026-01-27 18:27:33 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setCheckboxes(baseCheckboxes);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
buildCheckboxes();
|
2026-01-27 18:33:17 -06:00
|
|
|
}, [programas, ubicacion, id]);
|
2026-01-27 18:27:33 -06:00
|
|
|
|
2025-09-26 02:43:46 -06:00
|
|
|
return (
|
|
|
|
|
<form className="form-container" onSubmit={handleSubmit}>
|
2026-02-20 14:33:08 -05:00
|
|
|
<div className="checkbox-grid" style={{ gridAutoFlow: "column" }}>
|
2025-09-26 02:43:46 -06:00
|
|
|
{programas.map((prog) => (
|
2026-01-21 11:58:32 -06:00
|
|
|
<label key={prog.id_programa}>
|
2025-09-26 02:43:46 -06:00
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
2026-01-21 11:58:32 -06:00
|
|
|
checked={checkboxes[prog.id_programa] || false}
|
|
|
|
|
onChange={() => handleTogglePrograma(prog.id_programa)}
|
2025-09-26 02:43:46 -06:00
|
|
|
/>
|
2026-01-21 11:58:32 -06:00
|
|
|
{prog.programa}
|
2025-09-26 02:43:46 -06:00
|
|
|
</label>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
2025-09-10 13:09:26 -06:00
|
|
|
|
2025-09-26 02:43:46 -06:00
|
|
|
<button className="button buttonSearch" type="submit">
|
|
|
|
|
Guardar cambios
|
|
|
|
|
</button>
|
|
|
|
|
</form>
|
2025-09-10 13:09:26 -06:00
|
|
|
);
|
2026-01-21 11:58:32 -06:00
|
|
|
}
|
2026-02-18 17:50:15 -06:00
|
|
|
//IO
|