import { FormControl, FormField, FormItem, FormMessage, Input } from "@repo/shadcn-ui/components"; import { TextAreaField } from "@repo/rdx-ui/components"; import { ColumnDef } from "@tanstack/react-table"; import { ChevronDownIcon, ChevronUpIcon, CopyIcon, Trash2Icon } from "lucide-react"; import { useState } from "react"; import { useFieldArray, useFormContext } from "react-hook-form"; import { useDetailColumns } from "../../hooks"; import { useTranslation } from "../../i18n"; import { formatCurrency } from "../../pages/create/utils"; import { CustomerInvoiceTaxesMultiSelect } from "../customer-invoice-taxes-multi-select"; import { CustomerInvoiceItemsSortableDataTable, RowIdData, } from "./customer-invoice-items-sortable-datatable"; export const CustomerInvoiceItemsCardEditor = ({ //currency, //language, defaultValues, }: { //currency: CurrencyData; //language: Language; defaultValues: Readonly<{ [x: string]: any }> | undefined; }) => { const { t } = useTranslation(); const { control, watch, getValues } = useFormContext(); const watchedItems = watch("items"); //const [pickerMode] = useState<"dialog" | "panel">("dialog"); //const [articlePickerDialogOpen, setArticlePickerDialogOpen] = useState(false); //const [blockPickerDialogOpen, setBlockPickerDialogOpen] = useState(false); const { fields, ...fieldActions } = useFieldArray({ control, name: "items", }); const columns: ColumnDef[] = useDetailColumns( [ /*{ id: "row_id" as const, header: () => ( ), accessorFn: (_: unknown, index: number) => index + 1, size: 5, enableHiding: false, enableSorting: false, enableResizing: false, },*/ /*{ id: "id_article" as const, accessorKey: "id_article", header: "artículo", cell: ({ row: { index, original } }) => { return ( ); }, size: 500, },*/ { id: "description" as const, accessorKey: "description", header: t("form_fields.items.description.label"), cell: ({ row: { index, original } }) => ( ), minSize: 200, size: 400, }, { id: "quantity" as const, accessorKey: "quantity", header: () =>
{t("form_fields.items.quantity.label")}
, cell: ({ row: { index } }) => ( ( field.onChange(Number(e.target.value) * 100)} value={field.value / 100} /> )} /> ), size: 75, }, { id: "unit_price" as const, accessorKey: "unit_price", header: () =>
{t("form_fields.items.unit_price.label")}
, cell: ({ row: { index } }) => ( ( field.onChange(Number(e.target.value) * 100)} value={field.value / 100} /> )} /> ), size: 125, }, { id: "subtotal_price" as const, accessorKey: "subtotal_price", header: () => (
{t("form_fields.items.subtotal_price.label")}
), cell: ({ row: { index } }) => { /*return ( );*/ return null; }, size: 150, }, { id: "discount" as const, accessorKey: "discount", header: () =>
{t("form_fields.items.discount.label")}
, cell: ({ row: { index } }) => ( ( field.onChange(Number(e.target.value) * 100)} value={field.value / 100} /> )} /> ), size: 100, }, { id: "taxes" as const, accessorKey: "taxes", header: () =>
{t("form_fields.items.taxes.label")}
, cell: ({ row: { index } }) => ( ( field.onChange(Number(e.target.value) * 100)} //value={field.value / 100} /> )} /> ), size: 150, }, { id: "total_price" as const, accessorKey: "total_price", header: () =>
{t("form_fields.items.total_price.label")}
, cell: ({ row: { index } }) => ( <> {formatCurrency( watchedItems[index]?.total_price?.amount || 0, 2, getValues("currency") )} ), size: 150, }, ], { enableDragHandleColumn: true, enableSelectionColumn: true, enableActionsColumn: true, rowActionFn: (props) => { const { table, row } = props; return [ { label: t("common.duplicate_row"), icon: , onClick: () => table.options.meta?.duplicateItems(row.index), }, { label: t("common.insert_row_above"), icon: , onClick: () => table.options.meta?.insertItem(row.index), }, { label: t("common.insert_row_below"), icon: , onClick: () => table.options.meta?.insertItem(row.index + 1), }, { label: "-", }, { label: t("common.remove_row"), //shortcut: "⌘⌫", icon: , onClick: () => { table.options.meta?.deleteItems(row.index); }, }, ]; }, } ); const [isCollapsed, setIsCollapsed] = useState(false); const defaultLayout = [265, 440, 655]; const navCollapsedSize = 4; return (
setArticlePickerDialogOpen(true), //pickBlock: () => setBlockPickerDialogOpen(true), }} columns={columns} data={fields} defaultValues={defaultValues} />
); };