"use client";

import {
    Button,
    Flex,
    NumberInput,
    rem,
    Space,
    Text,
    TextInput,
} from "@mantine/core";
import { useForm } from "@mantine/form";
import { ContextModalProps } from "@mantine/modals";
import { DealsFiltersForm } from "@/app/deals/hooks/useDealsFilters";
import { ProjectSchema } from "@/lib/client";

type Props = {
    value: DealsFiltersForm;
    onChange: (values: DealsFiltersForm) => void;
    project: ProjectSchema | null;
};

const DealsBoardFiltersModal = ({
    id,
    context,
    innerProps,
}: ContextModalProps<Props>) => {
    const filtersForm = useForm({
        initialValues: innerProps.value,
    });

    const onSubmit = (values: DealsFiltersForm) => {
        innerProps.onChange(values);
        context.closeModal(id);
    };

    return (
        <form onSubmit={filtersForm.onSubmit(onSubmit)}>
            <Flex
                gap={rem(10)}
                direction={"column"}>
                <NumberInput
                    label={"ID"}
                    placeholder={"Введите ID"}
                    {...filtersForm.getInputProps("id")}
                    value={filtersForm.values.id ?? ""}
                    onChange={value =>
                        typeof value === "number"
                            ? filtersForm.setFieldValue("id", Number(value))
                            : filtersForm.setFieldValue("id", null)
                    }
                    hideControls
                    min={1}
                />
                <TextInput
                    label={"Название"}
                    placeholder={"Введите название"}
                    {...filtersForm.getInputProps("name")}
                />
                <Space />
                <Button
                    variant={"default"}
                    type={"submit"}>
                    <Text>Сохранить</Text>
                </Button>
            </Flex>
        </form>
    );
};

export default DealsBoardFiltersModal;
