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 CorporateHospitalForm from './Form';
import { Hospital } from '../../../@types/corporates';
import { Corporate } from "@/@types/corporates";
import { ConfiguredCorporateContext } from "@/contexts/ConfiguredCorporateContext";
import { Stack, Typography } from '@mui/material';
import ArrowBackIosIcon from '@mui/icons-material/ArrowBackIos';



export default function HospitalCreate() {
  const { corporate_id, id } = useParams();
  const navigate = useNavigate();

  const [corporate, setCorporate] = useState<Corporate|null>();

  const configuredCorporateContext = useContext(ConfiguredCorporateContext);

  const isEdit = !!id;

  useEffect(() => {
    setCorporate(configuredCorporateContext.currentCorporate);
  }, [corporate_id, id, configuredCorporateContext]);


  return (
    <Page title="Create Hospital">
      {isEdit ? (
        <Stack direction="row" alignItems="center" sx={{ marginBottom: 3 }}>
          <ArrowBackIosIcon sx={{cursor:'pointer'}} onClick={() => navigate(-1)}/>
          <Typography variant="h5" sx={{marginLeft:2}}>Edit Hospital</Typography>
        </Stack>
      ) : (
        <HeaderBreadcrumbs
          heading={'Create Hospital'}
          links={[
            {
              name: 'Corporates',
              href: '/corporates',
            },
            {
              name: corporate?.name ?? '-',
              href: '/corporates/'+corporate_id,
            },
            {
              name: 'Hospital',
              href: '/corporates/'+corporate_id+'/hospitals',
            },
            {
              name: !isEdit ? 'Create' : 'Edit',
              href: '/corporates/'+corporate_id+'/hospitals/create',
            },
          ]}
        />
      )}
      <CorporateHospitalForm isEdit={isEdit} />
    </Page>
  );
}
