import { Box } from "@chakra-ui/react";
import { useNavigate, useParams } from "react-router-dom";
import GlobalStateContext from "../../Contexts/GlobalStateContext";
import { useContext, useEffect, useState } from "react";
import FormInputView from "../../Components/FormInputView";
import { useForm } from "react-hook-form"; // assuming react-hook-form is used
import { OPACITY_ON_LOAD } from "../../Layout/animations";
import { ArrowBackIcon } from "@chakra-ui/icons";

const ViewIOdata = () => {
  const navigate = useNavigate();
  const params = useParams();
  const { viewIO } = useContext(GlobalStateContext);
  const { reset } = useForm(); // assuming react-hook-form

  const id = params?.id;
  const foundObject = viewIO.find((item) =>
    console.log(item?.id.toString() == id.toString())
  );

  if (!foundObject) {
    return <Box>Loading...</Box>;
  }

  const formFields = [
    {
      label: "Deal ID",
      value: foundObject.DealID,
    },
    {
      label: "Sponsor name",
      value: foundObject.SponsorName,
    },
    {
      label: "IO status",
      value: foundObject.IOstatus,
    },
  ];

  const groupedFields = formFields.reduce((groups, field) => {
    const { section } = field;
    if (!groups[section]) {
      groups[section] = [];
    }
    groups[section].push(field);
    return groups;
  }, {});

  return (
    <Box {...OPACITY_ON_LOAD} overflowY={"scroll"} height={"100vh"} pb={14}>
      <span
        onClick={() => navigate(-1)}
        style={{ fontSize: "15px", cursor: "pointer" }}
      >
        <ArrowBackIcon cursor={"pointer"} /> Back
      </span>
      <FormInputView groupedFields={groupedFields} />
      
    </Box>
  );
};

export default ViewIOdata;
