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


const ClientsTable: FC<CRUDTableProps<ClientSchema>> = ({
                                                            items,
                                                            onDelete,
                                                            onChange
                                                        }) => {
    const columns = useClientsTableColumns();
    const onEditClick = (client: ClientSchema) => {
        if (!onChange) return;
        modals.openContextModal({
            modal: "productFormModal",
            title: 'Создание клиента',
            withCloseButton: false,
            innerProps: {
                onChange: (newClient) => onChange(newClient),
                element: client,
            }
        })
    }
    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>
                    )
                } as MRT_TableOptions<ClientSchema>}
            />
        </>

    )
}

export default ClientsTable;