// /components/main/settingsPageComponents/handlers/handleNtpSubmit.ts

const handleNtpSubmit = (
  original: {
    ntp1: string;
    ntp2: string;
    ntp3: string;
    ntpTimezone: string;
    active: boolean;
  },
  current: {
    ntp1: string;
    ntp2: string;
    ntp3: string;
    ntpTimezone: string;
    active: boolean;
  }
) => {
  const changes: { [key: string]: string | boolean } = {};

  if (current.ntp1 !== original.ntp1) {
    changes.SNIP1 = current.ntp1;
  }
  if (current.ntp2 !== original.ntp2) {
    changes.SNIP2 = current.ntp2;
  }
  if (current.ntp3 !== original.ntp3) {
    changes.SNIP3 = current.ntp3;
  }
  if (current.ntpTimezone !== original.ntpTimezone) {
    changes.SNTZ = current.ntpTimezone;
  }
  if (current.active !== original.active) {
    changes.SNAC = current.active ? "1" : "0";
  }

  if (Object.keys(changes).length === 0) {
    alert("Keine Änderungen vorgenommen.");
    return;
  }

  let url = `${window.location.origin}/CPL?${window.location.pathname}`;
  Object.entries(changes).forEach(([key, value]) => {
    url += `&${key}=${encodeURIComponent(value)}`;
  });

  console.log(url);

  fetch(url, { method: "GET" })
    .then(() => {
      alert("NTP-Daten erfolgreich gesendet!");
    })
    .catch((err) => {
      console.error("Fehler:", err);
      alert("Fehler beim Senden der NTP-Daten.");
    });
};

export default handleNtpSubmit;
