import { UiSchema } from "@rjsf/utils";
import { useContext } from "react";
import { useForm, useTranslation } from "@refinedev/core";
import { CrudForm } from "../../../lib/crud/components/crud-form";
import { FirmContext } from "../../../contexts/FirmContext";
import SaveIcon from "@mui/icons-material/Save";
import { Button } from "@mui/material";

type NewProps = {
  resource: string,
  schemaName: string,
  uiSchema?: UiSchema,
  defaultValue?: any
}

const New = <T,>(props: NewProps) => {
  const { schemaName, resource, uiSchema, defaultValue } = props;
  const { currentFirm } = useContext(FirmContext);
  const { translate: t } = useTranslation();
  const resourceBasePath = `firm/${currentFirm.instance}/${currentFirm.firm}`

  const { onFinish } = useForm({
    resource: `${resourceBasePath}/${resource}`,
    action: "create",
    redirect: "show",
  });

  return (
    <CrudForm
      schemaName={schemaName}
      uiSchema={uiSchema}
      resourceBasePath={resourceBasePath}
      defaultValue={defaultValue}
      onSubmit={(data: any) => onFinish(data)}
    >
      <Button type='submit' variant="contained" size="large"><SaveIcon />{t("buttons.create")}</Button>
    </CrudForm>
  )
}

export default New;
