'use client' import { useState, useEffect } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Label } from '@/components/ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { ArrowLeft, Loader2, Play, FileText, Link2, Calendar, Landmark, AlertTriangle, } from 'lucide-react' import { formatCurrency } from '@/lib/utils' import { summarizeByCurrency } from '@/lib/import/bank-file/currency-summary' import { createClient } from '@/lib/supabase/client' import { useCompany } from '@/contexts/CompanyContext' import type { BankFileParseResult } from '@/lib/import/bank-file/types' interface BankAccount { account_number: string account_name: string } interface BankFileConfirmStepProps { parseResult: BankFileParseResult onExecute: (options: { skip_duplicates: boolean; auto_categorize: boolean; settlement_account?: string }) => void onBack: () => void isLoading: boolean } export default function BankFileConfirmStep({ parseResult, onExecute, onBack, isLoading, }: BankFileConfirmStepProps) { const { transactions, stats, date_from, date_to, issues } = parseResult const refsCount = transactions.filter((t) => t.reference).length const warnings = issues.filter((i) => i.severity === 'warning') // Same per-currency grouping as the preview step: parser-level totals sum // across currencies, which misleads on Wise/camt.053 multi-currency files. const currencyTotals = summarizeByCurrency(transactions) const [bankAccounts, setBankAccounts] = useState([]) const [selectedAccount, setSelectedAccount] = useState('1930') const { company } = useCompany() useEffect(() => { if (!company?.id) return async function fetchBankAccounts(companyId: string) { const supabase = createClient() const { data } = await supabase .from('chart_of_accounts') .select('account_number, account_name') .eq('company_id', companyId) .eq('is_active', true) .gte('account_number', '1900') .lte('account_number', '1999') .order('account_number') if (data && data.length > 0) { setBankAccounts(data) // Default to 1930 if available, otherwise first account const has1930 = data.some(a => a.account_number === '1930') if (!has1930) setSelectedAccount(data[0].account_number) } } fetchBankAccounts(company.id) }, [company?.id]) if (isLoading) { return (

Importerar transaktioner...

{stats.parsed_rows} transaktioner bearbetas

) } return (
{/* Summary */} Bekräfta import Granska sammanfattningen och importera transaktionerna. {/* Stats grid */}
Transaktioner

{stats.parsed_rows}

{stats.skipped_rows > 0 && (

{stats.skipped_rows} rader hoppades över

)}
Period

{date_from}: {date_to}

Inkomster
{(currencyTotals.length ? currencyTotals : [{ currency: 'SEK', total_income: 0, total_expenses: 0 }]).map((row) => (

{formatCurrency(row.total_income, row.currency)}

))}
Utgifter
{(currencyTotals.length ? currencyTotals : [{ currency: 'SEK', total_income: 0, total_expenses: 0 }]).map((row) => (

{formatCurrency(row.total_expenses, row.currency)}

))}
{/* Bank account selector */} {bankAccounts.length > 1 && (

Välj vilket bankkonto transaktionerna ska bokföras mot.

)} {/* Additional info */} {refsCount > 0 && (
{refsCount} med OCR/referens
)}
{/* Skipped rows: surfaced here because the manual-mapping path skips the preview step where these warnings would otherwise be shown. */} {warnings.length > 0 && ( {warnings.length} {warnings.length === 1 ? 'rad' : 'rader'} hoppades över Dessa rader kunde inte läsas och importeras inte. Kontrollera att inga transaktioner saknas.
{warnings.slice(0, 10).map((issue, i) => (

Rad {issue.row}: {issue.message}

))} {warnings.length > 10 && (

…och {warnings.length - 10} till

)}
)} {/* Actions */}
) }