"use client"; import { useState, useEffect } from "react"; import { Search, ChevronDown, ChevronUp, ExternalLink, FileText, User, Users, Calendar, Settings, ListChecks, Save, ShieldCheck, ShieldOff } from "lucide-react"; export default function AdminDashboard() { const [reports, setReports] = useState([]); const [loading, setLoading] = useState(true); const [search, setSearch] = useState(""); const [expandedId, setExpandedId] = useState(null); const [tab, setTab] = useState<"REPORTS" | "USERS" | "SETTINGS">("REPORTS"); const [folderId, setFolderId] = useState(""); const [saving, setSaving] = useState(false); const [users, setUsers] = useState([]); const [usersLoading, setUsersLoading] = useState(false); const [togglingId, setTogglingId] = useState(null); useEffect(() => { fetchReports(); fetchSettings(); }, []); const fetchSettings = async () => { try { const res = await fetch("/api/admin/settings"); const data = await res.json(); if (data.GOOGLE_DRIVE_FOLDER_ID) { setFolderId(data.GOOGLE_DRIVE_FOLDER_ID); } } catch (error) { console.error("Failed to fetch settings"); } }; const saveSetting = async (key: string, value: string) => { setSaving(true); try { await fetch("/api/admin/settings", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ key, value }), }); alert("Setting saved successfully!"); } catch (error) { alert("Failed to save setting"); } finally { setSaving(false); } }; const fetchUsers = async () => { setUsersLoading(true); try { const res = await fetch("/api/admin/users"); const data = await res.json(); setUsers(data); } catch (error) { console.error("Failed to fetch users"); } finally { setUsersLoading(false); } }; const toggleRole = async (userId: string, currentRole: string) => { setTogglingId(userId); const newRole = currentRole === "ADMIN" ? "EMPLOYEE" : "ADMIN"; try { const res = await fetch("/api/admin/users", { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ userId, role: newRole }), }); const data = await res.json(); if (data.error) { alert(data.error); } else { setUsers(users.map(u => u.id === userId ? { ...u, role: newRole } : u)); } } catch (error) { alert("Failed to update role"); } finally { setTogglingId(null); } }; const fetchReports = async () => { try { const res = await fetch("/api/reports"); const data = await res.json(); setReports(data); } catch (error) { console.error("Failed to fetch reports"); } finally { setLoading(false); } }; const filteredReports = reports.filter(r => r.user?.name?.toLowerCase().includes(search.toLowerCase()) || r.managerName?.toLowerCase().includes(search.toLowerCase()) ); if (loading) return
Loading reports...
; return (
{tab === "REPORTS" && (
setSearch(e.target.value)} className="glass-input w-full pl-10 pr-4 py-2 rounded-lg text-sm" />
)}
{tab === "REPORTS" ? (
{filteredReports.map((report) => (
setExpandedId(expandedId === report.id ? null : report.id)} >

{report.user?.name || "Unknown User"}

{new Date(report.date).toLocaleDateString()} • Manager: {report.managerName}

{report.status} {expandedId === report.id ? : }
{expandedId === report.id && (

Planned Tasks

{report.tasks.filter((t: any) => t.type === 'PLANNED').map((task: any) => (

{task.description}

Est: {task.timeEstimate} • {task.notes}

))}

Completed Tasks

{report.tasks.filter((t: any) => t.type === 'COMPLETED').map((task: any) => (

{task.description}

{task.status} {task.link && ( View Work )}
))}
)}
))} {filteredReports.length === 0 &&

No reports found matching your criteria.

}
) : tab === "USERS" ? (

Employee List

Manage admin privileges for your team.

{usersLoading ? (
Loading users...
) : (
{users.map((user) => { const lastReport = user.reports?.[0]; return (
{user.image ? ( {user.name} ) : (
)}

{user.name || "Unnamed"}

{user.role}

{user.email}

{lastReport && (

Last report: {new Date(lastReport.date).toLocaleDateString()} •{" "} {lastReport.status}

)}
); })} {users.length === 0 && (

No users found.

)}
)}
) : (

Google Drive Configuration

Designate a specific folder where all exported reports will be saved.

setFolderId(e.target.value)} placeholder="Enter Folder ID (from URL)" className="glass-input w-full px-4 py-3 rounded-xl text-sm" />

Example ID: `1abc12345xyz_...` (Found in the URL of your Google Drive folder)

)}
); }