import { useState, useEffect, useMemo, useCallback } from 'react' import { useSearchParams } from 'react-router-dom' import { ArrowDown, ArrowUp, BarChart3, Plus, Search, ArrowRight, Check, RotateCcw, History, Pencil, Trash2 } from 'lucide-react' import { useTripStore } from '../../store/tripStore' import { useAuthStore } from '../../store/authStore' import { useSettingsStore } from '../../store/settingsStore' import { useCanDo } from '../../store/permissionsStore' import { useToast } from '../shared/Toast' import { useTranslation } from '../../i18n' import { budgetApi } from '../../api/client' import { useExchangeRates } from '../../hooks/useExchangeRates' import { useIsMobile } from '../../hooks/useIsMobile' import { formatMoney, currencyDecimals, currencyLocale } from '../../utils/formatters' import Modal from '../shared/Modal' import CustomSelect from '../shared/CustomSelect' import { CustomDatePicker } from '../shared/CustomDateTimePicker' import { SYMBOLS, CURRENCIES, SPLIT_COLORS } from './BudgetPanel.constants' import { COST_CATEGORY_LIST, catMeta } from './costsCategories' import type { BudgetItem } from '../../types' import type { TripMember } from './BudgetPanelMemberChips' interface CostsPanelProps { tripId: number tripMembers?: TripMember[] } interface Settlement { id: number from_user_id: number to_user_id: number amount: number created_at?: string from_username?: string to_username?: string } interface SettlementData { balances: { user_id: number; username: string; avatar_url: string | null; balance: number }[] flows: { from: { user_id: number; username: string }; to: { user_id: number; username: string }; amount: number }[] settlements: Settlement[] } const round2 = (n: number) => Math.round(n * 100) / 100 const FIELD_H = 40 // shared height for the amount / currency / day row in the modal export default function CostsPanel({ tripId, tripMembers = [] }: CostsPanelProps) { const { trip, budgetItems, deleteBudgetItem, loadBudgetItems } = useTripStore() const me = useAuthStore(s => s.user?.id ?? -1) const can = useCanDo() const canEdit = can('budget_edit', trip) const toast = useToast() const { t, locale } = useTranslation() const isMobile = useIsMobile() // Display/base currency = the user's preferred currency (Settings), falling back // to the trip's own currency. Everything in Costs is converted to and shown in it. const displayCurrency = useSettingsStore(s => s.settings.default_currency) const base = (displayCurrency || trip?.currency || 'EUR').toUpperCase() // Pre-rework rows stored currency = NULL, meaning "the trip's own currency". const tripCurrency = (trip?.currency || base).toUpperCase() const { convert } = useExchangeRates(base) const curOf = useCallback((e: BudgetItem) => (e.currency || tripCurrency), [tripCurrency]) const [settlement, setSettlement] = useState(null) const [filter, setFilter] = useState<'all' | 'mine' | 'owed'>('all') const [search, setSearch] = useState('') const [histOpen, setHistOpen] = useState(false) const [modalOpen, setModalOpen] = useState(false) const [editing, setEditing] = useState(null) const people = tripMembers const personById = useCallback((id: number) => people.find(p => p.id === id), [people]) const personName = useCallback((id: number) => id === me ? t('costs.you') : (personById(id)?.username || '?'), [me, personById, t]) const colorFor = useCallback((id: number) => { const idx = people.findIndex(p => p.id === id) return SPLIT_COLORS[(idx >= 0 ? idx : 0) % SPLIT_COLORS.length].gradient }, [people]) const initial = useCallback((id: number) => id === me ? t('costs.youShort') : (personById(id)?.username || '?').charAt(0).toUpperCase(), [me, personById, t]) const fmt = useCallback((v: number, c = base) => formatMoney(v, c, locale), [base, locale]) const fmt0 = useCallback((v: number, c = base) => formatMoney(v, c, locale, { decimals: 0 }), [base, locale]) const loadSettlement = useCallback(() => { budgetApi.settlement(tripId, base).then(setSettlement).catch(() => {}) }, [tripId, base]) useEffect(() => { loadBudgetItems(tripId); loadSettlement() }, [tripId]) useEffect(() => { loadSettlement() }, [budgetItems.length, base]) // The bottom-nav "+" on the Costs tab opens the add-expense modal via ?create=expense. const [searchParams, setSearchParams] = useSearchParams() useEffect(() => { if (searchParams.get('create') === 'expense') { setEditing(null); setModalOpen(true) setSearchParams(p => { p.delete('create'); return p }, { replace: true }) } }, [searchParams]) // ── derived expense maths (everything converted to the base currency) ──── const baseTotal = (e: BudgetItem) => convert(e.total_price || 0, curOf(e)) const myPaidOf = (e: BudgetItem) => (e.payers || []).filter(p => p.user_id === me).reduce((a, p) => a + convert(p.amount, curOf(e)), 0) const myShareOf = (e: BudgetItem) => { const n = (e.members || []).length if (!n || !(e.members || []).some(m => m.user_id === me)) return 0 return baseTotal(e) / n } const totals = useMemo(() => { const totalSpend = budgetItems.reduce((a, e) => a + baseTotal(e), 0) const myPaid = budgetItems.reduce((a, e) => a + myPaidOf(e), 0) const myShare = budgetItems.reduce((a, e) => a + myShareOf(e), 0) const owe = (settlement?.flows || []).filter(f => f.from.user_id === me).reduce((a, f) => a + f.amount, 0) const owed = (settlement?.flows || []).filter(f => f.to.user_id === me).reduce((a, f) => a + f.amount, 0) return { totalSpend, myPaid, myShare, owe, owed } }, [budgetItems, settlement, me]) // ── filtering + day grouping ──────────────────────────────────────────── const filtered = useMemo(() => { let list = budgetItems.slice() if (filter === 'mine') list = list.filter(e => myPaidOf(e) > 0) if (filter === 'owed') list = list.filter(e => round2(myPaidOf(e) - myShareOf(e)) > 0) const q = search.trim().toLowerCase() if (q) list = list.filter(e => e.name.toLowerCase().includes(q)) return list }, [budgetItems, filter, search, me]) const dayGroups = useMemo(() => { const groups: { day: string; items: BudgetItem[] }[] = [] const labelOf = (e: BudgetItem) => { if (!e.expense_date) return t('costs.noDate') try { return new Date(e.expense_date + 'T00:00:00Z').toLocaleDateString(locale, { weekday: 'short', day: 'numeric', month: 'short', timeZone: 'UTC' }) } catch { return e.expense_date } } const sorted = filtered.slice().sort((a, b) => (b.expense_date || '').localeCompare(a.expense_date || '')) for (const e of sorted) { const day = labelOf(e) let g = groups.find(x => x.day === day) if (!g) { g = { day, items: [] }; groups.push(g) } g.items.push(e) } return groups }, [filtered, locale, t]) // ── settle actions ────────────────────────────────────────────────────── const settleFlow = async (fromId: number, toId: number, amount: number) => { try { await budgetApi.createSettlement(tripId, { from_user_id: fromId, to_user_id: toId, amount }) loadSettlement() } catch { toast.error(t('common.unknownError')) } } const undoSettlement = async (id: number) => { try { await budgetApi.deleteSettlement(tripId, id); loadSettlement() } catch { toast.error(t('common.unknownError')) } } const settleAll = async () => { const flows = settlement?.flows || [] if (!flows.length) return try { for (const f of flows) await budgetApi.createSettlement(tripId, { from_user_id: f.from.user_id, to_user_id: f.to.user_id, amount: f.amount }) loadSettlement() } catch { toast.error(t('common.unknownError')) } } const dateMeta = useMemo(() => { if (!trip?.start_date || !trip?.end_date) return null try { const s = new Date(trip.start_date + 'T00:00:00Z'), e = new Date(trip.end_date + 'T00:00:00Z') const days = Math.round((e.getTime() - s.getTime()) / 86400000) + 1 const opt = { day: 'numeric', month: 'short', timeZone: 'UTC' } as const return { range: `${s.toLocaleDateString(locale, opt)} – ${e.toLocaleDateString(locale, opt)}`, days } } catch { return null } }, [trip?.start_date, trip?.end_date, locale]) const handleDelete = async (id: number) => { try { await deleteBudgetItem(tripId, id); loadSettlement() } catch { toast.error(t('common.unknownError')) } } // ── small presentational helpers ──────────────────────────────────────── const Avatar = ({ id, size = 24 }: { id: number; size?: number }) => { const url = personById(id)?.avatar_url if (url) return return {initial(id)} } const cardCls = 'bg-surface-card border border-edge' const labelCls = 'text-[11px] font-semibold uppercase tracking-[0.12em] text-content-faint' // Big money number with the design's muted symbol/decimals, locale-correct via Intl. const bigMoney = (amount: number, smallSize: number, mutedColor: string) => { let parts: Intl.NumberFormatPart[] | null = null try { const d = currencyDecimals(base) parts = new Intl.NumberFormat(currencyLocale(base), { style: 'currency', currency: base, minimumFractionDigits: d, maximumFractionDigits: d }).formatToParts(amount || 0) } catch { return <>{formatMoney(amount, base, locale)} } const isBig = (p: Intl.NumberFormatPart) => p.type === 'integer' || p.type === 'group' || p.type === 'minusSign' return <>{parts.map((p, i) => {p.value})} } return (
{isMobile ? : (
{/* ── Header bar ── */}
{dateMeta && ( {dateMeta.range} · {t('costs.daysCount', { count: dateMeta.days })} )} {people.slice(0, 4).map((p, i) => { const common = { width: 22, height: 22, borderRadius: '50%', border: '2px solid var(--bg-card)', marginLeft: i ? -8 : 0, flexShrink: 0 } as const return p.avatar_url ? : {(p.id === me ? t('costs.youShort') : p.username.charAt(0)).toUpperCase()} })} {t('costs.travelers', { count: people.length })}
{canEdit && (
)}
{/* ── Summary cards ── */}
} tone="owe" foot={totals.owe > 0.01 ? f.from.user_id === me).map(f => f.to.user_id)} lead={t('costs.to')} Avatar={Avatar} name={personName} /> : {t('costs.allSettled')}} /> } tone="owed" foot={totals.owed > 0.01 ? f.to.user_id === me).map(f => f.from.user_id)} lead={t('costs.from')} Avatar={Avatar} name={personName} /> : {t('costs.nothingOwed')}} /> } tone="total" foot={{t('costs.yourShare')} · {fmt0(totals.myShare)}{t('costs.youPaid')} · {fmt0(totals.myPaid)}} />
{/* ── Main grid ── */}
{/* expenses */}

{t('costs.expenses')}

setSearch(e.target.value)} placeholder={t('costs.searchPlaceholder')} className="text-content" style={{ border: 0, background: 'none', outline: 'none', fontSize: 13, width: 150, fontFamily: 'inherit' }} />
{(['all', 'mine', 'owed'] as const).map(f => ( ))}
{dayGroups.length === 0 ? (
{search ? t('costs.noMatch') : t('costs.emptyText')}
) : dayGroups.map(g => { const dtot = g.items.reduce((a, e) => a + baseTotal(e), 0) return (
{g.day}{t('costs.spent', { amount: fmt(dtot) })}
{g.items.map(e => )}
) })}
{/* sidebar */}
{/* settle up */}
{t('costs.settleUp')} · {(settlement?.flows || []).length}
{/* balances */}
{t('costs.balances')}
{/* by category */}
{t('costs.byCategory')}
)} {modalOpen && ( setModalOpen(false)} onSaved={() => { setModalOpen(false); loadBudgetItems(tripId); loadSettlement() }} /> )} setHistOpen(false)} title={t('costs.settleHistory')} size="md">
) // ── shared settle-flow list ────────────────────────────────────────────── function SettleFlows() { const flows = settlement?.flows || [] if (flows.length === 0) return (
{t('costs.everyoneSquare')}
{t('costs.nothingOutstanding')}
) return (
{flows.map((f, i) => (
{fmt(f.amount)} {canEdit && }
))}
) } // ── mobile layout (Budget1Mobile.html): single flat column, total card on top ── function MobileBody() { return (
{/* Total card */}
{t('costs.totalSpend')}
{bigMoney(totals.totalSpend, 24, 'rgba(255,255,255,0.6)')}
{t('costs.yourShare')} · {fmt0(totals.myShare)} {t('costs.youPaid')} · {fmt0(totals.myPaid)}
{canEdit && ( )}
{/* Owe / Owed */}
{t('costs.youOwe')}
{t('costs.youOweSub')}
{bigMoney(totals.owe, 16, 'var(--c-ink3)')}
{t('costs.youreOwed')}
{t('costs.youreOwedSub')}
{bigMoney(totals.owed, 16, 'var(--c-ink3)')}
{/* Settle up */}
{t('costs.settleUp')} {(settlement?.flows || []).length}
{/* Expenses */}
{t('costs.expenses')}
setSearch(e.target.value)} placeholder={t('costs.searchPlaceholder')} className="text-content" style={{ border: 0, background: 'none', outline: 'none', fontSize: 14, width: '100%', fontFamily: 'inherit' }} />
{(['all', 'mine', 'owed'] as const).map(f => ( ))}
{dayGroups.length === 0 ?
{search ? t('costs.noMatch') : t('costs.emptyText')}
: dayGroups.map(g => { const dtot = g.items.reduce((a, e) => a + baseTotal(e), 0) return (
{g.day}{t('costs.spent', { amount: fmt(dtot) })}
{g.items.map(e => )}
) })}
{/* Balances */}
{t('costs.balances')}
{/* By category */}
{t('costs.byCategory')}
) } // ── inline subcomponents (close over helpers) ──────────────────────────── function ExpenseRow({ e }: { e: BudgetItem }) { const c = catMeta(e.category) const Icon = c.Icon const cur = curOf(e) const payers = (e.payers || []).filter(p => p.amount > 0) const net = round2(myPaidOf(e) - myShareOf(e)) return (
{e.name}
{payers.length > 0 && (
{payers.map(p => ( {fmt(convert(p.amount, cur))} ))}
)} {!isMobile && (
{t(c.labelKey)}{cur !== base ? ` · ${fmt(e.total_price, cur)} → ${fmt(baseTotal(e))}` : ''}
)}
{fmt(baseTotal(e))}
{(e.members || []).length > 0 && Math.abs(net) > 0.01 && (
0 ? '#16a34a' : '#dc2626' }}> {net > 0 ? t('costs.youLent', { amount: fmt(net) }) : t('costs.youBorrowed', { amount: fmt(-net) })}
)}
{canEdit && (
)}
) } function BalancesList({ balances }: { balances: SettlementData['balances'] }) { const rows = people.map(p => balances.find(b => b.user_id === p.id) || { user_id: p.id, username: p.username, avatar_url: null, balance: 0 }) const max = Math.max(1, ...rows.map(r => Math.abs(r.balance))) return (
{rows.map(r => { const pct = Math.min(100, Math.abs(r.balance) / max * 100) const pos = r.balance > 0.01, neg = r.balance < -0.01 return (
{personName(r.user_id)}
{pos && } {neg && }
{pos ? '+' + fmt(r.balance) : neg ? '−' + fmt(-r.balance) : fmt(0)}
) })}
) } function CategoryBreakdown() { const tot: Record = {} let grand = 0 for (const e of budgetItems) { const k = catMeta(e.category).key; tot[k] = (tot[k] || 0) + baseTotal(e); grand += baseTotal(e) } const rows = COST_CATEGORY_LIST.filter(c => (tot[c.key] || 0) > 0).sort((a, b) => (tot[b.key] || 0) - (tot[a.key] || 0)) if (rows.length === 0) return
{t('costs.noCategories')}
return (
{rows.map(c => { const v = tot[c.key]; const pct = grand ? v / grand * 100 : 0 return (
{t(c.labelKey)} {fmt0(v)}
) })}
) } } // ── pure subcomponents ───────────────────────────────────────────────────── function SummaryCard({ label, sub, amount, currency, locale, icon, foot, tone }: { label: string; sub: string; amount: number; currency: string; locale: string; icon: React.ReactNode; foot: React.ReactNode; tone: 'owe' | 'owed' | 'total' }) { const total = tone === 'total' const accent = tone === 'owe' ? '#dc2626' : tone === 'owed' ? '#16a34a' : undefined const muted = total ? 'rgba(255,255,255,0.55)' : 'var(--text-faint)' // formatToParts keeps the design's "big integer + muted symbol/decimals" styling // while letting Intl place the symbol and pick separators per locale + currency. let parts: Intl.NumberFormatPart[] | null = null try { const d = currencyDecimals(currency) parts = new Intl.NumberFormat(currencyLocale(currency), { style: 'currency', currency: (currency || 'EUR').toUpperCase(), minimumFractionDigits: d, maximumFractionDigits: d }).formatToParts(amount || 0) } catch { parts = null } const big = (p: Intl.NumberFormatPart) => p.type === 'integer' || p.type === 'group' || p.type === 'minusSign' return (
{icon}
{label}
{sub}
{parts ? parts.map((p, i) => {p.value}) : {formatMoney(amount, currency, locale)}}
{foot}
) } function FlowPills({ ids, lead, Avatar, name }: { ids: number[]; lead: string; Avatar: (p: { id: number; size?: number }) => React.JSX.Element; name: (id: number) => string }) { const uniq = Array.from(new Set(ids)) return ( {lead} {uniq.map(id => ( {name(id)} ))} ) } function SettleHistory({ settlements, fmt, Avatar, name, onUndo, canEdit }: { settlements: Settlement[]; fmt: (v: number) => string; Avatar: (p: { id: number; size?: number }) => React.JSX.Element; name: (id: number) => string; onUndo: (id: number) => void; canEdit: boolean }) { const { t } = useTranslation() if (settlements.length === 0) return
{t('costs.noSettlements')}
const total = settlements.reduce((a, s) => a + s.amount, 0) return (
{t('costs.paymentsSettled', { count: settlements.length })}{fmt(total)}
{settlements.map(s => (
{fmt(s.amount)} {canEdit && }
))}
) } // ── Add / edit expense modal ─────────────────────────────────────────────── function ExpenseModal({ tripId, base, people, me, editing, onClose, onSaved }: { tripId: number; base: string; people: TripMember[]; me: number; editing: BudgetItem | null; onClose: () => void; onSaved: () => void }) { const { t, locale } = useTranslation() const toast = useToast() const { addBudgetItem, updateBudgetItem } = useTripStore() const { convert } = useExchangeRates(base) const sym = (c: string) => SYMBOLS[c] || (c + ' ') const [name, setName] = useState(editing?.name || '') const [cat, setCat] = useState(editing ? catMeta(editing.category).key : 'food') const [currency, setCurrency] = useState((editing?.currency || base).toUpperCase()) const [day, setDay] = useState(editing?.expense_date || new Date().toISOString().slice(0, 10)) const [payers, setPayers] = useState>(() => { const m: Record = {} for (const p of editing?.payers || []) m[p.user_id] = String(p.amount) return m }) const [split, setSplit] = useState>(() => editing ? new Set((editing.members || []).map(m => m.user_id)) : new Set(people.map(p => p.id))) const [saving, setSaving] = useState(false) const payersTotal = Object.values(payers).reduce((a, v) => a + (parseFloat(v) || 0), 0) const each = split.size > 0 ? payersTotal / split.size : 0 const valid = name.trim().length > 0 && split.size > 0 && payersTotal > 0 const save = async () => { if (!valid) return setSaving(true) const payerList = Object.entries(payers).map(([uid, v]) => ({ user_id: Number(uid), amount: parseFloat(v) || 0 })).filter(p => p.amount > 0) const data = { name: name.trim(), category: cat, // Store the actual currency the amounts were entered in; conversion to the // viewer's display currency happens live (real rates), no manual rate. currency, payers: payerList, member_ids: [...split], expense_date: day || null, } try { if (editing) await updateBudgetItem(tripId, editing.id, data) else await addBudgetItem(tripId, data) onSaved() } catch { toast.error(t('common.unknownError')) } finally { setSaving(false) } } const inputCls = 'w-full bg-surface-input border border-edge text-content' const labelCls = 'block text-[11px] font-semibold uppercase tracking-[0.08em] text-content-faint mb-[6px]' return ( }>
setName(e.target.value)} placeholder={t('costs.namePlaceholder')} className={inputCls} style={{ borderRadius: 10, padding: '11px 13px', fontSize: 14, outline: 'none' }} />
{sym(currency)} {payersTotal.toFixed(2)}
setCurrency(String(v))} searchable options={CURRENCIES.map(c => ({ value: c, label: SYMBOLS[c] ? `${c} ${SYMBOLS[c]}` : c }))} style={{ width: '100%' }} />
{currency !== base && payersTotal > 0 && (
{formatMoney(payersTotal, currency, locale)} {formatMoney(convert(payersTotal, currency), base, locale)} · {t('costs.liveRate')}
)}
{COST_CATEGORY_LIST.map(c => { const Icon = c.Icon; const on = cat === c.key return ( ) })}
{people.map(p => (
{p.id === me ? t('costs.you') : p.username}
{sym(currency)} setPayers(prev => ({ ...prev, [p.id]: e.target.value }))} className="text-content" style={{ width: '100%', border: 0, background: 'none', outline: 'none', fontSize: 14, fontWeight: 600, padding: '8px 0', textAlign: 'right' }} />
))}
{people.map(p => { const on = split.has(p.id) return ( ) })}
{split.size === 0 ? t('costs.pickSomeone') : t('costs.splitSummary', { count: split.size, amount: sym(currency) + each.toFixed(2) })}
) }