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

type Props = CRUDTableProps<PositionSchema>;

const PositionsTable: FC<Props> = ({items, onCreate, onDelete}) => {
    const columns = usePositionsTableColumns();

    const onCreateClick = () => {
        if (!onCreate) return;
        modals.openContextModal({
            modal: "positionForm",
            withCloseButton: false,
            innerProps: {
                onCreate: onCreate
            }
        })
    }

    const onDeleteClick = (position: PositionSchema) => {
        if (!onDelete) return;
        modals.openConfirmModal({
            title: 'Удаление должности',
            children: (
                <Text size="sm">
                    Вы уверены что хотите удалить должность {position.name}
                </Text>
            ),
            labels: {confirm: 'Да', cancel: "Нет"},
            confirmProps: {color: 'red'},
            onConfirm: () => onDelete(position)
        });
    }

    return (

        <BaseTable
            data={items}
            columns={columns}
            restProps={{
                enableTopToolbar: true,
                enableSorting: false,
                enableColumnActions: false,
                enableRowActions: true,
                renderTopToolbar: (
                    <Flex p={rem(10)}>

                        <Button
                            variant={"default"}
                            onClick={() => onCreateClick()}
                        >
                            Создать должность
                        </Button>
                    </Flex>

                ),
                renderRowActions: ({row}) => (
                    <Flex gap="md">

                        <Tooltip label="Удалить">
                            <ActionIcon onClick={() => onDeleteClick(row.original)} variant={"default"}>
                                <IconTrash/>
                            </ActionIcon>
                        </Tooltip>
                    </Flex>
                )
            } as MRT_TableOptions<PositionSchema>}

        />

    )
}

export default PositionsTable;