import { useState } from 'react'; import { Plus, Pencil, Trash2, TrendingUp } from 'lucide-react'; import { fmt, parseAmount } from '../utils.js'; import Modal from './Modal.jsx'; const TYPE_LABELS = { salary: { label: 'Lön', color: 'bg-blue-100 text-blue-700' }, benefit: { label: 'Förmån', color: 'bg-purple-100 text-purple-700' }, optional: { label: 'Valfri', color: 'bg-amber-100 text-amber-700' }, other: { label: 'Övrigt', color: 'bg-slate-100 text-slate-600' }, }; function IncomeForm({ initial, onSave, onClose }) { const [name, setName] = useState(initial?.name ?? ''); const [amount, setAmount] = useState(initial?.amount ?? ''); const [type, setType] = useState(initial?.type ?? 'salary'); const [notes, setNotes] = useState(initial?.notes ?? ''); function submit(e) { e.preventDefault(); if (!name.trim() || isNaN(parseAmount(amount))) return; onSave({ name: name.trim(), amount: parseAmount(amount), type, notes: notes.trim() || null }); onClose(); } return (
setName(e.target.value)} placeholder="t.ex. Lön" required autoFocus />
setAmount(e.target.value)} placeholder="0" required />
{type === 'benefit' && (

Förmåner visas separat och påverkar inte den ordinarie balansen.

)} {type === 'optional' && (

Valfri inkomst visas separat i sammanfattningen.

)}
setNotes(e.target.value)} placeholder="Valfri notering" />
); } export default function IncomeSection({ income, monthId, onAdd, onUpdate, onDelete }) { const [modal, setModal] = useState(null); // null | 'add' | {edit: item} const mandatory = income.filter(i => i.type !== 'optional' && i.type !== 'benefit'); const optional = income.filter(i => i.type === 'optional'); const benefits = income.filter(i => i.type === 'benefit'); const totalMandatory = mandatory.reduce((s, i) => s + i.amount, 0); return (
Inkomster
{income.map(item => { const tInfo = TYPE_LABELS[item.type] ?? TYPE_LABELS.other; return (
{tInfo.label} {item.name} {item.notes && {item.notes}} {fmt(item.amount)}
); })} {income.length === 0 && (

Inga inkomster tillagda

)}
{optional.length > 0 && `+ ${fmt(optional.reduce((s, i) => s + i.amount, 0))} valfri`} {benefits.length > 0 && ` + ${fmt(benefits.reduce((s, i) => s + i.amount, 0))} förmån`}
Obligatorisk inkomst {fmt(totalMandatory)}
{modal === 'add' && ( setModal(null)}> onAdd(monthId, data)} onClose={() => setModal(null)} /> )} {modal?.edit && ( setModal(null)}> onUpdate(modal.edit.id, data)} onClose={() => setModal(null)} /> )}
); }