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

const style: ReactModalStyle = {
  content: {
    width: '650px',
    height: '600px',
  },
};

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

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

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

export default UserModal;
