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 { useGetSponserByIdQuery } from "../../../Services/io.service";

const ViewSponser = () => {
  const params = useParams();
  const { sponser } = useContext(GlobalStateContext);
  const { reset } = useForm(); // assuming react-hook-form
  const { data, error, isLoading } = useGetSponserByIdQuery(params?.id);

  console.log(data?.data);


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

  // ========================== [Input Setup] ===========================

  const formFields = [
    {
      label: "Sponser name",
      value:data?.data?.sponsorName,
      section: "",
    },
    {
      label: "Sponser name (Arabic)",
      value:data?.data?.sponsorNameArabic,
      section: "",
    },
    {
      label: "Mobile Number",
      value:data?.data?.mobileNo,
      section: "",
    },
  ];

  // ====================== [Group Fields] =========================

  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}>

      {/* ======================= [Input Form View] =================== */}

      <FormInputView groupedFields={groupedFields} />

    </Box>
  );
};

export default ViewSponser;
