import {ContextModalProps} from "@mantine/modals";
import BaseFormModal, {CreateEditFormProps} from "../../ClientsPage/modals/BaseFormModal/BaseFormModal.tsx";
import {DealProductSchema} from "../../../client";
import {useForm} from "@mantine/form";
import {NumberInput} from "@mantine/core";
import ProductSelect from "../../../components/ProductSelect/ProductSelect.tsx";

type RestProps = {
    clientId: number
}

type Props = CreateEditFormProps<Partial<DealProductSchema>> & RestProps;
const AddDealProductModal = ({
                                 context,
                                 id,
                                 innerProps
                             }: ContextModalProps<Props>) => {
    const form = useForm<Partial<DealProductSchema>>({
        initialValues: {
            product: undefined,
            quantity: 0
        },
        validate: {
            product: (product?: DealProductSchema['product']) => product !== undefined ? null : "Необходимо выбрать товар",
            quantity: (quantity?: number) => (quantity && quantity > 0) ? null : "Количество должно быть больше 0"
        }
    });
    const onClose = () => {
        context.closeContextModal(id);
    }


    return (
        <BaseFormModal
            {...innerProps}
            form={form}
            closeOnSubmit
            onClose={onClose}>
            <BaseFormModal.Body>
                <>
                    <ProductSelect
                        placeholder={"Выберите услугу"}
                        label={"Услуга"}
                        clientId={innerProps.clientId}
                        {...form.getInputProps('service')}
                    />
                    <NumberInput
                        placeholder={"Введите количество"}
                        label={"Количество"}
                        min={1}
                        {...form.getInputProps('quantity')}
                    />
                </>

            </BaseFormModal.Body>
        </BaseFormModal>
    )
}

export default AddDealProductModal;