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

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

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

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

export default StorageModal;
