/* admin.jsx — Painel de administração de usuários (apenas role=admin). */ function AdminPanel({ onClose, pushToast, currentUserId }) { const { Ic } = window; const [list, setList] = React.useState([]); const [loading, setLoading] = React.useState(true); const [err, setErr] = React.useState(""); const [showAdd, setShowAdd] = React.useState(false); // form de novo usuário const [form, setForm] = React.useState({ name: "", email: "", password: "", role: "colaborador", role_title: "Membro" }); const [creating, setCreating] = React.useState(false); const load = async () => { setLoading(true); setErr(""); try { const r = await window.Api.users.list(); setList(r.users || []); } catch (e) { setErr(e.message); } finally { setLoading(false); } }; React.useEffect(() => { load(); }, []); const create = async (e) => { e.preventDefault(); if (!form.name.trim() || !form.email.trim() || form.password.length < 12) { pushToast("⚠️", "Preencha nome, e-mail e senha (mín. 12)"); return; } setCreating(true); try { await window.Api.users.create(form); pushToast("✅", "Usuário criado"); setForm({ name: "", email: "", password: "", role: "colaborador", role_title: "Membro" }); setShowAdd(false); load(); } catch (e) { pushToast("⚠️", e.message); } finally { setCreating(false); } }; const setRole = async (u, role) => { try { await window.Api.users.update(u.id, { role }); load(); } catch (e) { pushToast("⚠️", e.message); } }; const toggleActive = async (u) => { try { await window.Api.users.update(u.id, { is_active: !u.is_active }); load(); } catch (e) { pushToast("⚠️", e.message); } }; const remove = async (u) => { if (!confirm(`Remover ${u.name} (${u.email})? Essa ação é irreversível.`)) return; try { await window.Api.users.remove(u.id); pushToast("🗑️", "Usuário removido"); load(); } catch (e) { pushToast("⚠️", e.message); } }; const resetPw = async (u) => { const np = prompt(`Nova senha para ${u.name} (mín. 12 caracteres):`); if (np === null) return; if (np.length < 12) { pushToast("⚠️", "A senha precisa de no mínimo 12 caracteres"); return; } try { await window.Api.users.update(u.id, { password: np }); pushToast("🔒", "Senha redefinida"); } catch (e) { pushToast("⚠️", e.message); } }; const roleMeta = { admin: { cls: "role-admin" }, gerente: { cls: "role-gerente" }, colaborador: { cls: "role-colab" }, }; const initials = (n) => (n || "U").trim().split(/\s+/).map(w => w[0]).slice(0, 2).join("").toUpperCase(); const total = list.length; const admins = list.filter(u => u.role === "admin").length; const ativos = list.filter(u => u.is_active).length; return (
{ if (e.target === e.currentTarget) onClose(); }}>

Administração

Gerencie usuários e permissões do painel.

{total} {total === 1 ? "usuário" : "usuários"} {ativos} {ativos === 1 ? "ativo" : "ativos"} {admins} admin{admins === 1 ? "" : "s"}
{showAdd && (
setForm({ ...form, name: e.target.value })} required />
setForm({ ...form, email: e.target.value })} required />
setForm({ ...form, password: e.target.value })} placeholder="mín. 12 caracteres" required />
setForm({ ...form, role_title: e.target.value })} />
)} {err &&

{err}

}
{loading &&
Carregando…
} {!loading && list.length === 0 &&
Nenhum usuário cadastrado
} {!loading && list.map(u => { const isSelf = u.id === currentUserId; const rm = roleMeta[u.role] || roleMeta.colaborador; return (
{u.avatar ? : {initials(u.name)}}
{u.name} {isSelf && você}
{u.role_title}
{u.email}
{!isSelf && ( )}
Último acesso: {u.last_login || "nunca"}
); })}
); } window.AdminPanel = AdminPanel;