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

type Props = CRUDTableProps<GetServiceKitSchema>;
const ServicesKitsTable: FC<Props> = ({items, onDelete, onChange}) => {
    const columns = useServicesKitsTableColumns();
    const onEditClick = (kit: GetServiceKitSchema) => {
        if (!onChange) return;
        modals.openContextModal({
            modal: 'serviceKitModalForm',
            title: 'Создание набора услуг',
            withCloseButton: false,
            innerProps: {
                element: kit,
                onChange
            }
        })
    }
    const onDeleteClick = () => {
        if (!onDelete) return;
    }
    return (
        <BaseTable
            data={items}
            columns={columns}
            restProps={{
                enableSorting: false,
                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) onDeleteClick();
                            }} variant={"default"}>
                                <IconTrash/>
                            </ActionIcon>
                        </Tooltip>
                    </Flex>
                )
            } as MRT_TableOptions<GetServiceKitSchema>}
        />
    )
}

export default ServicesKitsTable;