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

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

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

  return (
    <Modal
      visible={visible}
      title={`${id ? '修改' : '新增'}物料绑定信息`}
      onSubmit={onSubmit}
      onClose={onClose}
      testId='material_bind_modal'
      isLoading={isLoading}
    >
      <FormProvider {...formInstance}>
        <ErrorBoundary>
          <Suspense fallback={<Loading tip='正在获取' />}>
            <Info id={id} />
          </Suspense>
        </ErrorBoundary>
      </FormProvider>
    </Modal>
  );
};

export default PutModal;
