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

const style: ReactModal.Styles = {
  content: {
    width: '540px',
    height: '680px',
  },
};

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

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

  return (
    <Modal
      hideBtnGroup
      visible={visible}
      title={`${id.length ? '修改' : '新增'}货品`}
      style={style}
      onClose={onClose}
      onSubmit={onSubmit}
      testId='goods_modal'
    >
      <ErrorBoundary>
        <Suspense fallback={<Loading tip='正在获取货品信息' />}>
          <FormProvider {...formInstance}>
            <GoodsModalInfo id={id} onClose={onClose} isLoading={isLoading} />
          </FormProvider>
        </Suspense>
      </ErrorBoundary>
    </Modal>
  );
};

export default GoodsModal;
