"use client"; // /components/main/analogeEingaenge/AnalogeEingaengeTable.tsx
import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { RootState, AppDispatch } from "../../../redux/store";
import { fetchAnalogeEingaengeThunk } from "../../../redux/thunks/fetchAnalogeEingaengeThunk";
import { Icon } from "@iconify/react";
import settingsIcon from "@iconify/icons-mdi/settings";
import waveformIcon from "@iconify/icons-mdi/waveform";

export default function AnalogeEingaengeTable({
  setSelectedId,
  setSelectedInput,
  setIsSettingsModalOpen,
}: {
  setSelectedId: (id: number) => void;
  setSelectedInput: (input: any) => void;
  setIsSettingsModalOpen: (open: boolean) => void;
}) {
  const dispatch = useDispatch<AppDispatch>();
  const [activeId, setActiveId] = React.useState<number | null>(null);

  useEffect(() => {
    dispatch(fetchAnalogeEingaengeThunk());
  }, [dispatch]);

  const analogeEingaenge = useSelector(
    (state: RootState) => state.analogeEingaengeSlice
  );

  const handleSelect = (id: number) => {
    setSelectedId(id);
    setActiveId(id);
  };

  return (
    <div className="w-full">
      <h2 className="text-sm laptop:text-xs font-bold mb-3 flex items-center">
        <Icon
          icon={waveformIcon}
          className="text-littwin-blue mr-2 text-xl laptop:text-lg xl:text-xl 2xl:text-2xl border border-littwin-blue"
        />
        Messwerteingänge
      </h2>

      <div className="bg-white shadow-lg rounded-lg p-4 border border-gray-200">
        <div className="overflow-x-auto">
          <table className="w-full border-collapse border border-gray-300 text-sm md:text-base">
            <thead>
              <tr className="bg-gray-100 text-gray-700">
                <th className="border p-1 text-left">Eingang</th>
                <th className="border p-3 text-left">Messwert</th>
                <th className="border p-3 text-left">Einheit</th>
                <th className="border p-3 text-left">Bezeichnung</th>
                <th className="border p-3 text-left">Aktion</th>
              </tr>
            </thead>
            <tbody>
              {Object.values(analogeEingaenge)
                .filter((e) => e?.id !== null && e?.id !== undefined)
                .map((e, index) => (
                  <tr
                    key={index}
                    className={`transition cursor-pointer ${
                      e.id === activeId ? "bg-blue-100" : "hover:bg-gray-100"
                    }`}
                  >
                    <td
                      className="border p-3"
                      onClick={() => handleSelect(e.id!)}
                    >
                      {e.id ?? "-"}
                    </td>
                    <td
                      className="border p-3 text-right"
                      onClick={() => handleSelect(e.id!)}
                    >
                      {typeof e.value === "number" ? e.value.toFixed(2) : "-"}
                    </td>

                    <td className="border p-3">{e.unit || "-"}</td>
                    <td
                      className="border p-3"
                      onClick={() => handleSelect(e.id!)}
                    >
                      {e.name || "----"}
                    </td>

                    <td className="border p-3 text-center">
                      <button
                        onClick={() => {
                          handleSelect(e.id!);
                          setSelectedInput(e);
                          setIsSettingsModalOpen(true);
                        }}
                        className="text-gray-400 hover:text-gray-500"
                      >
                        <Icon icon={settingsIcon} className="text-xl" />
                      </button>
                    </td>
                  </tr>
                ))}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  );
}
