import {postJson} from "../utils/Request.js";
import {loading, notify} from "../utils/Notiflix.js";

const auth = () => {
    formData()
}

const formData = () => {
    const form = document.getElementById("auth_form");
    const token=document.querySelector("meta[name='csrf-token']").content

    let data = {token}
    form.addEventListener("submit", (ev) => {
        loading(true);
        [...ev.target.elements].forEach(element => {
            ev.preventDefault();
            if (element.type !== "submit" && element.type !== "checkbox") {
                data[element.name] = element.value
            }
            if (element.type === "checkbox") {
                data[element.name] = element.checked
            }
        })
        postJson("admin/login", data).then(result => {
            loading(false)
            const conclusion=result.conclusion
            const messages=result.message
            const url=result.url
            const timeout=result.timeout
            notify(conclusion,messages,url,timeout)
        })
    })

}

export default auth