import {useDispatch, useSelector} from "react-redux";
import {closeModal} from "../../../../../Redux/Slices/adminSlice.js";
import {Form, notification} from "antd";

const useCreateUserModalFormUI = (registerUser) => {
    const dispatch = useDispatch();
    const [form] = Form.useForm();

    const {
        modalVisible,
    } = useSelector(state => state.adminUI);

    const handleCancel = () => {
        dispatch(closeModal());
    };

    const handleFinish = async () => {
        const values = form.getFieldsValue();

        try {
            await registerUser(values).unwrap();
            notification.success({
                message: "Пользователь зарегистрирован",
                description: "Пользователь успешно зарегистрирован",
                placement: "topRight",
            });
            form.resetFields();
            handleCancel();
        } catch (error) {
            notification.error({
                message: "Ошибка регистрации пользователя",
                description: error?.data?.detail || "Не удалось зарегистрировать пользователя",
                placement: "topRight",
            });
        }
    };

    return {
        form,
        modalVisible,
        handleCancel,
        handleFinish,
    };
};

export default useCreateUserModalFormUI;