'use client' import { useEffect, useState } from 'react' import { useAdminI18n } from '@/components/I18nProvider' import { ADMIN_API_BASE } from '@/lib/api' interface Renter { id: string firstName: string lastName: string email: string phone: string | null isBlocked: boolean createdAt: string } export default function AdminRentersPage() { const { language, dict } = useAdminI18n() const copy = { en: { title: 'Renters', search: 'Search renters…', name: 'Name', email: 'Email', phone: 'Phone', status: 'Status', joined: 'Joined', loading: 'Loading…', empty: 'No renters found', blocked: 'Blocked', active: 'Active', unblock: 'Unblock', block: 'Block', }, fr: { title: 'Locataires', search: 'Rechercher des locataires…', name: 'Nom', email: 'E-mail', phone: 'Téléphone', status: 'Statut', joined: 'Date d’inscription', loading: 'Chargement…', empty: 'Aucun locataire trouvé', blocked: 'Bloqué', active: 'Actif', unblock: 'Débloquer', block: 'Bloquer', }, ar: { title: 'المستأجرون', search: 'ابحث عن مستأجرين…', name: 'الاسم', email: 'البريد الإلكتروني', phone: 'الهاتف', status: 'الحالة', joined: 'تاريخ التسجيل', loading: 'جارٍ التحميل…', empty: 'لم يتم العثور على مستأجرين', blocked: 'محظور', active: 'نشط', unblock: 'فك الحظر', block: 'حظر', }, }[language] const [renters, setRenters] = useState([]) const [filtered, setFiltered] = useState([]) const [search, setSearch] = useState('') const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [actioning, setActioning] = useState(null) async function fetchRenters() { try { const res = await fetch(`${ADMIN_API_BASE}/admin/renters`, { cache: 'no-store', credentials: 'include', }) const json = await res.json() if (!res.ok) throw new Error(json?.message ?? 'Failed') const list = Array.isArray(json.data) ? json.data : (json.data?.data ?? []) setRenters(list) setFiltered(list) } catch (err: any) { setError(err.message) } finally { setLoading(false) } } useEffect(() => { fetchRenters() }, []) useEffect(() => { const q = search.toLowerCase() setFiltered(renters.filter((r) => `${r.firstName} ${r.lastName}`.toLowerCase().includes(q) || r.email.toLowerCase().includes(q) )) }, [search, renters]) async function toggleBlock(id: string, isBlocked: boolean) { setActioning(id) try { const endpoint = isBlocked ? 'unblock' : 'block' const res = await fetch(`${ADMIN_API_BASE}/admin/renters/${id}/${endpoint}`, { method: 'POST', credentials: 'include', }) if (!res.ok) throw new Error('Action failed') await fetchRenters() } catch (err: any) { setError(err.message) } finally { setActioning(null) } } return (

{dict.platform}

{copy.title}

setSearch(e.target.value)} />
{error &&
{error}
}
{loading ? ( ) : filtered.length === 0 ? ( ) : filtered.map((r) => ( ))}
{copy.name} {copy.email} {copy.phone} {copy.status} {copy.joined}
{copy.loading}
{copy.empty}
{r.firstName} {r.lastName} {r.email} {r.phone ?? '—'} {r.isBlocked ? copy.blocked : copy.active} {new Date(r.createdAt).toLocaleDateString()}
) }