import { Checkbox, NumberInput, TextInput } from "@mantine/core";
import { UseFormReturnType } from "@mantine/form";
import { DatePickerInput, DateTimePicker } from "@mantine/dates";
import { AttributeSchema } from "../../../../../client";

type Props = {
    form: UseFormReturnType<Partial<AttributeSchema>>;
}

const DefaultAttributeValueInput = ({ form }: Props) => {
    const type = form.values.type?.type;
    const label = "Значение по умолчанию";
    const inputName = "defaultValue";

    if (type === "bool") {
        return (
            <Checkbox
                label={label}
                {...form.getInputProps(inputName, { type: "checkbox" })}
            />
        );
    } else if (type === "date") {
        return (
            <DatePickerInput
                label={label}
                {...form.getInputProps(inputName)}
                value={form.values.defaultValue ? new Date(String(form.values.defaultValue)) : null}
                clearable
                locale={"ru-RU"}
                valueFormat="DD.MM.YYYY"
            />
        );
    } else if (type === "datetime") {
        return (
            <DateTimePicker
                label={label}
                {...form.getInputProps(inputName)}
                value={form.values.defaultValue ? new Date(String(form.values.defaultValue)) : null}
                clearable
                locale={"ru-RU"}
                valueFormat="DD.MM.YYYY HH:mm"
            />
        );
    } else if (type === "str") {
        return (
            <TextInput
                label={label}
                {...form.getInputProps(inputName)}
                value={form.getInputProps(inputName).value ?? ""}
            />
        );
    } else if (type === "int" || type === "float") {
        return (
            <NumberInput
                allowDecimal={type === "float"}
                label={label}
                {...form.getInputProps(inputName)}
                value={form.values.defaultValue ? Number(form.values.defaultValue) : undefined}
            />
        )
    }

    return <></>;
};

export default DefaultAttributeValueInput;
