import { useState, useEffect } from 'react' import { useNavigate, Link } from 'react-router-dom' import { useAuth } from '../contexts/AuthContext' import { useLanguage } from '../i18n/LanguageContext' import { Lock, Mail, AlertCircle, User, CheckCircle } from 'lucide-react' import { api } from '../utils/api' function MarkaLogo({ className = '' }) { return ( ) } 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 [setupConfirm, setSetupConfirm] = 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 || t('login.invalidCredentials')) } finally { setLoading(false) } } const handleSetup = async (e) => { e.preventDefault() setError('') if (setupPassword !== setupConfirm) { setError(t('login.passwordMismatch')) return } setLoading(true) try { await api.post('/setup', { name: setupName, email: setupEmail, password: setupPassword }) setSetupDone(true) setNeedsSetup(false) setEmail(setupEmail) } catch (err) { setError(err.message || t('login.setupFailed')) } finally { setLoading(false) } } if (needsSetup === null) { return (
) } return (
{/* Logo & Title */}

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

{needsSetup ? t('login.initialSetupDesc') : t('login.subtitle')}

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

{t('login.accountCreated')}

)} {/* Card */}
{needsSetup ? (
{/* Name */}
setSetupName(e.target.value)} className="w-full ps-11 pe-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={t('login.fullNamePlaceholder')} required autoFocus aria-describedby={error ? 'setup-error' : undefined} />
{/* Email */}
setSetupEmail(e.target.value)} dir="auto" className="w-full ps-11 pe-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 ps-11 pe-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={t('login.passwordPlaceholder')} required minLength={6} />
{/* Confirm Password */}
setSetupConfirm(e.target.value)} className="w-full ps-11 pe-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={t('login.confirmPasswordPlaceholder')} required minLength={6} />
{/* Error */} {error && ( )} {/* Submit */}
) : (
{/* Email */}
setEmail(e.target.value)} dir="auto" className="w-full ps-11 pe-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 aria-describedby={error ? 'login-error' : undefined} />
{/* Password */}
setPassword(e.target.value)} className="w-full ps-11 pe-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 aria-describedby={error ? 'login-error' : undefined} />
{/* Error */} {error && ( )} {/* Submit */}
)} {/* Footer */} {!needsSetup && (

{t('login.forgotPassword')}

)}
) }