import { Pencil, Plus, X } from 'lucide-react' import type { InvoiceAccountTemplate, InvoiceItemTemplate } from '../types/invoice' import { formatIrtInput, formatIrtPrice } from '../utils/irtPrice' import type { DraftAccount, DraftKeyPoint, DraftLineItem, DraftPaymentMethod } from '../utils/invoiceDraft' import { draftAccountFromAccountTemplate, draftItemFromItemTemplate, emptyDraftAccount, emptyDraftItem, emptyDraftKeyPoint, emptyDraftPaymentMethod, } from '../utils/invoiceDraft' import tableStyles from '../pages/BusinessesPage.module.css' import styles from '../pages/BusinessInvoicesPage.module.css' function focusKeyPointInput(index: number) { const el = document.querySelector(`input[data-keypoint-index="${index}"]`) el?.focus() el?.select() } function focusPaymentMethodInput(index: number) { const el = document.querySelector(`input[data-payment-method-index="${index}"]`) el?.focus() el?.select() } type Props = { itemTemplates: InvoiceItemTemplate[] accountTemplates: InvoiceAccountTemplate[] items: DraftLineItem[] keyPoints: DraftKeyPoint[] paymentMethods: DraftPaymentMethod[] accounts: DraftAccount[] selectedItemTemplateId: string onSelectedItemTemplateId: (id: string) => void selectedAccountTemplateId: string onSelectedAccountTemplateId: (id: string) => void onItemsChange: (items: DraftLineItem[]) => void onKeyPointsChange: (points: DraftKeyPoint[]) => void onPaymentMethodsChange: (methods: DraftPaymentMethod[]) => void onAccountsChange: (accounts: DraftAccount[]) => void showItemTemplatePicker?: boolean showAccountTemplatePicker?: boolean } export function InvoiceDraftFields({ itemTemplates, accountTemplates, items, keyPoints, paymentMethods, accounts, selectedItemTemplateId, onSelectedItemTemplateId, selectedAccountTemplateId, onSelectedAccountTemplateId, onItemsChange, onKeyPointsChange, onPaymentMethodsChange, onAccountsChange, showItemTemplatePicker = true, showAccountTemplatePicker = true, }: Props) { function updateItem(key: string, patch: Partial) { onItemsChange(items.map((item) => (item.key === key ? { ...item, ...patch } : item))) } function removeItem(key: string) { if (items.length <= 1) return onItemsChange(items.filter((item) => item.key !== key)) } function addFromItemTemplate() { const template = itemTemplates.find((t) => t.id === selectedItemTemplateId) if (!template) return const onlyEmpty = items.length === 1 && !items[0].title.trim() && !items[0].price.trim() && !items[0].description.trim() onItemsChange(onlyEmpty ? [draftItemFromItemTemplate(template)] : [...items, draftItemFromItemTemplate(template)]) onSelectedItemTemplateId('') } function addFromAccountTemplate() { const template = accountTemplates.find((t) => t.id === selectedAccountTemplateId) if (!template) return onAccountsChange([...accounts, draftAccountFromAccountTemplate(template)]) onSelectedAccountTemplateId('') } return ( <>
{items.map((item, index) => (
Item {index + 1}
updateItem(item.key, { title: e.target.value })} placeholder="Service title" />
updateItem(item.key, { duration: e.target.value })} placeholder="e.g. 3 months" />
updateItem(item.key, { worktime: e.target.value })} placeholder="e.g. 40 hours" />
updateItem(item.key, { price: formatIrtInput(e.target.value) })} placeholder="0" />
updateItem(item.key, { discountedPrice: formatIrtInput(e.target.value) }) } placeholder="Optional" />