import { For, Show } from 'solid-js' import { Button, Field, IconButton, Modal } from '../components/ui' import type { MiniAppAdminSettingsPayload, MiniAppDashboard } from '../miniapp-api' type PurchaseDraft = { description: string amountMajor: string currency: 'USD' | 'GEL' splitMode: 'equal' | 'custom_amounts' participants: { memberId: string shareAmountMajor: string }[] } type PaymentDraft = { memberId: string kind: 'rent' | 'utilities' amountMajor: string currency: 'USD' | 'GEL' } type Props = { copy: Record dashboard: MiniAppDashboard | null readyIsAdmin: boolean adminMembers: readonly MiniAppAdminSettingsPayload['members'][number][] purchaseEntries: readonly MiniAppDashboard['ledger'][number][] utilityEntries: readonly MiniAppDashboard['ledger'][number][] paymentEntries: readonly MiniAppDashboard['ledger'][number][] editingPurchaseEntry: MiniAppDashboard['ledger'][number] | null editingPaymentEntry: MiniAppDashboard['ledger'][number] | null purchaseDraftMap: Record paymentDraftMap: Record paymentForm: PaymentDraft addingPaymentOpen: boolean savingPurchaseId: string | null deletingPurchaseId: string | null savingPaymentId: string | null deletingPaymentId: string | null addingPayment: boolean ledgerTitle: (entry: MiniAppDashboard['ledger'][number]) => string ledgerPrimaryAmount: (entry: MiniAppDashboard['ledger'][number]) => string ledgerSecondaryAmount: (entry: MiniAppDashboard['ledger'][number]) => string | null purchaseParticipantSummary: (entry: MiniAppDashboard['ledger'][number]) => string purchaseDraftForEntry: (entry: MiniAppDashboard['ledger'][number]) => PurchaseDraft paymentDraftForEntry: (entry: MiniAppDashboard['ledger'][number]) => PaymentDraft purchaseSplitPreview: (purchaseId: string) => { memberId: string; amountMajor: string }[] paymentMemberName: (entry: MiniAppDashboard['ledger'][number]) => string onOpenPurchaseEditor: (purchaseId: string) => void onClosePurchaseEditor: () => void onDeletePurchase: (purchaseId: string) => Promise onSavePurchase: (purchaseId: string) => Promise onPurchaseDescriptionChange: ( purchaseId: string, entry: MiniAppDashboard['ledger'][number], value: string ) => void onPurchaseAmountChange: ( purchaseId: string, entry: MiniAppDashboard['ledger'][number], value: string ) => void onPurchaseCurrencyChange: ( purchaseId: string, entry: MiniAppDashboard['ledger'][number], value: 'USD' | 'GEL' ) => void onPurchaseSplitModeChange: ( purchaseId: string, entry: MiniAppDashboard['ledger'][number], value: 'equal' | 'custom_amounts' ) => void onTogglePurchaseParticipant: ( purchaseId: string, entry: MiniAppDashboard['ledger'][number], memberId: string, included: boolean ) => void onPurchaseParticipantShareChange: ( purchaseId: string, entry: MiniAppDashboard['ledger'][number], memberId: string, value: string ) => void onOpenAddPayment: () => void onCloseAddPayment: () => void onAddPayment: () => Promise onPaymentFormMemberChange: (value: string) => void onPaymentFormKindChange: (value: 'rent' | 'utilities') => void onPaymentFormAmountChange: (value: string) => void onPaymentFormCurrencyChange: (value: 'USD' | 'GEL') => void onOpenPaymentEditor: (paymentId: string) => void onClosePaymentEditor: () => void onDeletePayment: (paymentId: string) => Promise onSavePayment: (paymentId: string) => Promise onPaymentDraftMemberChange: ( paymentId: string, entry: MiniAppDashboard['ledger'][number], value: string ) => void onPaymentDraftKindChange: ( paymentId: string, entry: MiniAppDashboard['ledger'][number], value: 'rent' | 'utilities' ) => void onPaymentDraftAmountChange: ( paymentId: string, entry: MiniAppDashboard['ledger'][number], value: string ) => void onPaymentDraftCurrencyChange: ( paymentId: string, entry: MiniAppDashboard['ledger'][number], value: 'USD' | 'GEL' ) => void } export function LedgerScreen(props: Props) { if (!props.dashboard) { return (

{props.copy.emptyDashboard ?? ''}

) } return (
{props.readyIsAdmin ? props.copy.purchaseReviewTitle : props.copy.purchasesTitle}

{props.copy.purchaseReviewBody ?? ''}

{props.purchaseEntries.length === 0 ? (

{props.copy.purchasesEmpty ?? ''}

) : (
{(entry) => (
{entry.title} {entry.occurredAt?.slice(0, 10) ?? '—'}

{entry.actorDisplayName ?? props.copy.ledgerActorFallback ?? ''}

{props.ledgerPrimaryAmount(entry)} {(secondary) => ( {secondary()} )} {props.purchaseParticipantSummary(entry)}
props.onOpenPurchaseEditor(entry.id)} > ...
)}
)}
{ const entry = props.editingPurchaseEntry if (!entry) { return null } return ( ) })()} > {(() => { const entry = props.editingPurchaseEntry if (!entry) { return null } const draft = props.purchaseDraftMap[entry.id] ?? props.purchaseDraftForEntry(entry) const splitPreview = props.purchaseSplitPreview(entry.id) return ( <>
props.onPurchaseDescriptionChange(entry.id, entry, event.currentTarget.value) } /> props.onPurchaseAmountChange(entry.id, entry, event.currentTarget.value) } />
{props.copy.purchaseSplitTitle ?? ''} {draft.splitMode === 'custom_amounts' ? props.copy.purchaseSplitCustom : props.copy.purchaseSplitEqual}
{(member) => { const included = draft.participants.some( (participant) => participant.memberId === member.id ) const previewAmount = splitPreview.find((participant) => participant.memberId === member.id) ?.amountMajor ?? '0.00' return (
{member.displayName} {previewAmount} {draft.currency}
participant.memberId === member.id )?.shareAmountMajor ?? '' } onInput={(event) => props.onPurchaseParticipantShareChange( entry.id, entry, member.id, event.currentTarget.value ) } />
) }}
) })()}
{props.copy.utilityLedgerTitle ?? ''}
{props.utilityEntries.length === 0 ? (

{props.copy.utilityLedgerEmpty ?? ''}

) : (
{(entry) => (
{props.ledgerTitle(entry)} {props.ledgerPrimaryAmount(entry)}
{(secondary) =>

{secondary()}

}

{entry.actorDisplayName ?? props.copy.ledgerActorFallback ?? ''}

)}
)}
{props.copy.paymentsAdminTitle ?? ''}

{props.copy.paymentsAdminBody ?? ''}

{props.paymentEntries.length === 0 ? (

{props.copy.paymentsEmpty ?? ''}

) : (
{(entry) => (
{props.paymentMemberName(entry)} {entry.occurredAt?.slice(0, 10) ?? '—'}

{props.ledgerTitle(entry)}

{props.ledgerPrimaryAmount(entry)} {(secondary) => ( {secondary()} )}
props.onOpenPaymentEditor(entry.id)} > ...
)}
)}
} >
props.onPaymentFormAmountChange(event.currentTarget.value)} />
{ const entry = props.editingPaymentEntry if (!entry) { return null } return ( ) })()} > {(() => { const entry = props.editingPaymentEntry if (!entry) { return null } const draft = props.paymentDraftMap[entry.id] ?? props.paymentDraftForEntry(entry) return (
props.onPaymentDraftAmountChange(entry.id, entry, event.currentTarget.value) } />
) })()}
) }