import {CRUDTableProps} from "../../../../types/CRUDTable.tsx";
import {BarcodeTemplateSchema} from "../../../../client";
import {FC} from "react";
import {useBarcodeTemplatesTableColumns} from "./columns.tsx";
import {BaseTable} from "../../../../components/BaseTable/BaseTable.tsx";
import {modals} from "@mantine/modals";
import {ActionIcon, Flex, Tooltip} from "@mantine/core";
import {IconEdit, IconTrash} from "@tabler/icons-react";

const BarcodeTemplatesTable: FC<CRUDTableProps<BarcodeTemplateSchema>> = ({
                                                                              items,
                                                                              onDelete,
                                                                              onChange
                                                                          }) => {
    const columns = useBarcodeTemplatesTableColumns();
    const onEditClick = (template: BarcodeTemplateSchema) => {
        if (!onChange) return;
        modals.openContextModal({
            modal: "barcodeTemplateFormModal",
            title: 'Создание шаблона',
            withCloseButton: false,
            innerProps: {
                onChange: (newTemplate) => onChange(newTemplate),
                element: template,
            }
        });
    }
    return (
        <>
            <BaseTable
                striped
                data={items}
                columns={columns}
                restProps={{
                    enableColumnActions: false,
                    enableRowActions: true,
                    renderRowActions: ({row}) => (
                        <Flex gap="md">
                            <Tooltip label="Редактировать">
                                <ActionIcon
                                    onClick={() => onEditClick(row.original)}
                                    variant={"default"}
                                >
                                    <IconEdit/>
                                </ActionIcon>
                            </Tooltip>
                            <Tooltip label="Удалить">
                                <ActionIcon onClick={() => {
                                    if (onDelete) onDelete(row.original);
                                }} variant={"default"}>
                                    <IconTrash/>
                                </ActionIcon>
                            </Tooltip>
                        </Flex>
                    )
                }}
            />
        </>
    )
}

export default BarcodeTemplatesTable;