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

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

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

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

  return (
    <Modal
      visible={visible}
      title={`${isEdit ? '修改' : '新增'}PDA菜单`}
      isLoading={isLoading}
      style={style}
      onSubmit={onSubmit}
      onClose={onClose}
      testId='menu_modal'
      hideBtnGroup
    >
      <FormProvider {...formInstance}>
        <ErrorBoundary>
          <Suspense fallback={<Loading tip='正在获取菜单信息' />}>
            <Info id={id} />
            <ModalBtnGroup isLoading={isLoading} onClose={onClose} />
          </Suspense>
        </ErrorBoundary>
      </FormProvider>

    </Modal>
  );
};

export default MenuModal;
