import React from "react";
import DatePicker from "react-datepicker";
import { useSelector, useDispatch } from "react-redux";
import { RootState } from "../../../../../redux/store";
import {
  setVonDatum,
  setBisDatum,
} from "../../../../../redux/slices/kabelueberwachungChartSlice";
import "react-datepicker/dist/react-datepicker.css";

const DateRangePicker: React.FC = () => {
  const dispatch = useDispatch();

  const reduxVonDatum = useSelector(
    (state: RootState) => state.kabelueberwachungChart.vonDatum
  );
  const reduxBisDatum = useSelector(
    (state: RootState) => state.kabelueberwachungChart.bisDatum
  );

  return (
    <div className="flex space-x-4">
      <div>
        <label className="block text-sm font-semibold">Von</label>
        <DatePicker
          selected={reduxVonDatum ? new Date(reduxVonDatum) : new Date()}
          onChange={(date) => {
            if (date) {
              dispatch(setVonDatum(date.toISOString().split("T")[0]));
            }
          }}
          selectsStart
          startDate={reduxVonDatum ? new Date(reduxVonDatum) : new Date()}
          endDate={reduxBisDatum ? new Date(reduxBisDatum) : new Date()}
          dateFormat="dd.MM.yyyy"
          className="border px-2 py-1 rounded"
        />
      </div>

      <div>
        <label className="block text-sm font-semibold">Bis</label>
        <DatePicker
          selected={reduxBisDatum ? new Date(reduxBisDatum) : new Date()}
          onChange={(date) => {
            if (date) {
              dispatch(setBisDatum(date.toISOString().split("T")[0]));
            }
          }}
          selectsEnd
          startDate={reduxVonDatum ? new Date(reduxVonDatum) : new Date()}
          endDate={reduxBisDatum ? new Date(reduxBisDatum) : new Date()}
          minDate={reduxVonDatum ? new Date(reduxVonDatum) : new Date()}
          dateFormat="dd.MM.yyyy"
          className="border px-2 py-1 rounded"
        />
      </div>
    </div>
  );
};

export default DateRangePicker;
