import { useState, useEffect } from 'react' import { useNavigate } from 'react-router-dom' import { useAuth } from '../contexts/AuthContext' import { useLanguage } from '../i18n/LanguageContext' import { Megaphone, Lock, Mail, AlertCircle, User, CheckCircle } from 'lucide-react' import { api } from '../utils/api' export default function Login() { const navigate = useNavigate() const { login } = useAuth() const { t } = useLanguage() const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [needsSetup, setNeedsSetup] = useState(null) const [setupName, setSetupName] = useState('') const [setupEmail, setSetupEmail] = useState('') const [setupPassword, setSetupPassword] = useState('') const [setupDone, setSetupDone] = useState(false) useEffect(() => { api.get('/setup/status').then(data => setNeedsSetup(data.needsSetup)).catch(() => setNeedsSetup(false)) }, []) const handleSubmit = async (e) => { e.preventDefault() setError('') setLoading(true) try { await login(email, password) navigate('/') } catch (err) { setError(err.message || 'Invalid email or password') } finally { setLoading(false) } } const handleSetup = async (e) => { e.preventDefault() setError('') setLoading(true) try { await api.post('/setup', { name: setupName, email: setupEmail, password: setupPassword }) setSetupDone(true) setNeedsSetup(false) setEmail(setupEmail) } catch (err) { setError(err.message || 'Setup failed') } finally { setLoading(false) } } if (needsSetup === null) { return (
) } return (
{/* Logo & Title */}

{needsSetup ? 'Initial Setup' : t('login.title')}

{needsSetup ? 'Create your superadmin account to get started' : t('login.subtitle')}

{/* Success Message */} {setupDone && (

Account created. You can now log in.

)} {/* Card */}
{needsSetup ? (
{/* Name */}
setSetupName(e.target.value)} className="w-full pl-11 pr-4 py-3 bg-slate-900/50 border border-slate-700 rounded-lg text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all" placeholder="Your name" required autoFocus />
{/* Email */}
setSetupEmail(e.target.value)} dir="auto" className="w-full pl-11 pr-4 py-3 bg-slate-900/50 border border-slate-700 rounded-lg text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all" placeholder="admin@company.com" required />
{/* Password */}
setSetupPassword(e.target.value)} className="w-full pl-11 pr-4 py-3 bg-slate-900/50 border border-slate-700 rounded-lg text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all" placeholder="Choose a strong password" required minLength={6} />
{/* Error */} {error && (

{error}

)} {/* Submit */}
) : (
{/* Email */}
setEmail(e.target.value)} dir="auto" className="w-full pl-11 pr-4 py-3 bg-slate-900/50 border border-slate-700 rounded-lg text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all" placeholder="user@company.com" required autoFocus />
{/* Password */}
setPassword(e.target.value)} className="w-full pl-11 pr-4 py-3 bg-slate-900/50 border border-slate-700 rounded-lg text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all" placeholder="••••••••" required />
{/* Error */} {error && (

{error}

)} {/* Submit */}
)} {/* Footer */} {!needsSetup && (

{t('login.forgotPassword')}

)}
) }