import React, { useState } from 'react'; import { Box, Button, TextField, Typography, useTheme, Snackbar, Alert } from '@mui/material'; import authService from '../../../services/authService'; const EmailInputSection = ({ emailOrPhone, setEmailOrPhone, onStepComplete }) => { const theme = useTheme(); const [loading, setLoading] = useState(false); // حالات Snackbar const [snackbarOpen, setSnackbarOpen] = useState(false); const [snackbarMessage, setSnackbarMessage] = useState(''); const [snackbarSeverity, setSnackbarSeverity] = useState('success'); const [inputError, setInputError] = useState(''); const handleCloseSnackbar = (event, reason) => { if (reason === 'clickaway') return; setSnackbarOpen(false); }; const validateEmail = (email) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; const handleSend = async () => { setInputError(''); // مسح الأخطاء السابقة if (!emailOrPhone) { setInputError("Please enter your email."); return; } if (!validateEmail(emailOrPhone)) { setInputError("Please enter a valid email address."); return; } setLoading(true); const response = await authService.resetPassword(emailOrPhone); setLoading(false); if (response.success !== false) { setSnackbarMessage("A reset code has been sent to your email."); setSnackbarSeverity('success'); setSnackbarOpen(true); setTimeout(() => { if (onStepComplete) onStepComplete(); }, 5000); } else { setInputError(''); let errorMessage = ""; if (response.errors) { const firstError = Object.values(response.errors)[0]; errorMessage = Array.isArray(firstError) ? firstError[0] : firstError; } else if (response.message) { errorMessage = response.message; } else { errorMessage = "Failed to send reset code."; } if ( errorMessage.toLowerCase().includes("not found") || errorMessage.toLowerCase().includes("not registered") || errorMessage.toLowerCase().includes("doesn't exist") || errorMessage.toLowerCase().includes("email") ) { setInputError("This email is not registered."); } else if (errorMessage.toLowerCase().includes("invalid credentials")) { setInputError("Email or password is incorrect."); } else { setSnackbarMessage(errorMessage); setSnackbarSeverity('error'); setSnackbarOpen(true); } } }; return ( Forget Password Enter your email to reset it and regain access to your account. Email setEmailOrPhone(e.target.value)} error={!!inputError} helperText={inputError} sx={{ '& input': { fontWeight: 500, fontSize: '15px' }, '& input::placeholder': { color: '#969BA7' }, '& .MuiOutlinedInput-root': { borderRadius: '10px', transition: '0.3s', '&.Mui-focused fieldset': { borderColor: '#FF914D', boxShadow: '0 0 0 2px rgba(255, 145, 77, 0.2)', }, }, '& .MuiOutlinedInput-root.Mui-focused': { borderColor: '#3f51b5', boxShadow: '0 0 0 2px rgba(63,81,181,0.1)', }, }} /> {/* By log in, I agree to the  Terms of Service {' '} and  Privacy Policy */} {/* Terms */} By logging in, I agree to the{' '} window.open('/terms', 'TermsOfService')} > Terms of Service {' '} and{' '} window.open('/privacy', 'PrivacyPolicy')} > Privacy Policy . {snackbarMessage} ); }; export default EmailInputSection;