import useShippingTableColumns from "../hooks/shippingTableColumns.tsx";
import { BaseTable } from "../../../../../components/BaseTable/BaseTable.tsx";
import { ShippingProductSchema, ShippingService } from "../../../../../client";
import { ActionIcon, Flex, Tooltip } from "@mantine/core";
import { IconEdit, IconTrash } from "@tabler/icons-react";
import { MRT_TableOptions } from "mantine-react-table";
import { notifications } from "../../../../../shared/lib/notifications.ts";
import useUpdateDeal from "../hooks/useUpdateDeal.tsx";
import { modals } from "@mantine/modals";
import { useDealPageContext } from "../../../contexts/DealPageContext.tsx";


type Props = {
    items: ShippingProductSchema[];
}

const ShippingProductsTable = ({ items }: Props) => {
    const columns = useShippingTableColumns<ShippingProductSchema>({ isBox: false });
    const { update } = useUpdateDeal();
    const { selectedDeal: deal } = useDealPageContext();

    const onDeleteClick = (shippingProduct: ShippingProductSchema) => {
        ShippingService.deleteShippingProduct({
            shippingProductId: shippingProduct.id,
        })
            .then(({ ok, message }) => {
                notifications.guess(ok, { message });
                update();
            })
            .catch(err => console.log(err));
    };

    const onEditClick = (shippingProduct: ShippingProductSchema) => {
        if (!deal) return;
        modals.openContextModal({
            modal: "shippingProductModal",
            title: "Редактирование товара на паллете",
            withCloseButton: false,
            innerProps: {
                deal,
                updateOnSubmit: update,
                isBox: false,
                shippingData: {
                    shippingProductId: shippingProduct.id,
                    productId: shippingProduct.product.id,
                    quantity: shippingProduct.quantity,
                },
            },
        });
    };

    return (
        <BaseTable
            data={items}
            columns={columns}
            restProps={
                {
                    enableSorting: false,
                    enableColumnActions: false,
                    enableRowActions: true,
                    enableRowNumbers: true,
                    positionActionsColumn: "last",
                    renderRowActions: ({ row }) => (
                        <Flex gap="md">
                            <Tooltip label="Удалить">
                                <ActionIcon
                                    onClick={() => onDeleteClick(row.original)}
                                    variant={"default"}>
                                    <IconTrash />
                                </ActionIcon>
                            </Tooltip>
                            <Tooltip label="Редактировать">
                                <ActionIcon
                                    onClick={() => onEditClick(row.original)}
                                    variant={"default"}>
                                    <IconEdit />
                                </ActionIcon>
                            </Tooltip>
                        </Flex>
                    ),
                } as MRT_TableOptions<ShippingProductSchema>
            }
        />
    );
};

export default ShippingProductsTable;
