import { FC, useEffect } from "react";
import { Button, Drawer, Flex, rem, TextInput } from "@mantine/core";
import DealsTable from "./components/tables/DealsTable/DealsTable.tsx";
import Preview from "./components/Preview/Preview.tsx";
import styles from "./DealPrefillDrawer.module.css";
import BaseMarketplaceSelect from "../../../../components/Selects/BaseMarketplaceSelect/BaseMarketplaceSelect.tsx";
import usePrefillDeal from "./hooks/usePrefillDeal.tsx";
import { notifications } from "../../../../shared/lib/notifications.ts";
import { usePrefillDealContext } from "../../contexts/PrefillDealContext.tsx";

const DealPrefillDrawer: FC = () => {
    const { prefillOpened, prefillOnClose, selectedPrefillDeal, setPrefillDeal, prefillDeal } = usePrefillDealContext();
    const { data, form } = usePrefillDeal();

    useEffect(() => {
        if (prefillOpened) return;
    }, [prefillOpened]);

    return (
        <Drawer
            size={"calc(77vw)"}
            position={"right"}
            onClose={prefillOnClose}
            removeScrollProps={{ allowPinchZoom: true }}
            withCloseButton={false}
            opened={prefillOpened}
            styles={{
                body: {
                    height: "100%",
                    display: "flex",
                    flexDirection: "row",
                    gap: rem(20),
                },
            }}
        >
            <div className={styles["deal-container"]}>
                <div className={styles["deal-container-wrapper"]}>
                    <div className={styles["top-panel"]}>
                        <TextInput
                            placeholder={"Введите название / id"}
                            {...form.getInputProps("idOrName")}
                        />
                        <BaseMarketplaceSelect
                            onClear={() =>
                                form.setFieldValue("marketplace", null)
                            }
                            clearable
                            placeholder={"Выберите маркетплейс"}
                            {...form.getInputProps("marketplace")}
                        />
                    </div>
                    <DealsTable items={data} />
                    <Flex direction={"row"} gap="sm">
                        <Button mt={10} w={"100%"} onClick={() => {
                            if (!selectedPrefillDeal) {
                                notifications.error({ message: "Сделка не выбрана." });
                                return;
                            }
                            setPrefillDeal(selectedPrefillDeal);
                            prefillOnClose();
                        }}>
                            Предзаполнить
                        </Button>
                        {
                            prefillDeal &&
                            <Button mt={10} w={"100%"} variant={"outline"} onClick={() => {
                                setPrefillDeal(undefined);
                                notifications.success({ message: "Предзаполнение отменено." });
                                prefillOnClose();
                            }}>
                                Отменить предзаполнение
                            </Button>
                        }
                    </Flex>
                </div>
            </div>
            <Preview />
        </Drawer>
    );
};

export default DealPrefillDrawer;
