"use client";
import { useState, useEffect } from "react";
import ReactModal from "react-modal";
import KueEinstellung from "./KueEinstellung";
import TdrEinstellung from "./TdrEinstellung";
import Knotenpunkte from "./Knotenpunkte";

interface KueModalProps {
  showModal: boolean;
  onClose: () => void;
  slot: number;
  onModulNameChange: (id: string) => void;
}

export default function KueModal({ showModal, onClose, slot }: KueModalProps) {
  const [activeTab, setActiveTab] = useState<"kue" | "tdr" | "knoten">(() => {
    if (typeof window !== "undefined" && (window as any).__lastKueTab) {
      return (window as any).__lastKueTab;
    }
    return "kue";
  });

  useEffect(() => {
    if (typeof window !== "undefined") {
      (window as any).__lastKueTab = activeTab;
    }
  }, [activeTab]);

  useEffect(() => {
    if (typeof window !== "undefined") {
      window.kabelModalOpen = showModal;
    }
  }, [showModal]);

  return (
    <ReactModal
      isOpen={showModal}
      onRequestClose={onClose}
      ariaHideApp={false}
      style={{
        overlay: {
          backgroundColor: "rgba(0, 0, 0, 0.5)",
          zIndex: 100,
        },
        content: {
          top: "50%",
          left: "50%",
          transform: "translate(-50%, -50%)",
          width: "90%",
          maxWidth: "850px",
          padding: "0px",
          border: "none",
          borderRadius: "8px",
          position: "relative",
          bottom: "auto",
          right: "auto",
        },
      }}
    >
      <div className="p-2 flex justify-between items-center rounded-t-md">
        <h2 className="text-base font-bold">
          Einstellungen Steckplatz {slot + 1}
        </h2>
        <button onClick={onClose} className="text-2xl hover:text-gray-200">
          <i className="bi bi-x-circle-fill"></i>
        </button>
      </div>

      <div className="flex justify-start bg-gray-100 space-x-2 p-2">
        {[
          { label: "Allgemein", key: "kue" },
          { label: "TDR ", key: "tdr" },
          { label: "Knotenpunkte", key: "knoten" },
        ].map(({ label, key }) => (
          <button
            key={key}
            onClick={() => setActiveTab(key as any)}
            className={`px-4 py-1 rounded-t font-bold text-sm ${
              activeTab === key
                ? "bg-white text-littwin-blue"
                : "text-gray-500 hover:text-black"
            }`}
          >
            {label}
          </button>
        ))}
      </div>

      <div className="p-4 bg-white rounded-b-md h-[20rem] laptop:h-[24rem] 2xl:h-[30rem] overflow-y-auto">
        {activeTab === "kue" && (
          <KueEinstellung
            slot={slot}
            showModal={showModal}
            onModulNameChange={(id) => console.log("Modulname geändert:", id)}
            onClose={onClose}
          />
        )}
        {activeTab === "tdr" && (
          <TdrEinstellung slot={slot} onClose={onClose} />
        )}
        {activeTab === "knoten" && (
          <Knotenpunkte slot={slot} onClose={onClose} />
        )}
      </div>
    </ReactModal>
  );
}
