//components/modales/handlers/handleSetDateTime.js
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!");
      }
    })
    .catch((error) => {
      console.error("Fehler:", error);
      alert("Fehler beim Setzen von Datum und Uhrzeit!");
    });
};

export default handleSetDateTime;
