// Configurazione API
const API_BASE_URL = '/api';
// Elementi DOM
const registerForm = document.getElementById('registerForm');
const emailInput = document.getElementById('email');
const verificationStep = document.getElementById('verificationStep');
const verificationCodeInput = document.getElementById('verificationCode');
const verifyButton = document.getElementById('verifyButton');
const resendButton = document.getElementById('resendButton');
const countdown = document.getElementById('countdown');
let countdownTimer;
let resendCooldown = 60;
// Gestione invio form registrazione
registerForm.addEventListener('submit', async function(e) {
e.preventDefault();
const email = emailInput.value.trim();
if (!email) {
showMessage('Inserisci un indirizzo email valido', 'error');
return;
}
const submitButton = registerForm.querySelector('button[type="submit"]');
const originalText = submitButton.textContent;
submitButton.disabled = true;
submitButton.textContent = 'Invio in corso...';
try {
const response = await fetch(`${API_BASE_URL}/verify-email.php`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
action: 'send_verification',
email: email
})
});
const result = await response.json();
if (result.success) {
showVerificationStep(email);
showMessage('Email di verifica inviata! Controlla la tua casella di posta.', 'success');
} else {
showMessage(result.message || 'Errore nell\'invio dell\'email di verifica', 'error');
}
} catch (error) {
console.error('Errore API:', error);
showMessage('Errore di connessione. Riprova più tardi.', 'error');
} finally {
submitButton.disabled = false;
submitButton.textContent = originalText;
}
});
// Gestione verifica codice OTP
verifyButton.addEventListener('click', async function() {
const code = verificationCodeInput.value.trim();
const email = emailInput.value.trim();
if (!code || code.length !== 6) {
showMessage('Inserisci un codice di verifica valido (6 cifre)', 'error');
return;
}
verifyButton.disabled = true;
verifyButton.textContent = 'Verifica in corso...';
try {
const response = await fetch(`${API_BASE_URL}/verify-email.php`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
action: 'verify_code',
email: email,
code: code
})
});
const result = await response.json();
if (result.success) {
showMessage('Email verificata con successo! Registrazione completata.', 'success');
localStorage.setItem('emailVerified', 'true');
localStorage.setItem('verifiedEmail', email);
setTimeout(() => {
window.location.href = 'login.html';
}, 2000);
} else {
showMessage(result.message || 'Codice di verifica non valido', 'error');
}
} catch (error) {
console.error('Errore verifica:', error);
showMessage('Errore di connessione. Riprova più tardi.', 'error');
} finally {
verifyButton.disabled = false;
verifyButton.textContent = 'Verifica Email';
}
});
// Gestione reinvio email
resendButton.addEventListener('click', async function() {
const email = emailInput.value.trim();
resendButton.disabled = true;
resendButton.textContent = 'Invio in corso...';
try {
const response = await fetch(`${API_BASE_URL}/verify-email.php`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
action: 'send_verification',
email: email
})
});
const result = await response.json();
if (result.success) {
showMessage('Email di verifica inviata nuovamente!', 'success');
startResendCooldown();
} else {
showMessage(result.message || 'Errore nel reinvio dell\'email', 'error');
}
} catch (error) {
console.error('Errore reinvio:', error);
showMessage('Errore di connessione. Riprova più tardi.', 'error');
} finally {
resendButton.disabled = false;
resendButton.textContent = 'Reinvia Email';
}
});
function showVerificationStep(email) {
registerForm.style.display = 'none';
verificationStep.style.display = 'block';
document.getElementById('emailDisplay').textContent = email;
startResendCooldown();
}
function startResendCooldown() {
resendCooldown = 60;
resendButton.disabled = true;
countdownTimer = setInterval(() => {
resendCooldown--;
countdown.textContent = `(${resendCooldown}s)`;
if (resendCooldown <= 0) {
clearInterval(countdownTimer);
resendButton.disabled = false;
countdown.textContent = '';
}
}, 1000);
}
function showMessage(message, type = 'info') {
const existingMessage = document.querySelector('.message');
if (existingMessage) {
existingMessage.remove();
}
const messageDiv = document.createElement('div');
messageDiv.className = `message ${type}`;
messageDiv.style.cssText = `
padding: 15px;
margin: 10px 0;
border-radius: 5px;
font-weight: 500;
text-align: center;
${type === 'success' ? 'background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb;' : ''}
${type === 'error' ? 'background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb;' : ''}
${type === 'info' ? 'background-color: #cce7ff; color: #004085; border: 1px solid #b3d7ff;' : ''}
`;
messageDiv.textContent = message;
const container = verificationStep.style.display !== 'none' ? verificationStep : registerForm;
container.insertBefore(messageDiv, container.firstChild);
setTimeout(() => {
if (messageDiv&&messageDiv.parentNode) {
messageDiv.remove();
}
}, 5000);
}
verificationCodeInput.addEventListener('input', function(e) {
this.value = this.value.replace(/[^0-9]/g, '');
if (this.value.length > 6) {
this.value = this.value.slice(0, 6);
}
if (this.value.length === 6) {
verifyButton.click();
}
});