import React, { useState } from "react";
import DateRangePicker from "../DateRangePicker";
import { useDispatch } from "react-redux";
import { setChartData } from "../../../../../../redux/slices/chartDataSlice";

const LoopChartActionBar: React.FC = () => {
  const isolationswiderstand = 3;
  const schleifenwiderstand = 4;
  const dispatch = useDispatch();
  const [showChart, setShowChart] = useState(false);

  const handleFetchData = async () => {
    try {
      const apiUrl =
        process.env.NODE_ENV === "development"
          ? "/CPLmockData/mockChartData.json"
          : `/CPL?seite.ACP&DIA1=2025;01;01;2025;07;31;${slotIndex};${schleifenwiderstand}`;

      const response = await fetch(apiUrl);
      const data = await response.json();

      if (Array.isArray(data)) {
        console.log("Daten geladen:", data);
        dispatch(setChartData(data));
      } else {
        console.error("Erwartetes Array, aber erhalten:", data);
      }
    } catch (error) {
      console.error("Fehler beim Laden der Daten:", error);
    }
  };

  return (
    <div className="flex justify-end items-center p-2 bg-gray-100 rounded-lg space-x-2">
      <DateRangePicker setVonDatum={() => {}} setBisDatum={() => {}} />
      <button
        onClick={handleFetchData}
        className="px-3 py-1 bg-green-500 text-white rounded text-sm"
      >
        Aktualisieren
      </button>
      {showChart && null}
    </div>
  );
};

export default LoopChartActionBar;
