//========================================================================================
// Registration

//Горячие клавиши 
const registForm = document.forms.registration;
console.log(registForm)

if(registForm) {
    registForm.addEventListener("submit", (event) => {
        event.preventDefault(); //Останови дефолтные функции браузера

        //Подключили значения с формы
        const formData = new FormData(registForm)
        const email = formData.get('email');
        const password = formData.get('password');
        const surname = formData.get('surname');
        const name = formData.get('name');
        const address = formData.get('address');
        const user = { name, surname, address, email, password };

        if(email === "" || password === "") return;

        registForm.reset();

        console.log(user);

        registUser(user).then(
            data => {
                console.log(data);
            }
        ).catch(ex => console.log(ex));
    })
}

async function registUser(user){
    const url = "http://todo/services/registration.php"

    const response = await fetch(url, {
        method: "POST",
        headers: {
            "Content-Type": "application/json; charset=utf-8"
        },
        body: JSON.stringify(user)
    });

    const info = await response.json();
    return info;
}









//========================================================================================
// Login



















// Modal =================================================================================
const modals = document.querySelectorAll('[data-name]')
const modalButtons = document.querySelectorAll('button[data-for]')

modalButtons.forEach(button => {
    const modalName = button.getAttribute('data-for')
    const modal = Array.from(modals).find(element => element.getAttribute('data-name') === modalName)

    if (!modal) return;

    const closeButton = modal.querySelector('button.task-form__actions_close')
    const confirmButton = modal.querySelector('button.task-form__actions_confirm')


    button.addEventListener('click', () => {
        modal.classList.add('active')
    })

    modal.addEventListener('click', (event) => {
        if (event.target === modal || event.target === closeButton) {
            modal.classList.remove('active')
        }
    })

    confirmButton?.addEventListener('click', sendFormHandler)
})


function sendFormHandler(event) {
}

function addCategory(form) {
}

async function createCategory(task) {
}

function addTask(form) {
}

async function createTask(task) {
}
//========================================================================================
// Menu links




//========================================================================================
// Change category to create a task




