import React, { useState, useEffect } from "react"; // omponents/modales/settingsModal.jsx
import ReactModal from "react-modal";
import { ClipLoader } from "react-spinners";
import "bootstrap-icons/font/bootstrap-icons.css"; // Import Bootstrap Icons
import { useSelector } from "react-redux";
import { current } from "@reduxjs/toolkit";
// Definiere das App-Element für ReactModal
ReactModal.setAppElement("#__next");
function SettingModal({ showModal, onClose }) {
// Redux-Werte abrufen
const deviceName_Redux = useSelector((state) => state.variables.deviceName);
const mac1_Redux = useSelector((state) => state.variables.mac1);
const ip_Redux = useSelector((state) => state.variables.ip);
const subnet_Redux = useSelector((state) => state.variables.subnet);
const gateway_Redux = useSelector((state) => state.variables.gateway);
const datetime_Redux = useSelector(
(state) => state.variables.cplInternalTimestamp
);
const ntp1_Redux = useSelector((state) => state.variables.ntp1);
const ntp2_Redux = useSelector((state) => state.variables.ntp2);
const ntp3_Redux = useSelector((state) => state.variables.ntp3);
const ntpTimezone_Redux = useSelector((state) => state.variables.ntpTimezone);
const active_Redux = useSelector((state) => state.variables.ntpActive);
// Lokale State-Variablen zum Bearbeiten
const [name, setName] = useState(deviceName_Redux || "");
const [mac1, setMac1] = useState(mac1_Redux || "");
const [ip, setIp] = useState(ip_Redux || "");
const [subnet, setSubnet] = useState(subnet_Redux || "");
const [gateway, setGateway] = useState(gateway_Redux || "");
const [systemUhr, setSystemUhr] = useState(datetime_Redux || "");
const [ntp1, setNtp1] = useState(ntp1_Redux || "");
const [ntp2, setNtp2] = useState(ntp2_Redux || "");
const [ntp3, setNtp3] = useState(ntp3_Redux || "");
const [ntpTimezone, setNtpTimezone] = useState(ntpTimezone_Redux || "");
const [active, setActive] = useState(active_Redux || "");
const [showRebootModal, setShowRebootModal] = useState(false);
// Originalwerte speichern
const [originalValues, setOriginalValues] = useState({});
// Effekt, um Redux-Werte beim Anzeigen des Modals in lokale State-Variablen zu setzen
// Initialisiere currentPath für die gesamte Datei
let currentPath = window.location.pathname;
if (!currentPath.endsWith(".html")) {
currentPath += ".html";
}
const handleReboot = () => {
if (
window.confirm("Sind Sie sicher, dass Sie den CPL neu starten möchten?")
) {
// Get the current path and ensure it ends with ".html"
let currentPath = window.location.pathname;
if (!currentPath.endsWith(".html")) {
currentPath += ".html";
}
// Full URL with host, current path, and reboot command
const url = `${window.location.origin}/CPL?${currentPath}&BOOT=1`;
// Log the full URL to the console for debugging
console.log(url);
// Send the reboot command to the server using fetch and GET method
fetch(url, { method: "GET" }).finally(() => {
window.location.href = `/wait`; // Redirect to wait page on client side
});
}
};
const handleClearDatabase = async () => {
const confirmClear = window.confirm(
"Sind Sie sicher, dass Sie die Datenbank leeren möchten?"
);
if (!confirmClear) return;
// Get the current path and ensure it ends with ".html"
let currentPath = window.location.pathname;
if (!currentPath.endsWith(".html")) {
currentPath += ".html";
}
// Full URL with host, current path, and clear database command
const url = `${window.location.origin}/CPL?${currentPath}&DEDB=1`;
// Log the full URL to the console for debugging
console.log(url);
// Send the clear database command to the server using fetch and GET method
try {
const response = await fetch(url, { method: "GET" });
if (response.ok) {
alert("Datenbank erfolgreich geleert!");
} else {
alert("Fehler beim Leeren der Datenbank!");
}
} catch (error) {
console.error("Fehler:", error);
alert("Fehler beim Leeren der Datenbank!");
}
};
const handleSubmit = () => {
const changes = {};
// Überprüfe, welche Werte sich geändert haben
if (name !== originalValues.name) {
changes.SNNA = name;
}
if (ip !== originalValues.ip) {
changes.SEI01 = ip;
}
if (subnet !== originalValues.subnet) {
changes.SEI02 = subnet;
}
if (gateway !== originalValues.gateway) {
changes.SEI03 = gateway;
}
if (ntp1 !== originalValues.ntp1) {
changes.SNIP1 = ntp1;
}
if (ntp2 !== originalValues.ntp2) {
changes.SNIP2 = ntp2;
}
if (ntp3 !== originalValues.ntp3) {
changes.SNIP3 = ntp3;
}
if (ntpTimezone !== originalValues.ntpTimezone) {
changes.SNTZ = ntpTimezone;
}
if (active !== originalValues.active) {
changes.SNAC = active;
}
// Falls Änderungen vorhanden sind, sende die neuen Daten
if (Object.keys(changes).length > 0) {
// Get the current path and ensure it ends with ".html"
let currentPath = window.location.pathname;
if (!currentPath.endsWith(".html")) {
currentPath += ".html";
}
// Full URL with host, current path, and all change parameters
let url = `${window.location.origin}/CPL?${currentPath}`;
Object.keys(changes).forEach((paramKey) => {
url += `&${paramKey}=${encodeURIComponent(changes[paramKey])}`;
});
// Log the full URL to the console for debugging
console.log(url);
// Send the URL with changes to the server
fetch(url, { method: "GET" })
.then((response) => {
if (response.ok) {
alert("Daten erfolgreich gesendet!");
} else {
//alert("Fehler beim Senden der Daten!"); //es wird ausgeführt aber kein Antwort deswegen auskommentiert
}
})
.catch((error) => {
console.error("Fehler:", error);
// alert("Fehler beim Senden der Daten!"); //es wird ausgeführt aber kein Antwort deswegen auskommentiert
});
} else {
alert("Keine Änderungen vorgenommen.");
}
};
const handleSetDateTime = () => {
const currentDate = new Date();
// Format date and time as required by the system without leading zeros:
const year = currentDate.getFullYear().toString().slice(-2); // Last two digits of the year
const month = Number(currentDate.getMonth() + 1); // Convert to Number to remove leading zero
const day = Number(currentDate.getDate()); // Convert to Number to remove leading zero
const hours = Number(currentDate.getHours()); // Convert to Number to remove leading zero
const minutes = Number(currentDate.getMinutes()); // Convert to Number to remove leading zero
const seconds = Number(currentDate.getSeconds()); // Convert to Number to remove leading zero
// Date and Time commands
const dateCommand = `CLK00=${year}-${month}-${day}`;
const timeCommand = `CLK01=${hours}-${minutes}-${seconds}`;
// Get the current path and ensure it ends with ".html"
let currentPath = window.location.pathname;
if (!currentPath.endsWith(".html")) {
currentPath += ".html";
}
// Full URL with host, current path, date, and time commands
const url = `${window.location.origin}/CPL?${currentPath}&${dateCommand}&${timeCommand}`;
// Log the full URL to the console
console.log(url);
// Send the commands to the server using fetch and GET method
fetch(url, { method: "GET" })
.then((response) => {
if (response.ok) {
alert("Datum und Uhrzeit erfolgreich gesetzt!");
} else {
//alert("Fehler beim Setzen von Datum und Uhrzeit!"); //es wird ausgeführt aber kein Antwort deswegen auskommentiert
}
})
.catch((error) => {
console.error("Fehler:", error);
//alert("Fehler beim Setzen von Datum und Uhrzeit!"); //es wird ausgeführt aber kein Antwort deswegen auskommentiert
});
};
//---------------------------------------------------
// Setze initiale Werte nur beim Öffnen des Modals
useEffect(() => {
if (showModal) {
setName(deviceName_Redux || "");
setMac1(mac1_Redux || "");
setIp(ip_Redux || "");
setSubnet(subnet_Redux || "");
setGateway(gateway_Redux || "");
setSystemUhr(datetime_Redux || "");
setNtp1(ntp1_Redux || "");
setNtp2(ntp2_Redux || "");
setNtp3(ntp3_Redux || "");
setNtpTimezone(ntpTimezone_Redux || "");
setActive(active_Redux || "");
}
}, [showModal]); // Nur beim Öffnen des Modals erneut setzen
useEffect(() => {
setOriginalValues({
name: deviceName_Redux,
ip: ip_Redux,
subnet: subnet_Redux,
gateway: gateway_Redux,
ntp1: ntp1_Redux,
ntp2: ntp2_Redux,
ntp3: ntp3_Redux,
ntpTimezone: ntpTimezone_Redux,
active: active_Redux,
});
}, [
deviceName_Redux,
ip_Redux,
subnet_Redux,
gateway_Redux,
ntp1_Redux,
ntp2_Redux,
ntp3_Redux,
ntpTimezone_Redux,
active_Redux,
]);
//---------------------------------------------------
// Aktualisiere `systemUhr`, wenn sich `datetime_Redux` ändert
useEffect(() => {
setSystemUhr(datetime_Redux || "");
}, [datetime_Redux]);
//---------------------------------------------------
return (
<>
{/* Modal-Inhalt */}
{/* Weitere Inhalte wie Formular */}