Files
AI_CPA/frontend/src/pages/AdminUserDetail.jsx
T
Atharv1705 9c7f25e06f feat: major platform upgrade — AI extraction pipeline, responsive UI, admin chatbot enhancements
CRITICAL FIXES
- Fix blank pages: useCallback declared after useEffect in all 5 pages
- Fix showToast undefined everywhere (ToastContext exported addToast not showToast)
- Fix admin doc upload always 422 (missing doc_key + doc_label form fields)
- Fix audit log wrong field names (log.details -> log.detail, remove user_agent)
- Fix SPA catch-all missing in main.py (direct URL access was 404)
- Fix chatbot using broken nvidia free model -> llama-3.3-70b

AI EXTRACTION PIPELINE
- 3-stage pipeline: Stage1 native text (free) -> Stage2 Tesseract OCR (free CPU) -> Stage3 Qwen vision (paid)
- Tesseract installed with eng+hin bilingual support, psm6, 2.5x DPI
- Scanned PDF fallback via PyMuPDF page render
- Blank template fallback: returns form_type/purpose/tax_year instead of empty
- Improved prompt: extracts actual document field names, no predefined terms
- Auto-poll UI: shows Processing spinner when extraction is running
- backfill_extraction.py: re-process all docs, fixed empty-dict-as-None bug

ADMIN DASHBOARD
- Pending docs split into Pending Personal + Pending Business stat cards
- User cards show typed badges: N personal / N business instead of generic N pending
- Backend returns pending_personal, pending_business, pending_docs per user

ADMIN USER DETAIL
- Doc count chips in user info bar: personal / business / returns / total
- Doc name bold 15px, date with calendar icon + TY chip, status as colored badges
- Responsive layout: doc-review-layout CSS classes replace inline flex styles
- Filing Timeline sidebar stacks above doc list on mobile
- AI Summary: bold labels and values, blank template shows metadata

CHATBOT ENHANCEMENTS
- Ask anything free-text input on menu screen (both admin and user chatbots)
- Admin: live context with latest uploads by date, new users today
- Admin: per-user activity lookup by name/email (docs + audit log)
- New endpoint: /api/chatbot/admin-user-context
- ExpandableSummaryBlock + ExpandableValue: bold navy field labels and values

RESPONSIVENESS
- components.css: 480px/640px/768px breakpoints for all pages
- Admin table, stat cards, tabs, toasts, FAB all properly responsive
- Auth shell stacks on mobile correctly
2026-08-05 11:41:06 +05:30

584 lines
28 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState, useEffect, useCallback, useRef } from 'react';
import { useSearchParams, useNavigate } from 'react-router-dom';
import { useAuth } from '../context/AuthContext';
import { useToast } from '../context/ToastContext';
import ExpandableSummaryBlock from '../components/ExpandableSummaryBlock';
export default function AdminUserDetail() {
const [searchParams] = useSearchParams();
const userId = searchParams.get('user_id');
const navigate = useNavigate();
const { authFetch, user: currentUser } = useAuth();
const { showToast } = useToast();
const [userDetail, setUserDetail] = useState(null);
const [documents, setDocuments] = useState([]);
const [activeTab, setActiveTab] = useState('personal');
const [taxYear, setTaxYear] = useState("");
const [loading, setLoading] = useState(true);
// States for Review Emails
const [personalTimeline, setPersonalTimeline] = useState(0);
const [businessTimeline, setBusinessTimeline] = useState(0);
// States for Audit Log
const [auditLog, setAuditLog] = useState([]);
const [auditLoaded, setAuditLoaded] = useState(false);
const adminDocInputRef = useRef(null);
const fetchUserDetails = useCallback(async () => {
setLoading(true);
try {
const url = taxYear ? `/api/upload/admin/users/${userId}/documents?tax_year=${taxYear}` : `/api/upload/admin/users/${userId}/documents`;
const res = await authFetch(url);
if (!res.ok) {
if (res.status === 403) navigate('/admin-dashboard');
return;
}
const payload = await res.json();
setUserDetail(payload.user);
// Map 'table' to 'type' for personal and business docs
const userDocs = (payload.documents || []).map(d => ({ ...d, type: d.table }));
// Fetch admin docs separately
const adminDocsRes = await authFetch(`/api/upload/admin-documents?user_id=${userId}`);
let adminDocs = [];
if (adminDocsRes.ok) {
adminDocs = await adminDocsRes.json();
adminDocs = adminDocs.map(d => ({ ...d, type: 'admin', uploaded_at: d.created_at }));
}
setDocuments([...userDocs, ...adminDocs]);
} catch (err) {
console.error(err);
} finally {
setLoading(false);
}
}, [authFetch, navigate, taxYear, userId]);
useEffect(() => {
if (!userId) {
navigate('/admin-dashboard');
return;
}
fetchUserDetails();
}, [userId, taxYear, navigate, fetchUserDetails]);
const loadAuditLog = useCallback(async () => {
try {
const res = await authFetch(`/api/auth/admin/audit-logs?user_id=${userId}&limit=30`);
if (res.ok) {
setAuditLog(await res.json());
setAuditLoaded(true);
}
} catch (err) {
console.error(err);
}
}, [authFetch, userId]);
useEffect(() => {
if (activeTab === 'audit' && !auditLoaded) {
loadAuditLog();
}
}, [activeTab, auditLoaded, loadAuditLog]);
// Auto-refresh if any doc is still awaiting AI extraction (background task lag)
useEffect(() => {
const hasPending = documents.some(
d => (d.type === 'personal' || d.type === 'business') && d.extracted_data === null
);
if (!hasPending) return;
const timer = setTimeout(() => fetchUserDetails(), 5000);
return () => clearTimeout(timer);
}, [documents, fetchUserDetails]);
const deleteUser = async () => {
if (!window.confirm("WARNING: This will permanently delete this user, all their documents, and their audit history. Are you sure?")) return;
try {
const res = await authFetch(`/api/upload/admin/users/${userId}`, { method: "DELETE" });
if (res.ok) {
navigate('/admin-dashboard');
} else {
alert("Failed to delete user");
}
} catch (err) {
console.error("Error deleting user", err);
}
};
const handleChangeRole = async (newRole) => {
if (!window.confirm(`Change user role to ${newRole}?`)) return;
try {
const res = await authFetch(`/api/auth/admin/users/${userId}/role`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ role: newRole })
});
if (res.ok) {
showToast("Role updated successfully", "success");
fetchUserDetails();
} else {
const data = await res.json();
showToast(data.detail || "Failed to change role", "error");
}
} catch (err) {
showToast("Network error", "error");
}
};
const handleDocApprove = async (docId, type, isApproved) => {
try {
const status = isApproved ? 'approved' : 'rejected';
const ep = type === "personal" ? `/api/upload/personal-documents/${docId}/review-status` : `/api/upload/business-documents/${docId}/review-status`;
const res = await authFetch(ep, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ status, note: "" })
});
if (res.ok) {
fetchUserDetails(); // reload docs
} else {
showToast("Failed to save review status", "error");
}
} catch (err) {
console.error("Approval error", err);
showToast("Approval error", "error");
}
};
const deleteAdminDoc = async (docId) => {
if (!window.confirm("Delete this admin document?")) return;
try {
const res = await authFetch(`/api/upload/admin-documents/${docId}`, { method: "DELETE" });
if (res.ok) {
showToast("Document deleted", "success");
fetchUserDetails();
}
} catch(err) {
console.error(err);
}
};
const uploadAdminDoc = async (file) => {
const fd = new FormData();
fd.append("file", file);
fd.append("user_id", userId);
// doc_key and doc_label are required by the backend
const label = file.name.replace(/\.[^.]+$/, ''); // strip extension for a clean label
fd.append("doc_key", `admin_${Date.now()}`);
fd.append("doc_label", label);
try {
const res = await authFetch("/api/upload/admin-documents", {
method: "POST",
body: fd
});
if (res.ok) {
showToast("Admin document uploaded", "success");
fetchUserDetails();
} else {
showToast("Failed to upload admin document", "error");
}
} catch(err) {
showToast("Upload failed", "error");
}
};
const viewDoc = async (storageKey) => {
try {
const res = await authFetch(`/api/upload/view-url?key=${encodeURIComponent(storageKey)}`);
if (res.ok) {
const data = await res.json();
window.open(data.url, "_blank");
} else {
showToast("Could not generate view link", "error");
}
} catch (err) {
showToast("Could not generate view link", "error");
}
};
const sendEmail = async (type) => {
const docsToReview = documents.filter(d => d.type === type);
const approved = docsToReview.filter(d => d.review_status === "approved").map(d => d.doc_type);
const rejected = docsToReview.filter(d => d.review_status === "rejected").map(d => d.doc_type);
if (approved.length === 0 && rejected.length === 0) {
showToast(`No ${type} documents selected.`, "warning");
return;
}
try {
const res = await authFetch("/api/review/notify-user", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
user_id: userId,
type: type,
approved: approved,
rejected: rejected,
personal_timeline: type === "personal" ? personalTimeline : 0,
business_timeline: type === "business" ? businessTimeline : 0
})
});
if (res.ok) {
showToast(`${type.charAt(0).toUpperCase() + type.slice(1)} approval email sent`, "success");
} else {
showToast("Failed to send email", "error");
}
} catch(err) {
showToast("Error sending email", "error");
}
};
const submitAllDocs = async () => {
if (!window.confirm("Submit all documents for review?\nAn email will be sent to the user.")) return;
try {
const res = await authFetch("/api/review/submit", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ user_id: userId })
});
if (res.ok) {
showToast("Documents submitted — email sent to user.", "success");
} else {
showToast("Failed to submit", "error");
}
} catch (err) {
showToast("Error submitting documents", "error");
}
};
if (loading && !userDetail) {
return <div className="page-main-wide page-enter"><div className="empty-state"><div className="empty-title">Loading user details...</div></div></div>;
}
if (!userDetail) {
return <div className="page-main-wide page-enter"><div className="empty-state"><div className="empty-title">User not found</div></div></div>;
}
const currentYear = new Date().getFullYear();
const yearOptions = [currentYear - 1, currentYear, currentYear + 1];
const personalDocs = documents.filter(d => d.type === "personal");
const businessDocs = documents.filter(d => d.type === "business");
const returnDocs = documents.filter(d => d.type === "admin");
return (
<div className="page-main-wide page-enter">
{/* User Info Bar */}
<div className="card aud-user-bar" style={{marginBottom: '20px'}}>
<div className="aud-bar-inner">
<div className="aud-bar-left">
<div style={{width:'52px', height:'52px', borderRadius:'50%', background:'linear-gradient(135deg,var(--header-bg),var(--blue-light))', display:'flex', alignItems:'center', justifyContent:'center', fontWeight:700, fontSize:'20px', color:'var(--navy)', flexShrink:0}}>
{(userDetail.name || "U").charAt(0).toUpperCase()}
</div>
<div>
<div style={{fontWeight:700, fontSize:'18px', color:'var(--navy)'}}>{userDetail.name || "—"}</div>
<div style={{fontSize:'13px', color:'var(--muted)', marginTop:'2px'}}>{userDetail.email}</div>
<div style={{display:'flex', gap:'8px', marginTop:'6px', flexWrap:'wrap'}}>
{currentUser?.role === 'super_admin' && currentUser.id !== userDetail.id ? (
<select
className="select-input"
style={{padding: '2px 8px', fontSize: '12px', height: 'auto', borderRadius: 'var(--radius-pill)', background: 'var(--card)', border: '1px solid var(--border)'}}
value={userDetail.role}
onChange={(e) => handleChangeRole(e.target.value)}
>
<option value="user">user</option>
<option value="admin">admin</option>
{userDetail.role === 'super_admin' && <option value="super_admin" disabled>super_admin</option>}
</select>
) : (
<span className={`badge ${userDetail.role === 'super_admin' ? 'badge-red' : userDetail.role === 'admin' ? 'badge-orange' : 'badge-blue'}`}>{userDetail.role}</span>
)}
{!userDetail.engagement_acknowledged_at && (
<span className="badge badge-yellow"><span className="material-symbols-outlined" style={{fontSize: '14px'}}>pending</span> Engagement not acknowledged</span>
)}
</div>
{/* Doc count summary */}
<div style={{display:'flex', gap:'12px', marginTop:'10px', flexWrap:'wrap'}}>
<div className="doc-count-chip">
<span className="material-symbols-outlined" style={{fontSize:'14px', color:'var(--emerald)'}}>description</span>
<span style={{fontWeight:600, color:'var(--navy)'}}>{personalDocs.length}</span>
<span style={{color:'var(--muted)'}}>personal</span>
</div>
<div className="doc-count-chip">
<span className="material-symbols-outlined" style={{fontSize:'14px', color:'var(--brass)'}}>business</span>
<span style={{fontWeight:600, color:'var(--navy)'}}>{businessDocs.length}</span>
<span style={{color:'var(--muted)'}}>business</span>
</div>
<div className="doc-count-chip">
<span className="material-symbols-outlined" style={{fontSize:'14px', color:'var(--muted)'}}>upload_file</span>
<span style={{fontWeight:600, color:'var(--navy)'}}>{returnDocs.length}</span>
<span style={{color:'var(--muted)'}}>returns</span>
</div>
<div className="doc-count-chip" style={{borderLeft:'1px solid var(--border)', paddingLeft:'12px'}}>
<span className="material-symbols-outlined" style={{fontSize:'14px', color:'var(--muted)'}}>folder</span>
<span style={{fontWeight:700, color:'var(--navy)'}}>{personalDocs.length + businessDocs.length + returnDocs.length}</span>
<span style={{color:'var(--muted)'}}>total</span>
</div>
</div>
</div>
</div>
<div className="aud-bar-actions">
<button className="btn btn-primary btn-sm" onClick={submitAllDocs} style={{borderRadius:'var(--radius-sm)'}}>Trigger Submit Review</button>
<div className="aud-year-picker">
<label className="form-label" style={{margin:0, fontSize: '11px'}}>Tax Year</label>
<select className="select-input" style={{width: 'auto', padding: '6px 12px', fontSize: '13px'}} value={taxYear} onChange={(e) => setTaxYear(e.target.value)}>
<option value="">All Years</option>
{yearOptions.map(y => <option key={y} value={y}>{y}</option>)}
</select>
</div>
<button className="btn btn-danger btn-sm" onClick={deleteUser} style={{borderRadius:'var(--radius-sm)'}}>Delete User</button>
</div>
</div>
</div>
{/* Tabs */}
<div className="tab-bar">
<button className={`tab-btn ${activeTab === 'personal' ? 'active' : ''}`} onClick={() => setActiveTab('personal')}><span className="material-symbols-outlined">description</span> Personal Docs</button>
<button className={`tab-btn ${activeTab === 'business' ? 'active' : ''}`} onClick={() => setActiveTab('business')}><span className="material-symbols-outlined">business</span> Business Docs</button>
<button className={`tab-btn ${activeTab === 'returns' ? 'active' : ''}`} onClick={() => setActiveTab('returns')}>📤 Returns for Review</button>
<button className={`tab-btn ${activeTab === 'audit' ? 'active' : ''}`} onClick={() => setActiveTab('audit')}><span className="material-symbols-outlined">history</span> Audit Trail</button>
</div>
{/* Tab: Personal */}
{activeTab === 'personal' && (
<div className="tab-panel active">
<div className="doc-review-layout">
<div className="doc-review-main">
<div className="card-flat">
{personalDocs.length === 0 ? (
<p className="text-muted">No personal documents uploaded{taxYear ? ` for ${taxYear}` : ''}.</p>
) : (
personalDocs.map(doc => (
<div key={doc.id} className="doc-review-row">
<div className="doc-review-row-top">
<div className="doc-review-info">
<div style={{ fontWeight:700, fontSize:'15px', color:'var(--navy)', lineHeight:'1.3' }}>{doc.doc_type}</div>
<div style={{ fontSize:'12px', color:'var(--muted)', marginTop:'3px' }}>
<span className="material-symbols-outlined" style={{fontSize:'12px', verticalAlign:'middle', marginRight:'3px'}}>calendar_today</span>
{new Date(doc.uploaded_at).toLocaleDateString('en-US', {year:'numeric', month:'short', day:'numeric'})}
{doc.tax_year && <span style={{marginLeft:'8px', fontWeight:600, color:'var(--brass)'}}>TY {doc.tax_year}</span>}
</div>
{doc.notes && <div style={{ fontSize:'12px', color:'var(--warn)', marginTop:'3px', fontWeight:500 }}>⚠ {doc.notes}</div>}
{doc.review_status && (
<div style={{ marginTop:'5px' }}>
<span className={`badge ${doc.review_status === 'approved' ? 'badge-green' : doc.review_status === 'rejected' ? 'badge-red' : 'badge-yellow'}`} style={{fontWeight:700}}>
{doc.review_status.toUpperCase()}
</span>
</div>
)}
</div>
<div className="doc-review-actions">
<button onClick={() => viewDoc(doc.storage_key)} className="btn btn-secondary btn-sm" style={{borderRadius:'var(--radius-sm)'}}>View</button>
<button className="btn btn-secondary btn-sm" style={{borderRadius:'var(--radius-sm)'}} onClick={() => handleDocApprove(doc.id, doc.type, true)}>Approve</button>
<button className="btn btn-danger btn-sm" style={{borderRadius:'var(--radius-sm)'}} onClick={() => handleDocApprove(doc.id, doc.type, false)}>Reject</button>
</div>
</div>
{doc.extracted_data && Object.keys(doc.extracted_data).length > 0 ? (
<div className="ai-summary-block">
<div className="ai-summary-label">
<span className="material-symbols-outlined" style={{ fontSize: '14px', verticalAlign: 'middle', marginRight: '4px' }}>auto_awesome</span>
AI Summary
</div>
<ExpandableSummaryBlock extractedData={doc.extracted_data} />
</div>
) : doc.extracted_data === null ? (
<div className="ai-summary-block" style={{ opacity: 0.6 }}>
<div className="ai-summary-label">
<span className="material-symbols-outlined" style={{ fontSize: '14px', verticalAlign: 'middle', marginRight: '4px', animation: 'spin 1.2s linear infinite' }}>progress_activity</span>
AI Summary — Processing…
</div>
</div>
) : null}
</div>
))
)}
</div>
</div>
<div className="doc-review-sidebar">
<div className="card-flat" style={{display:'flex', flexDirection:'column', gap:'16px'}}>
<h3>Filing Timeline</h3>
<div className="timeline-control">
<button className="tl-btn" onClick={() => setPersonalTimeline(Math.max(0, personalTimeline - 1))}>−</button>
<div>
<div className="tl-value">{personalTimeline}</div>
<div className="tl-label">days</div>
</div>
<button className="tl-btn" onClick={() => setPersonalTimeline(personalTimeline + 1)}>+</button>
</div>
<button className="btn btn-secondary w-full" style={{borderRadius:'var(--radius-sm)'}} onClick={() => sendEmail("personal")}>
Send Personal Approval Email
</button>
</div>
</div>
</div>
</div>
)}
{/* Tab: Business */}
{activeTab === 'business' && (
<div className="tab-panel active">
<div className="doc-review-layout">
<div className="doc-review-main">
<div className="card-flat">
{businessDocs.length === 0 ? (
<p className="text-muted">No business documents uploaded{taxYear ? ` for ${taxYear}` : ''}.</p>
) : (
businessDocs.map(doc => (
<div key={doc.id} className="doc-review-row">
<div className="doc-review-row-top">
<div className="doc-review-info">
<div style={{ fontWeight:700, fontSize:'15px', color:'var(--navy)', lineHeight:'1.3' }}>{doc.doc_type}</div>
<div style={{ fontSize:'12px', color:'var(--muted)', marginTop:'3px' }}>
<span className="material-symbols-outlined" style={{fontSize:'12px', verticalAlign:'middle', marginRight:'3px'}}>calendar_today</span>
{new Date(doc.uploaded_at).toLocaleDateString('en-US', {year:'numeric', month:'short', day:'numeric'})}
{doc.tax_year && <span style={{marginLeft:'8px', fontWeight:600, color:'var(--brass)'}}>TY {doc.tax_year}</span>}
</div>
{doc.notes && <div style={{ fontSize:'12px', color:'var(--warn)', marginTop:'3px', fontWeight:500 }}>⚠ {doc.notes}</div>}
{doc.review_status && (
<div style={{ marginTop:'5px' }}>
<span className={`badge ${doc.review_status === 'approved' ? 'badge-green' : doc.review_status === 'rejected' ? 'badge-red' : 'badge-yellow'}`} style={{fontWeight:700}}>
{doc.review_status.toUpperCase()}
</span>
</div>
)}
</div>
<div className="doc-review-actions">
<button onClick={() => viewDoc(doc.storage_key)} className="btn btn-secondary btn-sm" style={{borderRadius:'var(--radius-sm)'}}>View</button>
<button className="btn btn-secondary btn-sm" style={{borderRadius:'var(--radius-sm)'}} onClick={() => handleDocApprove(doc.id, doc.type, true)}>Approve</button>
<button className="btn btn-danger btn-sm" style={{borderRadius:'var(--radius-sm)'}} onClick={() => handleDocApprove(doc.id, doc.type, false)}>Reject</button>
</div>
</div>
{doc.extracted_data && Object.keys(doc.extracted_data).length > 0 ? (
<div className="ai-summary-block">
<div className="ai-summary-label">
<span className="material-symbols-outlined" style={{ fontSize: '14px', verticalAlign: 'middle', marginRight: '4px' }}>auto_awesome</span>
AI Summary
</div>
<ExpandableSummaryBlock extractedData={doc.extracted_data} />
</div>
) : doc.extracted_data === null ? (
<div className="ai-summary-block" style={{ opacity: 0.6 }}>
<div className="ai-summary-label">
<span className="material-symbols-outlined" style={{ fontSize: '14px', verticalAlign: 'middle', marginRight: '4px', animation: 'spin 1.2s linear infinite' }}>progress_activity</span>
AI Summary — Processing…
</div>
</div>
) : null}
</div>
))
)}
</div>
</div>
<div className="doc-review-sidebar">
<div className="card-flat" style={{display:'flex', flexDirection:'column', gap:'16px'}}>
<h3>Filing Timeline</h3>
<div className="timeline-control">
<button className="tl-btn" onClick={() => setBusinessTimeline(Math.max(0, businessTimeline - 1))}>−</button>
<div>
<div className="tl-value">{businessTimeline}</div>
<div className="tl-label">days</div>
</div>
<button className="tl-btn" onClick={() => setBusinessTimeline(businessTimeline + 1)}>+</button>
</div>
<button className="btn btn-secondary w-full" style={{borderRadius:'var(--radius-sm)'}} onClick={() => sendEmail("business")}>
Send Business Approval Email
</button>
</div>
</div>
</div>
</div>
)}
{/* Tab: Returns */}
{activeTab === 'returns' && (
<div className="tab-panel active">
<div className="card-flat">
<div style={{display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'16px'}}>
<h3 style={{margin:0}}>Admin Documents</h3>
<div>
<input type="file" hidden ref={adminDocInputRef} accept=".pdf,.doc,.docx,.xls,.xlsx" onChange={e => {if(e.target.files[0]) uploadAdminDoc(e.target.files[0])}} />
<button className="btn btn-primary btn-sm" style={{borderRadius:'var(--radius-sm)'}} onClick={() => adminDocInputRef.current?.click()}>+ Upload Document</button>
</div>
</div>
{returnDocs.length === 0 ? (
<p className="text-muted">No returns or final documents uploaded yet.</p>
) : (
<div className="table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Filename</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{returnDocs.map(doc => (
<tr key={doc.id}>
<td>
<div style={{fontWeight:500, color:'var(--navy)'}}>{doc.filename}</div>
</td>
<td>{new Date(doc.uploaded_at).toLocaleDateString()}</td>
<td>
<div style={{display:'flex', gap:'8px'}}>
<button className="btn btn-secondary btn-sm" onClick={() => viewDoc(doc.storage_key)}>View</button>
<button className="btn btn-danger btn-sm" onClick={() => deleteAdminDoc(doc.id)}>Delete</button>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</div>
)}
{/* Tab: Audit */}
{activeTab === 'audit' && (
<div className="tab-panel active">
<div className="card-flat">
<h3 style={{marginBottom:'16px'}}>Recent Activity</h3>
{auditLog.length === 0 ? (
<p className="text-muted">No audit logs found.</p>
) : (
<div className="table-responsive">
<table className="admin-table">
<thead>
<tr>
<th>Date</th>
<th>Action</th>
<th>Details</th>
<th>IP / Agent</th>
</tr>
</thead>
<tbody>
{auditLog.map(log => (
<tr key={log.id}>
<td style={{whiteSpace:'nowrap', color:'var(--muted)'}}>{new Date(log.created_at).toLocaleString()}</td>
<td><span className="badge badge-blue">{log.action}</span></td>
<td>{log.detail}</td>
<td className="text-sm text-muted">
{log.ip_address}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</div>
)}
</div>
);
}