import { useState } from 'react'; import { Form, Input, Button, App } from 'antd'; import { MobileOutlined, LockOutlined, SafetyOutlined } from '@ant-design/icons'; import { useTranslation } from 'react-i18next'; import { resetPasswordApi } from '@/api/auth'; interface ForgetPasswordFormProps { onSwitchLogin: () => void; } export function ForgetPasswordForm({ onSwitchLogin }: ForgetPasswordFormProps) { const { t } = useTranslation('login'); const { message } = App.useApp(); const [loading, setLoading] = useState(false); const [codeSent, setCodeSent] = useState(false); const [countdown, setCountdown] = useState(0); const [form] = Form.useForm(); const sendCode = async () => { try { const mobile = form.getFieldValue('mobile'); if (!mobile || !/^\d{11}$/.test(mobile)) { message.error(t('mobileInvalid')); return; } setCodeSent(true); message.success(t('sendCodeSuccess')); setCountdown(60); const timer = setInterval(() => { setCountdown((prev) => { if (prev <= 1) { clearInterval(timer); return 0; } return prev - 1; }); }, 1000); } catch { // ignore } }; const onFinish = async (values: { mobile: string; code: string; password: string }) => { setLoading(true); try { await resetPasswordApi({ mobile: values.mobile, code: values.code, password: values.password }); message.success(t('resetSuccess')); onSwitchLogin(); } catch { message.error(t('resetFail')); } finally { setLoading(false); } }; return (