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

const registrationForm = document.forms.registration;
if (registrationForm) {
    registrationForm.addEventListener("submit", (event) => {
        event.preventDefault(); //остановка поведения браузера по дефолту
        //Создаем FormData, передаем в него элемент формы
        const formData = new FormData(registrationForm);
        const name = formData.get("name");
        const surname = formData.get("surname");
        const address = formData.get("address");
        const email = formData.get("email");
        const password = formData.get("password");
        const user = { name, surname, address, email, password };
        //Не имеет значение "" или ''
        if (email === "" || password === '')
            return
        registrationForm.reset()
        registrationAddUser(user).then(
            data => {
                console.log(data);
            }).catch(ex => console.log(ex))
    })

    async function registrationAddUser(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) //преобразует JavaScript в строку JSON
        });
        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




