import { useEffect, useState } from "react";
import FormInfoBox from "./FormInfoBox";
import { useGetCompanyProfileDetails } from "../../../../utils/hooks";
import UseFullScreenLoader from "../../../../hooks/UseFullScreenLoader";

const CompanyProfile = () => {
  const [companyProfile, setCompanyProfile] = useState<CompanyProfileDetails>();
  const [getIsLoading, setIsLoading] = useState<boolean>();

  const FetchCompanyDetails = async () => {
    try {
      setIsLoading(true);
      const { resp, status } =
        (await useGetCompanyProfileDetails()) as CompanyProfileDetailsResp;
      setCompanyProfile(resp);
      setIsLoading(false);
    } catch (error) {
      console.error(
        "Error Occurred when fetching the company account details",
        error
      );
    }
  };

  useEffect(() => {
    if (!sessionStorage.getItem("isReturnedAfterSubscription")) {
      console.log("Fetching Company Details");
      FetchCompanyDetails();
    } else sessionStorage.removeItem("isReturnedAfterSubscription");
  }, []);

  return (
    <>
      {getIsLoading && (
        <UseFullScreenLoader text={"Loading Profile Details..."} />
      )}
      <div className="widget-content">
        <FormInfoBox companyProfile={companyProfile!} />
        {/* compnay info box */}
      </div>
    </>
  );
};

export default CompanyProfile;
