
import { useNavigate, useParams } from "react-router-dom";
import HeaderBreadcrumbs from "../../../../components/HeaderBreadcrumbs";
import Page from "../../../../components/Page";
import useSettings from "../../../../hooks/useSettings";
import {useContext, useEffect, useMemo, useState } from 'react';
import axios from '../../../../utils/axios';
import { useSnackbar } from 'notistack';
import CorporatePlanForm from './Form';
import { CorporatePlan } from '../../../../@types/corporates';
import { Corporate } from "@/@types/corporates";
import { ConfiguredCorporateContext } from "@/contexts/ConfiguredCorporateContext";



export default function PlanCreate() {
  const { themeStretch } = useSettings();
  const { corporate_id, id } = useParams();
  const [corporate, setCorporate] = useState<Corporate|null>();
  const configuredCorporateContext = useContext(ConfiguredCorporateContext);

  useEffect(() => {
    setCorporate(configuredCorporateContext.currentCorporate);
    console.log(configuredCorporateContext.currentCorporate);
  }, [configuredCorporateContext])

  const [ currentCorporatePlan, setCurrentCorporatePlan ] = useState<CorporatePlan>();
  
  
  const navigate = useNavigate();

  const isEdit = !!id;

  useEffect(() => {
    if (isEdit) {
      axios.get('/master/formularium-template/'+id+'/edit')
        .then((res) => {
          setCurrentCorporatePlan(res.data);
        })
        .catch((err) => {
          if (err.response.status === 404) {
            navigate('/404');
          }
        })
    }
  }, [corporate_id, id]);


  return (
    <Page title= "Master Formularium">
      <HeaderBreadcrumbs
        heading={'Master Formularium'}
        links={[
          {
            name: 'Master',
            href: '/master/formularium-template',
          },
          {
            name: 'Formularium Template',
            href: '/master/formularium-template',
          },
          {
            name: !isEdit ? 'Create' : 'Edit',
            href: '/corporate/'+corporate_id+'/divisions/'+id,
          },
        ]}
      />

      <CorporatePlanForm isEdit={isEdit} currentCorporatePlan={currentCorporatePlan}/>
    </Page>
  );
}
