import { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { useAuth } from '@/lib/auth'; import { api } from '@/lib/api'; import { useToast } from '@/hooks/useToast'; import { CreditCard, Plus, X } from 'lucide-react'; export default function BillingManager() { const { t } = useTranslation(); const { token } = useAuth(); const toast = useToast(); const [transactions, setTransactions] = useState([]); const [total, setTotal] = useState(0); const [page, setPage] = useState(1); const [loading, setLoading] = useState(true); const [showAddCredits, setShowAddCredits] = useState(false); const [creditOrgId, setCreditOrgId] = useState(''); const [creditAmount, setCreditAmount] = useState(''); const [creditDesc, setCreditDesc] = useState(''); const [saving, setSaving] = useState(false); const LIMIT = 20; async function load() { if (!token) return; setLoading(true); try { const data = await api.get(`/v1/super-admin/billing/transactions?page=${page}&limit=${LIMIT}`, token); setTransactions(data.data ?? []); setTotal(data.total ?? 0); } catch { toast.error(t('super_admin.err_load_transactions')); } finally { setLoading(false); } } useEffect(() => { load(); }, [page, token]); async function handleAddCredits() { if (!creditOrgId || !creditAmount) return; setSaving(true); try { const result = await api.post('/v1/super-admin/billing/credits', { orgId: creditOrgId, amount: parseInt(creditAmount), description: creditDesc || undefined, }, token); toast.success(t('super_admin.credits_added', { amount: creditAmount, balance: result.newBalance })); setShowAddCredits(false); setCreditOrgId(''); setCreditAmount(''); setCreditDesc(''); load(); } catch { toast.error(t('super_admin.err_add_credits')); } finally { setSaving(false); } } const TYPE_COLORS: Record = { TOP_UP_MANUAL: 'text-emerald-400', TOP_UP_PAYMENT: 'text-emerald-400', DEBIT_AI: 'text-red-400', }; return (

{t('super_admin.billing_title')}

{t('super_admin.billing_transactions', { count: total })}

{loading ? (
{t('super_admin.org_loading')}
) : transactions.length === 0 ? (

{t('super_admin.billing_no_transactions')}

) : (
{transactions.map(tx => ( ))}
{t('super_admin.col_date')} {t('super_admin.col_org')} {t('super_admin.col_type')} {t('super_admin.col_description')} {t('super_admin.col_amount')} {t('super_admin.col_balance_after')}
{new Date(tx.createdAt).toLocaleDateString()} {tx.organization?.name || '—'} {tx.type} {tx.description || '—'} 0 ? 'text-emerald-400' : 'text-red-400'}`}> {(tx.amount ?? 0) > 0 ? '+' : ''}{(tx.amount ?? 0).toLocaleString()} {tx.balanceAfter?.toLocaleString()}
)}
{total > LIMIT && (
{t('super_admin.pagination_info', { from: ((page - 1) * LIMIT) + 1, to: Math.min(page * LIMIT, total), total })}
)} {showAddCredits && (

{t('super_admin.modal_add_credits')}

setCreditOrgId(e.target.value)} placeholder="org-uuid..." className="mt-1.5 w-full bg-slate-800 border border-slate-700 rounded-lg px-3 py-2 text-sm text-white placeholder-slate-500 focus:outline-none focus:border-violet-500" />
setCreditAmount(e.target.value)} placeholder="100" className="mt-1.5 w-full bg-slate-800 border border-slate-700 rounded-lg px-3 py-2 text-sm text-white placeholder-slate-500 focus:outline-none focus:border-violet-500" />
setCreditDesc(e.target.value)} placeholder={t('super_admin.credits_desc_placeholder')} className="mt-1.5 w-full bg-slate-800 border border-slate-700 rounded-lg px-3 py-2 text-sm text-white placeholder-slate-500 focus:outline-none focus:border-violet-500" />
)}
); }