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";

const ViewSponser = () => {
  const params = useParams();
  const { sponser } = useContext(GlobalStateContext);
  const { reset } = useForm(); // assuming react-hook-form

  const id = params?.id;
  const foundObject = sponser.find(
    (item) => item?.id.toString() === id.toString()
  );

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

  const formFields = [
    {
      label: "Sponser name (English)",
      value: foundObject.sponserName,
      type: "text",
      isRequired: true,
      section: "Add Details",
    },
    {
      label: "Sponser Name (Arabic)",
      value: foundObject.sponserName,
      type: "text",
      isRequired: true,
      arabic: true,
      section: "Add Details",
    },
    {
      label: "Description (English)",
      value: foundObject.description,
      type: "textarea",
      isRequired: true,
      section: "Add Details",
    },
    {
      label: "Description (Arabic)",
      value: foundObject.descriptionArabic,
      type: "textarea",
      isRequired: true,
      arabic: true,
      section: "Add Details",
    },
    {
      label: "Address (English)",
      value: foundObject.sponserAddress,
      type: "text",
      isRequired: true,
      section: "Add Details",
    },
    {
      label: "Address (Arabic)",
      value: foundObject.sponserAddressArabic,
      type: "text",
      isRequired: true,
      arabic: true,
      section: "Add Details",
    },
    {
      label: "Bank Name (English)",
      value: foundObject.bankName,
      type: "text",
      isRequired: true,
      section: "Add Details",
    },
    {
      label: "Bank Name (Arabic)",
      value: foundObject.bankNameArabic,
      type: "text",
      isRequired: true,
      arabic: true,
      section: "Add Details",
    },
    {
      label: "Account Name (English)",
      value: foundObject.accountNumber,
      type: "text",
      isRequired: true,
      section: "Add Details",
    },
    {
      label: "Account Name (Arabic)",
      value: foundObject.accountNumberArabic,
      type: "text",
      isRequired: true,
      arabic: true,
      section: "Add Details",
    },
    {
      label: "Mobile Number (English)",
      value: foundObject.mobileNo,
      type: "number",
      isRequired: true,
      section: "Add Details",
    },
    {
      label: "Account Number (English)",
      value: foundObject.accountNumber,
      type: "text",
      isRequired: true,
      section: "Add Details",
    },
    {
      label: "IFSC Code (English)",
      value: foundObject.swiftCode,
      type: "text",
      isRequired: true,
      section: "Add Details",
    },
  ];


  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}>
      <FormInputView groupedFields={groupedFields} />
    </Box>
  );
};

export default ViewSponser;
