import {Loading, Modal, ErrorBoundary} from '@components';
import {FC, Suspense} from 'react';
import {useFormState} from './hooks';
import {FormProvider} from 'react-hook-form';
import Info from './Info';

type Props = {
  onFetch: () => void;
  onClose: () => void;
  visible: boolean;
  id: string;
};

const EditModal: FC<Props> = function ({id, visible, onClose, onFetch}) {
  const [{formInstance, isLoading}, {onSubmit}] = useFormState({
    id,
    visible,
    onClose,
    onFetch,
  });

  return (
    <Modal
      visible={visible}
      title='修改物料信息'
      onSubmit={onSubmit}
      onClose={onClose}
      testId='goods_modal'
      isLoading={isLoading}
    >
      <FormProvider {...formInstance}>
        <ErrorBoundary>
          <Suspense fallback={<Loading tip='正在获取' />}>
            <Info id={id} />
          </Suspense>
        </ErrorBoundary>
      </FormProvider>
    </Modal>
  );
};

export default EditModal;
