import { useState } from "react";
import { useApp } from "../context/AppContext.jsx";
import { RED } from "../constants/index.js";
function initials(naam = "") {
const parts = naam.trim().split(/\s+/);
if (parts.length >= 2) return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase();
return naam.slice(0, 2).toUpperCase();
}
const LOCK_ICON = (
);
const LOGOUT_ICON = (
);
const CHEVRON = (
);
const BACK_ICON = (
);
function InputField({ label, type = "text", value, onChange, T }) {
return (
);
}
export default function ProfielPopup({ onClose }) {
const { currentUser, updateProfile, changePassword, logout, T, avatar, setAvatar } = useApp();
const [view, setView] = useState("main");
const [naam, setNaam] = useState(currentUser?.naam || "");
const [email, setEmail] = useState(currentUser?.email || "");
const [profielErr, setProfielErr] = useState("");
const [profielOk, setProfielOk] = useState(false);
const [profielBusy, setProfielBusy] = useState(false);
const [huidig, setHuidig] = useState("");
const [nieuw, setNieuw] = useState("");
const [bevestig, setBevestig] = useState("");
const [pwErr, setPwErr] = useState("");
const [pwOk, setPwOk] = useState(false);
const [pwBusy, setPwBusy] = useState(false);
const saveProfiel = async () => {
setProfielErr(""); setProfielOk(false);
if (!naam.trim() || !email.trim()) { setProfielErr("Naam en e-mail zijn verplicht."); return; }
setProfielBusy(true);
try { await updateProfile(naam.trim(), email.trim()); setProfielOk(true); }
catch (err) { setProfielErr(err.message || "Opslaan mislukt."); }
finally { setProfielBusy(false); }
};
const saveWachtwoord = async () => {
setPwErr(""); setPwOk(false);
if (!huidig || !nieuw || !bevestig) { setPwErr("Vul alle velden in."); return; }
if (nieuw !== bevestig) { setPwErr("Nieuwe wachtwoorden komen niet overeen."); return; }
if (nieuw.length < 8) { setPwErr("Minimaal 8 tekens."); return; }
setPwBusy(true);
try { await changePassword(huidig, nieuw); setPwOk(true); setHuidig(""); setNieuw(""); setBevestig(""); }
catch (err) { setPwErr(err.message || "Wijzigen mislukt."); }
finally { setPwBusy(false); }
};
const popupStyle = {
position: "fixed", top: 64, right: 16, zIndex: 199,
background: T.popupBg,
border: `1px solid ${T.border}`,
borderRadius: 16, width: 340,
boxShadow: "0 16px 48px rgba(0,0,0,0.45)",
overflow: "hidden",
};
const headerStyle = {
display: "flex", alignItems: "center", justifyContent: "center",
padding: "16px 16px 14px", position: "relative",
borderBottom: `1px solid ${T.border}`,
};
const closeBtn = (
);
const backBtn = (label) => (
);
const actionRow = (icon, label, onClick, color) => (
{icon}
{label}
{!color && {CHEVRON}}
);
return (
<>
{/* ── Hoofdscherm ── */}
{view === "main" && (
<>
Mijn account
{closeBtn}
{/* Avatar + naam + email */}
{avatar
?

: initials(currentUser?.naam)
}
{currentUser?.naam}
{currentUser?.email}
{actionRow(LOCK_ICON, "Wachtwoord wijzigen", () => setView("wachtwoord"))}
>
)}
{/* ── Profiel bewerken ── */}
{view === "profiel" && (
<>
{backBtn()}
Profiel bewerken
{closeBtn}
{/* Avatar — klikbaar om foto te uploaden */}
{avatar && (
)}
{ setNaam(e.target.value); setProfielOk(false); }} T={T} />
{ setEmail(e.target.value); setProfielOk(false); }} T={T} />
{profielErr && {profielErr}
}
{profielOk && Profiel opgeslagen.
}
>
)}
{/* ── Wachtwoord wijzigen ── */}
{view === "wachtwoord" && (
<>
{backBtn()}
Wachtwoord wijzigen
{closeBtn}
{ setHuidig(e.target.value); setPwOk(false); }} T={T} />
{ setNieuw(e.target.value); setPwOk(false); }} T={T} />
{ setBevestig(e.target.value); setPwOk(false); }} T={T} />
{pwErr && {pwErr}
}
{pwOk && Wachtwoord gewijzigd.
}
>
)}
>
);
}