shortcode uses.
var toggleOtp=document.getElementById('toggleOtp'), otpBox=document.getElementById('otpBox'),
otpStep1=document.getElementById('otpStep1'), otpStep2=document.getElementById('otpStep2'),
otpEmail=document.getElementById('otpEmail'), otpCode=document.getElementById('otpCode'),
otpSendBtn=document.getElementById('otpSendBtn'), otpVerifyBtn=document.getElementById('otpVerifyBtn'),
otpMsg1=document.getElementById('otpMsg1'), otpMsg2=document.getElementById('otpMsg2'),
otpBack=document.getElementById('otpBack'), otpEmailDisplay=document.getElementById('otpEmailDisplay');
toggleOtp.addEventListener('click', function(e){
e.preventDefault();
var showing = otpBox.style.display !== 'none';
otpBox.style.display = showing ? 'none' : 'block';
toggleOtp.textContent = showing ? 'Or log in with a one-time email code' : 'Use your password instead';
});
otpBack.addEventListener('click', function(e){
e.preventDefault();
otpStep2.style.display = 'none';
otpStep1.style.display = 'block';
otpMsg2.textContent = '';
});
otpSendBtn.addEventListener('click', function(){
var email = otpEmail.value.trim();
if (!email) { otpMsg1.textContent = 'Please enter your email address.'; return; }
otpSendBtn.disabled = true; otpSendBtn.textContent = 'Sending...'; otpMsg1.textContent = '';
fetch('/wp-admin/admin-ajax.php', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'action=indvyapar_b2b_send_otp&email=' + encodeURIComponent(email)
})
.then(function(r){ return r.json(); })
.then(function(result){
otpSendBtn.disabled = false; otpSendBtn.textContent = 'Send login code';
if (result && result.success) {
otpEmailDisplay.textContent = email;
otpStep1.style.display = 'none';
otpStep2.style.display = 'block';
} else {
otpMsg1.textContent = (result && result.data && result.data.message) || 'Something went wrong. Please try again.';
}
})
.catch(function(){
otpSendBtn.disabled = false; otpSendBtn.textContent = 'Send login code';
otpMsg1.textContent = 'Network error, please try again.';
});
});
otpVerifyBtn.addEventListener('click', function(){
var email = otpEmail.value.trim(), code = otpCode.value.trim();
if (!code) { otpMsg2.textContent = 'Please enter the code from your email.'; return; }
otpVerifyBtn.disabled = true; otpVerifyBtn.textContent = 'Verifying...'; otpMsg2.textContent = '';
fetch('/wp-admin/admin-ajax.php', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'action=indvyapar_b2b_verify_otp&email=' + encodeURIComponent(email) + '&code=' + encodeURIComponent(code)
})
.then(function(r){ return r.json(); })
.then(function(result){
if (result && result.success) {
otpVerifyBtn.textContent = 'Success! Redirecting...';
window.location.href = (result.data && result.data.redirect) || '/b2b-dashboard/';
} else {
otpVerifyBtn.disabled = false; otpVerifyBtn.textContent = 'Verify & log in';
otpMsg2.textContent = (result && result.data && result.data.message) || 'Incorrect code.';
}
})
.catch(function(){
otpVerifyBtn.disabled = false; otpVerifyBtn.textContent = 'Verify & log in';
otpMsg2.textContent = 'Network error, please try again.';
});
});
})();