81 lines
3.3 KiB
JavaScript
81 lines
3.3 KiB
JavaScript
"use client";
|
|
import { useEffect } from "react";
|
|
import { useRouter, usePathname, useSearchParams } from "next/navigation";
|
|
|
|
const addDays = (iso, n) => {
|
|
const d = new Date(iso + "T12:00:00Z");
|
|
d.setUTCDate(d.getUTCDate() + n);
|
|
return d.toISOString().slice(0, 10);
|
|
};
|
|
|
|
// Selector de fecha + botones de PDF del Plan del Día. La fecha vive en la URL (`fecha`), igual que
|
|
// el resto de filtros, así el enlace se puede compartir. El PDF sale del diálogo de impresión del
|
|
// navegador ("Guardar como PDF"); `body.plan-print` oculta menú y barra superior (layout.css) y
|
|
// `plan-print-equipo` además deja solo el resumen del equipo. El título del documento se cambia
|
|
// mientras imprime porque el navegador lo usa de nombre de archivo.
|
|
export default function PlanControles({ fecha, hoy, tituloPdf, tituloPdfEquipo, conEquipo }) {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const searchParams = useSearchParams();
|
|
|
|
function irA(nueva) {
|
|
const params = new URLSearchParams(searchParams.toString());
|
|
if (!nueva || nueva === hoy) params.delete("fecha");
|
|
else params.set("fecha", nueva);
|
|
const qs = params.toString();
|
|
router.push(qs ? `${pathname}?${qs}` : pathname, { scroll: false });
|
|
}
|
|
|
|
// Si por algún motivo el diálogo se cierra sin disparar afterprint, no se deja el modo impresión puesto.
|
|
useEffect(() => () => document.body.classList.remove("plan-print", "plan-print-equipo"), []);
|
|
|
|
function imprimir(equipo) {
|
|
const tituloPrevio = document.title;
|
|
document.title = equipo ? tituloPdfEquipo : tituloPdf;
|
|
document.body.classList.add("plan-print");
|
|
if (equipo) document.body.classList.add("plan-print-equipo");
|
|
// Hoja tamaño Carta (Letter) horizontal con márgenes chicos, solo mientras se imprime el plan
|
|
// (los demás informes imprimibles son Carta vertical, ver layout.css). El estilo se inserta
|
|
// justo antes de abrir el diálogo y se retira al terminar.
|
|
const hoja = document.createElement("style");
|
|
hoja.id = "plan-hoja";
|
|
hoja.textContent = "@page{size:letter landscape;margin:8mm 9mm;}";
|
|
document.head.appendChild(hoja);
|
|
const limpiar = () => {
|
|
document.body.classList.remove("plan-print", "plan-print-equipo");
|
|
document.title = tituloPrevio;
|
|
document.getElementById("plan-hoja")?.remove();
|
|
window.removeEventListener("afterprint", limpiar);
|
|
};
|
|
window.addEventListener("afterprint", limpiar);
|
|
window.print();
|
|
}
|
|
|
|
return (
|
|
<div className="plan-controles no-print">
|
|
<button type="button" className="ag-nav-b" aria-label="Día anterior" onClick={() => irA(addDays(fecha, -1))}>
|
|
←
|
|
</button>
|
|
<input
|
|
type="date"
|
|
className="plan-fecha"
|
|
value={fecha}
|
|
onChange={(e) => e.target.value && irA(e.target.value)}
|
|
/>
|
|
<button type="button" className="ag-nav-b" aria-label="Día siguiente" onClick={() => irA(addDays(fecha, 1))}>
|
|
→
|
|
</button>
|
|
<button type="button" className="ag-nav-b" onClick={() => irA(hoy)} disabled={fecha === hoy}>
|
|
Hoy
|
|
</button>
|
|
<button type="button" className="rep-btn" onClick={() => imprimir(false)}>
|
|
🖨 Exportar PDF
|
|
</button>
|
|
{conEquipo && (
|
|
<button type="button" className="rep-btn" onClick={() => imprimir(true)}>
|
|
🖨 PDF del equipo
|
|
</button>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|