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

type RestProps = {
    client?: ClientSchema;
    onSynchronize?: (marketplace: MarketplaceSchema) => void;

}
type Props = CRUDTableProps<MarketplaceSchema> & RestProps;

const MarketplacesTable: FC<Props> = ({onDelete, onChange, onCreate, items, client, onSynchronize}) => {
    const columns = useMarketplacesTableColumns();
    const onDeleteClick = (marketplace: MarketplaceSchema) => {
        if (!onDelete) return;
        modals.openConfirmModal({
            title: 'Удаление маркетплейса',
            children: (
                <Text size="sm">
                    Вы уверены что хотите удалить маркетплейс {marketplace.name}
                </Text>
            ),
            labels: {confirm: 'Да', cancel: "Нет"},
            confirmProps: {color: 'red'},
            onConfirm: () => onDelete(marketplace)
        });
    }
    const onEditClick = (marketplace: MarketplaceSchema) => {
        if (!onChange) return;
        modals.openContextModal({
            modal: "marketplaceFormModal",
            withCloseButton: false,
            innerProps: {
                onChange: (event) => onChange(event),
                element: marketplace
            }
        })
    }
    const onCreateClick = () => {
        if (!onCreate || !client) return;
        modals.openContextModal({
            modal: "marketplaceFormModal",
            withCloseButton: false,
            innerProps: {
                onCreate: (event) => onCreate({...event, client: client})
            }
        })
    }
    return (
        <BaseTable
            data={items}
            columns={columns}
            restProps={{
                enableSorting: false,
                enableColumnActions: false,
                enableTopToolbar: true,
                renderTopToolbar: (
                    <Flex p={rem(10)} justify="end">
                        <Button
                            disabled={!client}
                            variant={"default"}
                            onClick={() => onCreateClick()}>
                            Добавить
                        </Button>
                    </Flex>
                ),
                enableRowActions: true,
                renderRowActions: ({row}) => (
                    <Flex gap="md">
                        <Tooltip label="Редактировать">
                            <ActionIcon
                                onClick={() => onEditClick(row.original)}
                                variant={"default"}>
                                <IconEdit/>
                            </ActionIcon>
                        </Tooltip>
                        <Tooltip label="Удалить">
                            <ActionIcon onClick={() => onDeleteClick(row.original)} variant={"default"}>
                                <IconTrash/>
                            </ActionIcon>
                        </Tooltip>
                        <Tooltip label="Синхронизировать">
                            <ActionIcon onClick={() => onSynchronize && onSynchronize(row.original)}
                                        variant={"default"}>
                                <IconRefresh/>
                            </ActionIcon>
                        </Tooltip>
                    </Flex>
                )
            } as MRT_TableOptions<MarketplaceSchema>}
        />
    )
}
export default MarketplacesTable;