import styles from "../../ServicesPage/ui/ServicesPage.module.css";
import PageBlock from "../../../components/PageBlock/PageBlock.tsx";
import ShippingWarehousesTable from "../components/ShippingWarehousesTable/ShippingWarehousesTable.tsx";
import useShippingWarehousesList
    from "../../../components/Selects/ShippingWarehouseAutocomplete/hooks/useShippingWarehousesList.tsx";
import {Button} from "@mantine/core";
import {modals} from "@mantine/modals";
import {useCRUD} from "../../../hooks/useCRUD.tsx";
import {ShippingWarehouseSchema, ShippingWarehouseService} from "../../../client";
import {notifications} from "../../../shared/lib/notifications.ts";
import {useDealSummariesFull} from "../../LeadsPage/hooks/useDealSummaries.tsx";

export const ShippingWarehousesPage = () => {
    const {shippingWarehouses, refetch} = useShippingWarehousesList();
    const {objects: summaries} = useDealSummariesFull();
    const crud = useCRUD<ShippingWarehouseSchema>({
        onChange: (element) => {
            ShippingWarehouseService
                .updateShippingWarehouse({
                    requestBody: {
                        shippingWarehouse: element
                    }
                })
                .then(async ({ok, message}) => {
                    notifications.guess(ok, {message});
                    if (!ok) return;
                    await refetch();
                })
        },
        onCreate: (element) => {
            ShippingWarehouseService
                .createShippingWarehouse({
                    requestBody: {
                        shippingWarehouse: element
                    }
                })
                .then(async ({ok, message}) => {
                    notifications.guess(ok, {message});
                    if (!ok) return;
                    await refetch();
                })
        },
        onDelete: (element) => {
            ShippingWarehouseService
                .deleteShippingWarehouse({
                    requestBody: {
                        shippingWarehouseId: element.id
                    }
                })
                .then(async ({ok, message}) => {
                    notifications.guess(ok, {message});
                    if (!ok) return;
                    await refetch();
                })
        }
    });
    const onCreateClick = () => {
        modals.openContextModal({
            modal: "shippingWarehouseForm",
            title: 'Редактирование склада отгрузки',
            withCloseButton: false,
            innerProps: {
                onCreate: crud.onCreate,
                summaries: []
            },
        })
    }
    return (
            <div className={styles['container']}>
                <PageBlock>
                    <div className={styles['top-panel']}>
                        <Button
                            onClick={onCreateClick}
                            variant={"default"}
                        >
                            Создать склад отгрузки
                        </Button>
                    </div>
                </PageBlock>
                <PageBlock>
                    <ShippingWarehousesTable
                        items={shippingWarehouses}
                        summaries={summaries}
                        {...crud}
                    />
                </PageBlock>
            </div>

    )
}

