import { useState, useEffect, useCallback } from 'react'; import { useAuth } from '../context/AuthContext'; import { useNavigate } from 'react-router-dom'; export default function AdminDashboard() { const { authFetch } = useAuth(); const [stats, setStats] = useState({ total_users: 0, pending_personal: 0, pending_business: 0, admins: 0 }); const [users, setUsers] = useState([]); const [search, setSearch] = useState(""); const [tab, setTab] = useState("users"); const [loading, setLoading] = useState(true); const navigate = useNavigate(); // --- Template Management State --- const [tplCategory, setTplCategory] = useState("personal"); const [tplYear, setTplYear] = useState(new Date().getFullYear().toString()); const [templates, setTemplates] = useState([]); const [tplName, setTplName] = useState(""); const [tplFile, setTplFile] = useState(null); const [tplLoading, setTplLoading] = useState(false); const [digestHtml, setDigestHtml] = useState(null); const [digestLoading, setDigestLoading] = useState(false); const fetchDigest = useCallback(async () => { setDigestLoading(true); try { const res = await authFetch('/api/chatbot/daily-digest'); if (res.ok) { const data = await res.json(); setDigestHtml(data.digest); } } catch (err) { console.error(err); } finally { setDigestLoading(false); } }, [authFetch]); const fetchAdminData = useCallback(async () => { setLoading(true); try { const res = await authFetch("/api/auth/admin/users"); if (res.ok) { const payload = await res.json(); const usersArray = Array.isArray(payload) ? payload : (payload.users || []); setUsers(usersArray); // Compute stats locally const total = usersArray.length; const pendingP = usersArray.reduce((s, u) => s + (u.pending_personal || 0), 0); const pendingB = usersArray.reduce((s, u) => s + (u.pending_business || 0), 0); const admins = usersArray.filter(u => u.role === "admin" || u.role === "super_admin").length; setStats({ total_users: total, pending_personal: pendingP, pending_business: pendingB, admins: admins }); } } catch (err) { console.error(err); } finally { setLoading(false); } }, [authFetch]); const loadTemplates = useCallback(async () => { setTplLoading(true); try { const res = await authFetch(`/api/upload/templates?category=${tplCategory}&tax_year=${tplYear}`); if (res.ok) { setTemplates(await res.json()); } else { setTemplates([]); } } catch (err) { console.error("Failed to load templates", err); setTemplates([]); } finally { setTplLoading(false); } }, [authFetch, tplCategory, tplYear]); useEffect(() => { fetchAdminData(); }, [authFetch, fetchAdminData]); useEffect(() => { if (tab === "templates") { loadTemplates(); } }, [tab, tplCategory, tplYear, loadTemplates]); const handleTplUpload = async (e) => { e.preventDefault(); if (!tplFile || !tplName) return; const fd = new FormData(); fd.append("category", tplCategory); fd.append("tax_year", tplYear); fd.append("name", tplName); fd.append("file", tplFile); try { const res = await authFetch("/api/upload/admin/templates", { method: "POST", body: fd }); if (res.ok) { setTplName(""); setTplFile(null); document.getElementById("tplFile").value = ""; loadTemplates(); } else { const err = await res.json(); alert(err.detail || "Upload failed"); } } catch (err) { console.error("Upload error", err); } }; const deleteTemplate = async (id) => { if (!window.confirm("Are you sure you want to delete this template?")) return; try { const res = await authFetch(`/api/upload/admin/templates/${id}`, { method: "DELETE" }); if (res.ok) { loadTemplates(); } else { alert("Failed to delete template"); } } catch (err) { console.error("Delete error", err); } }; const currentYear = new Date().getFullYear(); const yearOptions = [currentYear - 1, currentYear, currentYear + 1]; const filteredUsers = users.filter(u => { if (!search) return true; const q = search.toLowerCase(); return (u.name?.toLowerCase().includes(q) || u.email?.toLowerCase().includes(q)); }); return (
Upload new templates for users to download.
No templates found for {tplCategory} {tplYear}.
) : ( templates.map(t => (