// reset_password.js
document.getElementById("forgotPasswordBtn").addEventListener("click", function(e) {
    e.preventDefault();
    document.getElementById("passwordRecoveryModal").style.display = "flex";
});

document.getElementById("cancelRecovery").addEventListener("click", function() {
    document.getElementById("passwordRecoveryModal").style.display = "none";
});

document.getElementById("submitRecovery").addEventListener("click", function() {
    const code = document.getElementById("recoveryCode").value.trim();
    const newPassword = document.getElementById("newPassword").value.trim();

    if (!code || !newPassword) {
        alert("Completa todos los campos.");
        return;
    }

    fetch('/api/reset_admin_password.php', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({ code: code, new_password: newPassword })
    })
    .then(res => res.json())
    .then(data => {
        if (data.success) {
            alert("Contraseña actualizada con éxito.");
            document.getElementById("passwordRecoveryModal").style.display = "none";
        } else {
            alert(data.message || "Error al actualizar.");
        }
    })
    .catch(err => {
        alert("Error de red.");
        console.error(err);
    });
});
