const actual = document.getElementById('actual');
const nueva = document.getElementById('nueva');
const confirmar = document.getElementById('confirmar');
const frmPass = document.getElementById('frmPass');

const usuario = document.getElementById('usuario');
const nombre = document.getElementById('nombre');
const correo = document.getElementById('correo');
const frmDatos = document.getElementById('frmDatos');
const btnProfile = document.getElementById('btnProfile');
document.addEventListener('DOMContentLoaded', function () {
    //credenciales
    var forms1 = document.getElementsByClassName('needs-validation');
    // Loop over them and prevent submission
    var validation = Array.prototype.filter.call(forms1, function (frmPass) {
        frmPass.addEventListener('submit', function (event) {
            event.preventDefault();
            if (frmPass.checkValidity() === false) {
                event.stopPropagation();
                frmPass.classList.add('was-validated');
            } else {
                if (nueva.value != confirmar.value) {
                    alertas('Las contraseñas no coinciden', 'warning');
                } else {
                    const url = base_url + 'usuarios/cambiar_pass';
                    const http = new XMLHttpRequest();
                    http.open("POST", url, true);
                    http.send(new FormData(this));
                    http.onreadystatechange = function () {
                        if (this.readyState == 4 && this.status == 200) {
                            const res = JSON.parse(this.responseText);
                            alertas(res.msg, res.icono);
                            if (res.icono == 'success') {
                                setTimeout(() => {
                                    window.location = base_url + 'usuarios/salir';
                                }, 2000);
                            }
                        }
                    }
                }
            }

        }, false);
    });

    //credenciales
    var forms2 = document.getElementsByClassName('needs-validation');
    // Loop over them and prevent submission
    var validation1 = Array.prototype.filter.call(forms2, function (frmDatos) {
        btnProfile.addEventListener('click', function () {
            if (frmDatos.checkValidity() === false) {
                frmDatos.classList.add('was-validated');
            } else {
                const url = base_url + 'usuarios/cambiar_datos';
                    const http = new XMLHttpRequest();
                    http.open("POST", url, true);
                    http.send(new FormData(frmDatos));
                    http.onreadystatechange = function () {
                        if (this.readyState == 4 && this.status == 200) {
                            const res = JSON.parse(this.responseText);
                            console.log(res);
                            alertas(res.msg, res.icono);
                            if (res.icono == 'success') {
                                setTimeout(() => {
                                    window.location.reload();
                                }, 2000);
                            }
                        }
                    }
            }

        }, false);
    });

})