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

type RestProps = {
    summaries: DealSummary[];
};
type Props = CRUDTableProps<ShippingWarehouseSchema> & RestProps;

const ShippingWarehouseTable: FC<Props> = ({
                                               onChange,
                                               onDelete,
                                               items,
                                               summaries
                                           }) => {
    const columns = useShippingWarehouseTableColumns();

    const onEditClick = (element: ShippingWarehouseSchema) => {
        if (!onChange) return;
        modals.openContextModal({
            modal: "shippingWarehouseForm",
            title: 'Редактирование склада отгрузки',
            withCloseButton: false,
            innerProps: {
                element,
                onChange,
                summaries: summaries.filter(s => s.shipmentWarehouseId === element.id).slice(0, 5)
            },
            size: "xl"
        })
    }
    const onDeleteClick = (element: ShippingWarehouseSchema) => {
        if (!onDelete) return;
        modals.openConfirmModal({
            title: 'Удаление склада отгрузки',
            children: (
                <Text size="sm">
                    Вы уверены что хотите удалить склад отгрузки {element.name}
                </Text>
            ),
            labels: {confirm: 'Да', cancel: "Нет"},
            confirmProps: {color: 'red'},
            onConfirm: () => {
                onDelete(element);
            }
        });
    }
    return (
        <BaseTable
            data={items}
            columns={columns}
            restProps={{
                enableColumnActions: false,
                enableSorting: false,
                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>
                    </Flex>
                )
            } as MRT_TableOptions<ShippingWarehouseSchema>}
        />
    )
}

export default ShippingWarehouseTable;