"use client"; import React, { useState } from "react"; import { useSelector, useDispatch } from "react-redux"; import { RootState } from "../../../../../redux/store"; import { setKueData } from "../../../../../redux/slices/kueDataSlice"; import { useAdminAuth } from "../../../settingsPageComponents/hooks/useAdminAuth"; type KvzData = { // Hier können später weitere KVz-spezifische Einstellungen hinzugefügt werden kvzSettings: string; }; declare global { interface Window { __kvzCache?: Record; } } interface Props { slot: number; onClose?: () => void; } export default function KvzModalView({ slot, onClose }: Props) { const { isAdminLoggedIn } = useAdminAuth(true); const dispatch = useDispatch(); const kvzSlice = useSelector((state: RootState) => state.kueDataSlice); const cacheKey = `kvz_slot_${slot}`; if (typeof window !== "undefined") { window.__kvzCache = window.__kvzCache || {}; } const cachedKvz = typeof window !== "undefined" ? window.__kvzCache?.[cacheKey] ?? null : null; const [kvzData] = useState( () => cachedKvz?.data || { kvzSettings: "", // Placeholder für zukünftige Einstellungen } ); const [kvzActive, setKvzActive] = useState( () => cachedKvz?.kvzActive ?? kvzSlice.win_fallSensorsActive?.[slot] === 1 ); const updateCache = (data: typeof kvzData, active = kvzActive) => { if (typeof window !== "undefined") { (window.__kvzCache ??= {})[cacheKey] = { data, kvzActive: active, }; } }; const handleKvzToggle = () => { const newState = !kvzActive; setKvzActive(newState); updateCache(kvzData, newState); // Redux State sofort aktualisieren für UI-Update const updatedKvzActive = [...(kvzSlice.win_fallSensorsActive || [])]; updatedKvzActive[slot] = newState ? 1 : 0; dispatch(setKueData({ win_fallSensorsActive: updatedKvzActive })); const isDev = window.location.hostname === "localhost"; const slotParam = `KVZ${slot}=${newState ? 1 : 0}`; const reloadAfterConfirm = () => { const msg = newState ? "✅ KVz wurde aktiviert." : "⚠️ KVz wurde deaktiviert."; alert(msg); location.reload(); }; if (isDev) { const updates = [ { key: "win_fallSensorsActive", slot, value: newState ? 1 : 0 }, ]; fetch("/api/cpl/updateKvzSettingsDataAPIHandler", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ updates }), }) .then((res) => res.json()) .then(() => { console.log("KVz-Aktiv-Status gespeichert."); reloadAfterConfirm(); }) .catch((err) => { console.error("Fehler beim Speichern von KVz aktiv:", err); }); } else { const url = `${window.location.origin}/CPL?/kabelueberwachung.html&${slotParam}`; fetch(url) .then((res) => { if (!res.ok) throw new Error("KVz-Befehl fehlgeschlagen"); console.log("KVz aktiviert/deaktiviert:", res.status); reloadAfterConfirm(); }) .catch((err) => { console.error("Fehler beim KVz-Befehl:", err); alert("Fehler beim Umschalten der KVz-Funktion."); }); } }; const handleSave = () => { const isDev = window.location.hostname === "localhost"; if (isDev) { const updates = [ { key: "win_fallSensorsActive", slot, value: kvzActive ? 1 : 0 }, // Hier können später weitere KVz-Einstellungen hinzugefügt werden ]; fetch("/api/cpl/updateKvzSettingsDataAPIHandler", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ updates }), }) .then((res) => res.json()) .then(() => { alert("KVz-Einstellungen erfolgreich gespeichert."); if (typeof onClose === "function") onClose(); }) .catch((err) => { console.error("Fehler beim Speichern:", err); alert("Speichern fehlgeschlagen."); }); } else { // Originaler Webservice-Teil für Produktionsumgebung alert("KVz-Einstellungen gespeichert."); if (typeof onClose === "function") onClose(); } updateCache(kvzData, kvzActive); }; return (
{/* KVz-Funktion */} {isAdminLoggedIn && (
KVz-Funktion:
{kvzActive ? "aktiviert" : "deaktiviert"}
)} {/* Zukünftige KVz-Einstellungen können hier hinzugefügt werden */} {!isAdminLoggedIn && (

Nur Admin-Benutzer können diese Einstellungen ändern.

)} {/* Speichern Button */} {/*
*/}
); }