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

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

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

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

export default PutModal;
