import {useState} from 'react'
import {Link} from 'react-router-dom'
import './InputForm.css';
import {registerUser} from '../services/PhpApi';
import { PasswordInput, Stack, TextInput } from '@mantine/core';
import { notificationAlert, notificationError, notificationSuccess } from '../services/Notifications';

const Register = () => 
{
    const [formData, setFormData] = useState({
        email:'',
        password:'',
        password2:''
    });

    const onChangeInput = (e: React.FormEvent<HTMLInputElement> ) => {
        setFormData({
            ...formData,
            [e.currentTarget.name]:e.currentTarget.value
        })
    }

    const submitForm = async (e: React.FormEvent<HTMLFormElement>) => 
    {
        e.preventDefault();

        if(!Object.values(formData).every(val => val.trim() !== ''))
        {
            notificationAlert('Bitte alle Felder ausfüllen!');

            return;
        }

        if(formData.password !== formData.password2)
        {
            notificationAlert('Bitte 2mal das gleiche Passwort eingeben!');
            return;
        }

        const data = await registerUser(formData);
        if(data.success)
        {
            notificationSuccess('Erfolgreich Registriert!');
            e.currentTarget.reset();
        }
        else if(!data.success && data.message)
        {
            notificationError(data.message);
        }
    }

    return ( 
        <div className='InputForm'>
            <h2>Register</h2>
            <div className='frameCenter'>
            <div className='frame'>
            <form onSubmit={submitForm}>
            <Stack align="flex-start" sx={{textAlign: 'left'}}>
                <TextInput
                    label = 'Email:'
                    type="email" 
                    name="email" 
                    onChange={onChangeInput} 
                    placeholder="Deine Email" 
                    id="email" 
                    value={formData.email} 
                    required />
                <PasswordInput className='IF_pw'
                    sx = {{  maxWidth: '80vw', minWidth: '30vw', width: '300px' }}
                    label = 'Passwort:'
                    name="password" 
                    onChange={onChangeInput} 
                    placeholder="Passwort" 
                    id="password" value={formData.password} 
                    required/>
                <PasswordInput className='IF_pw'
                    sx = {{  maxWidth: '80vw', minWidth: '30vw', width: '300px' }}
                    label = 'Passwort:'
                    name="password2" 
                    onChange={onChangeInput} 
                    placeholder="Passwort bestätigen" 
                    id="password2" value={formData.password2} 
                    required/>
                </Stack>
                <button type="submit" >Register</button>
                <div className="bottom-link"><Link to="/login">Login</Link></div>
            </form>
            </div>
            </div>
        </div>
    )
}
export default Register;