'use client' import { useState } from 'react' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import type { AIProposal, BookingProposalLine, BookingProposalPayload } from '@/types' interface EditBookingDialogProps { proposal: AIProposal onClose: () => void onSubmit: (edits: BookingProposalPayload) => Promise } /** * Minimal in-place editor for a booking proposal. * * v1 lets the user change account numbers + amounts per line; the full * AccountCombobox + VatTreatmentSelect UX comes in a polish pass. The * point of this dialog is proving the edit-accept-learning-prompt path * works end-to-end before we invest in the richer form. */ export default function EditBookingDialog({ proposal, onClose, onSubmit }: EditBookingDialogProps) { const original = proposal.proposal_json as BookingProposalPayload const [lines, setLines] = useState(original.lines) const [description, setDescription] = useState(original.description) const [submitting, setSubmitting] = useState(false) const totalDebit = lines.reduce((s, l) => s + (Number(l.debit_amount) || 0), 0) const totalCredit = lines.reduce((s, l) => s + (Number(l.credit_amount) || 0), 0) const balanced = Math.abs(totalDebit - totalCredit) < 0.005 && totalDebit > 0 const updateLine = (index: number, patch: Partial) => { setLines((prev) => prev.map((l, i) => (i === index ? { ...l, ...patch } : l))) } const handleSubmit = async () => { if (!balanced) return setSubmitting(true) try { await onSubmit({ ...original, lines, description, }) } finally { setSubmitting(false) } } return ( !open && onClose()}> Redigera bokföringsförslag
setDescription(e.target.value)} className="mt-1" />
{lines.map((line, i) => ( ))}
Konto Beskrivning Debet Kredit
updateLine(i, { account_number: e.target.value })} className="h-8 font-mono w-20" maxLength={4} /> updateLine(i, { description: e.target.value })} className="h-8" /> updateLine(i, { debit_amount: parseFloat(e.target.value) || 0 }) } className="h-8 text-right tabular-nums w-24 ml-auto" /> updateLine(i, { credit_amount: parseFloat(e.target.value) || 0 }) } className="h-8 text-right tabular-nums w-24 ml-auto" />
Summa {totalDebit.toFixed(2)} {totalCredit.toFixed(2)}
{!balanced && (

Debet och kredit måste summera till samma belopp.

)}
) }