let wadaifToken=localStorage.getItem('wadaif_otp_token')||null;
let wadaifTimer=null;
let wadaifResetToken=null;
function wadaifMsg(selector, message, type='error'){
const color =
type==='success'
? 'green'
: 'red';
jQuery(selector).html('<div style="color:' +
color +
'">' +
message +
'</div>'
);
}
function wadaifAjaxError(selector){
wadaifMsg(
selector,
'تعذر الاتصال بالخادم، يرجى المحاولة مرة أخرى'
);
}
function wadaifStartCountdown(seconds){
clearInterval(wadaifTimer);
let time=seconds;
jQuery('#send_otp')
.prop('disabled', true);
jQuery('#resend_timer')
.text('إعادة الإرسال بعد ' +
time +
' ثانية'
);
wadaifTimer=setInterval(function (){
time--;
if(time <=0){
clearInterval(wadaifTimer);
jQuery('#send_otp')
.prop('disabled', false)
.text('إعادة إرسال الرمز'
);
jQuery('#resend_timer')
.text('');
return;
}
jQuery('#resend_timer')
.text('إعادة الإرسال بعد ' +
time +
' ثانية'
);
}, 1000);
}
jQuery(document).on('click',
'#send_otp',
function (){
const phone =
jQuery('#phone').val();
wadaifMsg(
'#wadaif-msg-otp',
'',
'success'
);
jQuery.post(wadaifOTP.ajax,
{
action:
'wadaif_send_otp',
nonce:
wadaifOTP.nonce,
phone:
phone
},
function (response){
if(response.success){
jQuery('#otp_area')
.show();
jQuery('#send_otp')
.text('تم الإرسال');
wadaifStartCountdown(60);
}else{
if(typeof response.data==='object'
){
wadaifMsg(
'#wadaif-msg-otp',
response.data.message
);
}else{
wadaifMsg(
'#wadaif-msg-otp',
response.data
);
}}
}
).fail(function (){
wadaifAjaxError(
'#wadaif-msg-otp'
);
});
}
);
jQuery(document).on('click',
'#verify_otp',
function (){
const phone =
jQuery('#phone').val();
const otp =
jQuery('#otp').val();
wadaifMsg(
'#wadaif-msg-otp',
'',
'success'
);
jQuery.post(wadaifOTP.ajax,
{
action:
'wadaif_verify_otp',
nonce:
wadaifOTP.nonce,
phone:
phone,
otp:
otp
},
function (response){
if(response.success){
wadaifToken =
response.data.token;
localStorage.setItem('wadaif_otp_token',
wadaifToken
);
jQuery('#otp-success')
.show();
jQuery('#step-otp')
.hide();
jQuery('#step-profile')
.fadeIn();
}else{
wadaifMsg(
'#wadaif-msg-otp',
response.data
);
}}
).fail(function (){
wadaifAjaxError(
'#wadaif-msg-otp'
);
});
}
);
jQuery(document).on('change',
'#account_type',
function (){
const type =
jQuery(this).val();
if(type==='employer'){
jQuery('#wadaif-header-text')
.text(wadaifOTP.text_employer
);
}else{
jQuery('#wadaif-header-text')
.text(wadaifOTP.text_candidate
);
}}
);
jQuery(document).on('click',
'#complete_register',
function (){
wadaifMsg(
'#wadaif-msg-profile',
'',
'success'
);
if(!wadaifToken){
wadaifToken=localStorage.getItem('wadaif_otp_token');
}
if(!wadaifToken){
wadaifMsg(
'#wadaif-msg-profile',
'يرجى إتمام التحقق برمز OTP أولاً'
);
return;
}
const button =
jQuery('#complete_register');
button.prop('disabled',
true
);
const payload={
action:
'wadaif_complete_register',
nonce:
wadaifOTP.nonce,
token:
wadaifToken,
display_name:
jQuery('#display_name').val(),
account_type:
jQuery('#account_type').val(),
password:
jQuery('#password').val(),
password2:
jQuery('#password2').val()
};
jQuery.post(wadaifOTP.ajax,
payload,
function (response){
if(response.success){
localStorage.removeItem('wadaif_otp_token'
);
window.location.href =
response.data.redirect;
}else{
wadaifMsg(
'#wadaif-msg-profile',
response.data
);
button.prop('disabled',
false
);
}}
).fail(function (){
wadaifAjaxError(
'#wadaif-msg-profile'
);
button.prop('disabled',
false
);
});
}
);
jQuery(document).on('click',
'#send_reset_otp',
function (){
const phone =
jQuery('#reset_phone').val();
wadaifMsg(
'#reset_msg',
'',
'success'
);
jQuery.post(wadaifOTP.ajax,
{
action:
'wadaif_send_reset_otp',
nonce:
wadaifOTP.nonce,
phone:
phone
},
function (response){
if(response.success){
jQuery(
'#reset_otp_area'
).slideDown();
wadaifMsg(
'#reset_msg',
response.data,
'success'
);
}else{
wadaifMsg(
'#reset_msg',
response.data
);
}}
).fail(function (){
wadaifAjaxError(
'#reset_msg'
);
});
}
);
jQuery(document).on('click',
'#verify_reset_otp',
function (){
const phone =
jQuery('#reset_phone').val();
const otp =
jQuery('#reset_otp').val();
wadaifMsg(
'#reset_msg',
'',
'success'
);
jQuery.post(wadaifOTP.ajax,
{
action:
'wadaif_verify_reset_otp',
nonce:
wadaifOTP.nonce,
phone:
phone,
otp:
otp
},
function (response){
if(response.success){
wadaifResetToken =
response.data.token;
jQuery(
'#wadaif-reset-step-1'
).hide();
jQuery(
'#wadaif-reset-step-2'
).fadeIn();
}else{
wadaifMsg(
'#reset_msg',
response.data
);
}}
).fail(function (){
wadaifAjaxError(
'#reset_msg'
);
});
}
);
jQuery(document).on('click',
'#save_new_password',
function (){
const password =
jQuery('#new_password').val();
const confirm =
jQuery('#confirm_password').val();
if(password!==confirm){
wadaifMsg(
'#reset_msg',
'كلمتا المرور غير متطابقتين'
);
return;
}
jQuery.post(wadaifOTP.ajax,
{
action:
'wadaif_save_new_password',
nonce:
wadaifOTP.nonce,
token:
wadaifResetToken,
password:
password
},
function (response){
if(response.success){
jQuery(
'#wadaif-reset-step-2'
).html('<div style="color:green;text-align:center;padding:20px">' +
'تم تغيير كلمة المرور بنجاح' +
'</div>'
);
}else{
wadaifMsg(
'#reset_msg',
response.data
);
}}
).fail(function (){
wadaifAjaxError(
'#reset_msg'
);
});
}
);
jQuery(document).on('shown.bs.modal',
'.modal',
function (){
if(typeof grecaptcha!=='undefined'
){
jQuery('.g-recaptcha').each(function(){
if(jQuery(this).html()===''
){
grecaptcha.render(this,
{
sitekey:
jQuery(this).data('sitekey')
}
);
}});
}}
);