"use client" import { useFormContext } from "react-hook-form" import { Card, CardContent, CardHeader, CardTitle } from "@/shared/components/ui/card" import { useDocumentTotals, type DocumentLineItem, } from "@/shared/hooks/use-document-totals" import { DocumentTotalsSummary } from "@/shared/components/document-totals-summary" import type { BillFormValues } from "./bill.schema" function parseTaxRateFromLabel(label: string | undefined | null): number | undefined { if (!label) return undefined const match = label.match(/\((\d+(?:\.\d+)?)%\)/) return match ? Number(match[1]) : undefined } export function BillFormSummary() { const { watch } = useFormContext() const partItems = watch("part_items") ?? [] const serviceItems = watch("service_items") ?? [] const expenseItems = watch("expense_items") ?? [] const discountType = watch("discount") const discountAmount = watch("discount_amount") const taxRelation = watch("tax") const taxRate = parseTaxRateFromLabel(taxRelation?.label) const taxLabel = taxRelation?.label ?? "Tax" const lineItems: DocumentLineItem[] = [ ...partItems.map((p) => ({ quantity: p.quantity, rate: p.rate, discount_amount: p.discount_amount, })), ...serviceItems.map((s) => ({ quantity: s.quantity, rate: s.rate, discount_amount: s.discount_amount, })), ...expenseItems.map((e) => ({ quantity: e.quantity, rate: e.rate, discount_amount: e.discount_amount, })), ] const groupLabels: Record = {} if (partItems.length > 0) { groupLabels[`Parts (${partItems.length})`] = partItems.reduce( (s, p) => s + (Number(p.quantity) || 0) * (Number(p.rate) || 0), 0, ) } if (serviceItems.length > 0) { groupLabels[`Services (${serviceItems.length})`] = serviceItems.reduce( (s, sv) => s + (Number(sv.quantity) || 0) * (Number(sv.rate) || 0), 0, ) } if (expenseItems.length > 0) { groupLabels[`Expenses (${expenseItems.length})`] = expenseItems.reduce( (s, e) => s + (Number(e.quantity) || 0) * (Number(e.rate) || 0), 0, ) } const totals = useDocumentTotals({ lineItems, discountType, discountAmount, taxRate, }) if (!totals.hasLineItems) return null return ( Summary 1 ? groupLabels : undefined} /> ) }