const LoginPage = ({ onLogin, setPage, redirect, lang }) => { const t = (en, id) => lang === "id" ? id : en; const [tab, setTab] = React.useState("login"); const [form, setForm] = React.useState({ name:"", email:"", password:"", confirm:"" }); const [error, setError] = React.useState(""); const [loading, setLoading] = React.useState(false); const set = (k, v) => { setForm(f => ({ ...f, [k]: v })); setError(""); }; const handleLogin = async () => { if (!form.email || !form.password) { setError(t("Fill in all fields","Isi semua field")); return; } setLoading(true); try { const res = await fetch("api/login.php", { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: form.email, password: form.password }) }); const data = await res.json(); if (!res.ok) { setError(data.error || t("Login failed","Login gagal")); return; } if (data.role === "admin") { window.location.href = "admin.html"; return; } onLogin(data.user); setPage(redirect || "home"); } catch { setError(t("Connection error. Try again.","Gagal terhubung. Coba lagi.")); } finally { setLoading(false); } }; const handleRegister = async () => { if (!form.name || !form.email || !form.password) { setError(t("Fill in all fields","Isi semua field")); return; } if (form.password.length < 6) { setError(t("Password min. 6 characters","Password minimal 6 karakter")); return; } if (form.password !== form.confirm) { setError(t("Passwords don't match","Password tidak cocok")); return; } setLoading(true); try { const res = await fetch("api/register.php", { method: "POST", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: form.name, email: form.email, password: form.password }) }); const data = await res.json(); if (!res.ok) { setError(data.error || t("Registration failed","Gagal mendaftar")); return; } onLogin(data.user); setPage(redirect || "home"); } catch { setError(t("Connection error. Try again.","Gagal terhubung. Coba lagi.")); } finally { setLoading(false); } }; const inputStyle = { width:"100%", padding:"12px 14px", border:"1px solid #e0deda", background:"#fafaf8", fontFamily:"'DM Sans',sans-serif", fontSize:14, color:"#111", outline:"none", borderRadius:2 }; const tabBtn = (active) => ({ flex:1, padding:"12px", background:"none", border:"none", borderBottom: active ? "2px solid #111" : "2px solid transparent", fontFamily:"'DM Sans',sans-serif", fontSize:12, letterSpacing:"0.1em", textTransform:"uppercase", fontWeight: active ? 600 : 400, color: active ? "#111" : "#aaa", cursor:"pointer" }); return (
LOPOBYA
{redirect === "checkout" ? t("Login to continue checkout","Login untuk melanjutkan checkout") : t("My Account","Akun Saya")}
{error}