import { auth } from '@/services';
import { aesEncrypt } from '@/utils/aes';
import { formLayout } from '@/utils/formLayout';
import { ModalForm, ProFormText } from '@ant-design/pro-components';
import type { ModalProps } from 'antd';
import React from 'react';

interface ISafeModalProps {
	open?: ModalProps['open'];
	onVisibleChange?: (visible: boolean) => void;
}

const SafeModal: React.FC<ISafeModalProps> = (props) => {
	return (
		<ModalForm<AuthAPI.OpenSafeCmd>
			{...formLayout()}
			title={'二级验证'}
			{...props}
			onFinish={async (values) => {
				const { success } = await auth.userAuth.openSafe({
					...values,
					password: aesEncrypt(values.password),
				});
				if (success) {
					return true;
				}
			}}
		>
			<ProFormText
				name={'password'}
				label={'管理员密码'}
				placeholder="请输入密码"
				required={true}
				rules={[
					{
						required: true,
						message: '请输入密码',
					},
				]}
				fieldProps={{
					type: 'password',
				}}
			/>
		</ModalForm>
	);
};

export default SafeModal;
