(function($){
'use strict';
const config={
animationDuration: 400,
debounceDelay: 300
};
let state={
currentForm: 'login',
currentStep: 1,
isAnimating: false,
isSubmitting: false,
formData: {},
validators: new Map()
};
$(document).ready(function(){
if(typeof cekonay_auth==='undefined'){
console.error('Variables cekonay_auth manquantes');
return;
}
initAuth();
});
function initAuth(){
console.log('🚀 Initialisation Cekonay Auth Formulaire');
if(document.body.classList.contains('logged-in')||document.cookie.includes('wordpress_logged_in_')){
window.location.replace(cekonay_auth.myaccount_url);
return;
}
initializeVideo();
initializeForms();
initializeSteps();
initializeValidation();
initializeNavigation();
console.log('✅ Cekonay Auth Formulaire initialisé');
}
function initializeVideo(){
const video=document.getElementById('cekonay-bg-video');
if(!video) return;
video.addEventListener('loadeddata', function(){
video.classList.add('loaded');
video.play().catch(e=> console.warn('⚠️ Autoplay vidéo bloqué:', e));
});
video.addEventListener('error', function(e){
console.warn('⚠️ Erreur vidéo:', e);
video.style.display='none';
});
if(/Mobi|Android/i.test(navigator.userAgent)){
video.removeAttribute('autoplay');
}}
function initializeForms(){
$('#cekonay-login-form form').on('submit', handleLogin);
$('#cekonay-register-hidden-form').on('submit', handleRegister);
showForm('login');
}
async function handleLogin(e){
e.preventDefault();
if(state.isSubmitting) return;
const $form=$(e.target);
const $button=$form.find('.cekonay-submit-btn');
try {
state.isSubmitting=true;
setFormLoading($form, true);
const formData=prepareFormData($form, 'login');
const response=await submitForm(formData, 'login');
if(response.success){
showMessage(response.data.message, 'success');
setTimeout(()=> {
window.location.href=response.data.redirect||cekonay_auth.myaccount_url;
}, response.data.already_logged_in ? 800:1000);
}else{
const msg =
(response.data&&(response.data.message||response.data.error||response.message)) ||
'Erreur inconnue';
throw new Error(typeof msg==='string' ? msg:JSON.stringify(msg));
}} catch (error){
console.error('❌ Erreur:', error);
showMessage(error.message, 'error');
} finally {
state.isSubmitting=false;
setFormLoading($form, false);
}}
async function handleRegister(e){
e.preventDefault();
if(state.isSubmitting) return;
const password=$('#reg_password').val();
const confirm=$('#reg_password_confirm').val();
if(password!==confirm){
showMessage('Les mots de passe ne correspondent pas', 'error');
return;
}
if(!$('#accept_terms').is(':checked')){
showMessage('Veuillez accepter les conditions', 'error');
return;
}
const $button=$('.cekonay-submit-btn');
try {
state.isSubmitting=true;
setRegisterLoading(true);
saveStepData();
const formData=prepareRegisterData();
const response=await submitForm(formData, 'register');
if(response.success){
showMessage(response.data.message, 'success');
setTimeout(()=> {
window.location.href=response.data.redirect||cekonay_auth.myaccount_url;
}, 1000);
}else{
const msg =
(response.data&&(response.data.message||response.data.error||response.message)) ||
'Erreur inconnue';
throw new Error(typeof msg==='string' ? msg:JSON.stringify(msg));
}} catch (error){
console.error('❌ Erreur inscription:', error);
showMessage(error.message, 'error');
} finally {
state.isSubmitting=false;
setRegisterLoading(false);
}}
function prepareFormData(form, formType){
const formData=new FormData();
formData.append('action', `cekonay_${formType}`);
if(formType==='login'){
formData.append('nonce', cekonay_auth.login_nonce);
}else{
formData.append('nonce', cekonay_auth.register_nonce);
}
const inputs=form.find('input[type="text"], input[type="email"], input[type="password"], input[type="tel"]');
inputs.each(function(){
if(this.name&&this.value.trim()){
formData.append(this.name, this.value.trim());
}});
const checkbox=form.find('input[type="checkbox"]:checked');
if(checkbox.length){
formData.append(checkbox.attr('name'), checkbox.val());
}
return formData;
}
function prepareRegisterData(){
const formData=new FormData();
formData.append('action', 'cekonay_register');
formData.append('nonce', cekonay_auth.register_nonce);
Object.keys(state.formData).forEach(key=> {
formData.append(key, state.formData[key]);
});
const acceptTerms=$('#accept_terms');
if(acceptTerms.is(':checked')){
formData.append('reg_accept_terms', '1');
}
return formData;
}
async function submitForm(formData, formType){
const response=await fetch(cekonay_auth.ajax_url, {
method: 'POST',
body: formData,
credentials: 'same-origin'
});
if(!response.ok){
throw new Error(`Erreur réseau: ${response.status}`);
}
return await response.json();
}
function setFormLoading(form, isLoading){
const button=form.find('.cekonay-submit-btn');
const inputs=form.find('input, button');
if(isLoading){
button.addClass('loading');
inputs.prop('disabled', true);
}else{
button.removeClass('loading');
inputs.prop('disabled', false);
}}
function setRegisterLoading(isLoading){
const button=$('#cekonay-register-form .cekonay-submit-btn');
const inputs=$('#cekonay-register-form input, #cekonay-register-form button');
if(isLoading){
button.addClass('loading');
inputs.prop('disabled', true);
}else{
button.removeClass('loading');
inputs.prop('disabled', false);
}}
function initializeSteps(){
const progressBar=$('#cekonay-progress');
if(progressBar.length){
progressBar.removeClass('visible');
}
showStep(1);
}
window.nextStep=function(step){
if(state.isAnimating) return;
if(!validateCurrentStep()){
return;
}
saveStepData();
showStep(step);
updateProgress(step);
if(step===3){
updateSummary();
}};
window.prevStep=function(step){
if(state.isAnimating) return;
showStep(step);
updateProgress(step);
};
function showStep(stepNumber){
if(state.isAnimating) return;
state.isAnimating=true;
const currentStepEl=$('.cekonay-step.active');
const targetStepEl=$(`.cekonay-step[data-step="${stepNumber}"]`);
if(!targetStepEl.length){
state.isAnimating=false;
return;
}
if(currentStepEl.length){
currentStepEl.css({
'opacity': '0',
'transform': stepNumber > state.currentStep ? 'translateX(-30px)':'translateX(30px)'
});
setTimeout(()=> {
currentStepEl.removeClass('active').hide();
targetStepEl.show().css({
'opacity': '0',
'transform': stepNumber > state.currentStep ? 'translateX(30px)':'translateX(-30px)'
});
setTimeout(()=> {
targetStepEl.addClass('active').css({
'opacity': '1',
'transform': 'translateX(0)'
});
const firstInput=targetStepEl.find('input').first();
if(firstInput.length) firstInput.focus();
state.currentStep=stepNumber;
state.isAnimating=false;
}, 50);
}, config.animationDuration);
}else{
targetStepEl.show().addClass('active').css({
'opacity': '1',
'transform': 'translateX(0)'
});
state.currentStep=stepNumber;
state.isAnimating=false;
}}
function updateProgress(step){
const progressBar=$('#cekonay-progress');
const steps=progressBar.find('.progress-step');
const lines=progressBar.find('.progress-line');
steps.removeClass('active completed').each(function(index){
const stepNum=index + 1;
const $step=$(this);
if(stepNum===step){
$step.addClass('active');
}else if(stepNum < step){
$step.addClass('completed');
}});
lines.removeClass('completed').each(function(index){
if(index + 1 < step){
$(this).addClass('completed');
}});
}
function validateCurrentStep(){
const currentStepEl=$('.cekonay-step.active');
if(!currentStepEl.length) return true;
const inputs=currentStepEl.find('input[required]');
let isValid=true;
inputs.each(function(){
const $input=$(this);
if(this.type==='checkbox'){
if(!this.checked){
const termsGroup=$input.closest('.cekonay-terms-group');
if(termsGroup.length){
termsGroup.css({
'border-color': '#ef4444',
'background-color': '#fef2f2'
});
}
isValid=false;
}}else if(!this.value.trim()){
$input.addClass('cekonay-field-invalid');
showFieldError($input, 'Ce champ est requis');
isValid=false;
}else{
$input.removeClass('cekonay-field-invalid');
showFieldError($input, '');
}});
return isValid;
}
function saveStepData(){
const currentStepEl=$('.cekonay-step.active');
if(!currentStepEl.length) return;
currentStepEl.find('input').each(function(){
if(this.name&&this.value.trim()){
state.formData[this.name]=this.value.trim();
}});
}
function updateSummary(){
$('#summary-name').text(`${state.formData.reg_first_name||''} ${state.formData.reg_last_name||''}`.trim());
$('#summary-email').text(state.formData.reg_email||'');
$('#summary-mobile').text(state.formData.reg_mobile||'');
}
function initializeValidation(){
$('#reg_password_confirm').on('input', function(){
const password=$('#reg_password').val();
const match=this.value&&password&&this.value===password;
$(this).toggleClass('cekonay-field-valid', match)
.toggleClass('cekonay-field-invalid', this.value&&!match);
});
$('#reg_email').on('blur', function(){
const isValid=/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.value);
$(this).toggleClass('cekonay-field-valid', isValid&&this.value)
.toggleClass('cekonay-field-invalid', this.value&&!isValid);
});
const passwordField=$('#reg_password');
if(passwordField.length){
passwordField.on('input', debounce(updatePasswordStrength, 200));
}
const acceptTerms=$('#accept_terms');
if(acceptTerms.length){
acceptTerms.on('change', function(){
const termsGroup=$(this).closest('.cekonay-terms-group');
if(this.checked){
termsGroup.css({
'border-color': '#10b981',
'background': 'linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%)'
});
}else{
termsGroup.css({
'border-color': '#fbbf24',
'background': 'linear-gradient(135deg, #fffbeb 0%, #fef3c7 100%)'
});
}});
}}
function updatePasswordStrength(){
const password=$('#reg_password').val();
const indicator=$('#password-strength-indicator');
if(!password||!indicator.length) return;
const strength=calculatePasswordStrength(password);
const levels=['Très faible', 'Faible', 'Moyen', 'Fort', 'Très fort'];
const colors=['#ef4444', '#f59e0b', '#eab308', '#22c55e', '#10b981'];
indicator.text(`Force: ${levels[strength]}`).css({
'color': colors[strength],
'border-color': colors[strength]
}).addClass('visible');
}
function calculatePasswordStrength(password){
let score=0;
if(password.length >=8) score++;
if(password.length >=12) score++;
if(/[A-Z]/.test(password)) score++;
if(/[a-z]/.test(password)) score++;
if(/[0-9]/.test(password)) score++;
if(/[^a-zA-Z0-9]/.test(password)) score++;
return Math.min(Math.floor(score * 4 / 6), 4);
}
function showFieldError(field, message){
let feedback=field.parent().find('.cekonay-field-feedback');
if(message){
if(!feedback.length){
feedback=$('<div class="cekonay-field-feedback"></div>');
field.parent().append(feedback);
}
feedback.text(message).show();
}else if(feedback.length){
feedback.hide();
}}
function initializeNavigation(){
$('.cekonay-switch-btn').on('click', function(e){
e.preventDefault();
const target=$(this).data('target');
if(target) switchForm(target);
});
}
function switchForm(target){
if(state.isAnimating) return;
const currentFormEl=$('.cekonay-form-section.active');
const targetFormEl=$(`#cekonay-${target}-form`);
const subtitle=$('#cekonay-subtitle');
const progressBar=$('#cekonay-progress');
if(!targetFormEl.length) return;
state.isAnimating=true;
if(currentFormEl.length){
currentFormEl.css({
'opacity': '0',
'transform': 'translateY(-20px)'
});
setTimeout(()=> {
currentFormEl.removeClass('active').hide();
targetFormEl.show().css({
'opacity': '0',
'transform': 'translateY(20px)'
});
setTimeout(()=> {
targetFormEl.addClass('active').css({
'opacity': '1',
'transform': 'translateY(0)'
});
if(target==='register'){
progressBar.addClass('visible');
showStep(1);
updateProgress(1);
}else{
progressBar.removeClass('visible');
}
const titles={
login: 'Connectez-vous à votre compte',
register: 'Créez votre compte Cekonay'
};
if(subtitle.length){
subtitle.text(titles[target]);
}
state.currentForm=target;
state.isAnimating=false;
const firstInput=targetFormEl.find('input').first();
if(firstInput.length) firstInput.focus();
}, 50);
}, config.animationDuration);
}}
function showForm(formType){
$('.cekonay-form-section').removeClass('active').hide();
const targetForm=$(`#cekonay-${formType}-form`);
if(targetForm.length){
targetForm.show().addClass('active');
}
state.currentForm=formType;
}
function showMessage(message, type){
const $container=$('#cekonay-messages');
const className=type==='error' ? 'cekonay-message--error':'cekonay-message--success';
let text=message;
if(typeof text!=='string'){
if(text&&typeof text==='object'&&typeof text.message==='string'){
text=text.message;
}else{
try { text=JSON.stringify(text); } catch (e){ text=String(text); }}
}
if(type==='error'&&text.length > 40){
text=text.slice(0, 40) + '…';
}
$container.html(`<div class="cekonay-message ${className}">${text}</div>`);
setTimeout(()=> {
$container.empty();
}, 5000);
}
function debounce(func, wait){
let timeout;
return function executedFunction(...args){
const later=()=> {
clearTimeout(timeout);
func.apply(this, args);
};
clearTimeout(timeout);
timeout=setTimeout(later, wait);
};}})(jQuery);
(function (){
'use strict';
document.addEventListener('DOMContentLoaded', function (){
const selectors=[
'#cekonay-login-form input[type="password"]',
'#cekonay-register-form input[type="password"]'
];
const fields=document.querySelectorAll(selectors.join(','));
if(!fields.length) return;
fields.forEach((input)=> {
if(input.closest('.ckn-passwrap')) return;
const wrap=document.createElement('div');
wrap.className='ckn-passwrap';
input.parentNode.insertBefore(wrap, input);
wrap.appendChild(input);
const btn=document.createElement('button');
btn.type='button';
btn.className='ckn-pass-toggle';
btn.setAttribute('aria-label', 'Afficher le mot de passe');
btn.setAttribute('aria-pressed', 'false');
btn.innerHTML=getEyeSVG();
wrap.appendChild(btn);
btn.addEventListener('click', function (e){
e.preventDefault();
const isMasked=input.getAttribute('type')==='password';
input.setAttribute('type', isMasked ? 'text':'password');
btn.setAttribute('aria-pressed', String(isMasked));
btn.setAttribute('aria-label', isMasked ? 'Masquer le mot de passe':'Afficher le mot de passe');
btn.innerHTML=isMasked ? getEyeOffSVG():getEyeSVG();
input.focus({ preventScroll: true });
});
btn.addEventListener('mousedown', (e)=> e.preventDefault());
});
function getEyeSVG(){
return `
<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true">
<path d="M1 12s4-7 11-7 11 7 11 7-4 7-11 7S1 12 1 12Z" fill="none" stroke="currentColor" stroke-width="2"/>
<circle cx="12" cy="12" r="3" fill="none" stroke="currentColor" stroke-width="2"/>
</svg>`;
}
function getEyeOffSVG(){
return `
<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true">
<path d="M3 3l18 18" fill="none" stroke="currentColor" stroke-width="2"/>
<path d="M10.58 10.58A3 3 0 0 0 12 15a3 3 0 0 0 2.12-.88M9.88 7.88A6.75 6.75 0 0 1 12 7c7 0 11 7 11 7a16.32 16.32 0 0 1-4.2 4.64M6.11 5.5A16 16 0 0 0 1 14s4 7 11 7a10.4 10.4 0 0 0 4.5-1.02" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
</svg>`;
}});
})();
jQuery(function ($){
$(document).ready(function (){
$('.dropdown-menu-column').each(function (index){
var indexPlusOne=index + 1;
var $dropdownMenuItems=$(this).find('.dropdown-menu-item');
var $mainMenuItem=$('.first-level-' + indexPlusOne + '>a');
$dropdownMenuItems.wrapAll('<div class="dropdown-menu-container-' + indexPlusOne + '" />');
var $dropdownContainer=$('.dropdown-menu-container-' + indexPlusOne);
$dropdownContainer.insertAfter($mainMenuItem);
});
var $firstLevel=$('.et_mobile_menu .first-level > a');
var $allDropdowns=$('.et_mobile_menu [class*="dropdown-menu-container"]');
$firstLevel.off('click').on('click', function (e){
e.preventDefault();
var $thisDropdown=$(this).siblings();
$thisDropdown.slideToggle();
$(this).toggleClass('icon-switch');
if($(this).hasClass('first-level-open')){
$(this).removeClass('first-level-open').addClass('first-level-closed');
$firstLevel.not(this).css('height', '50px');
}else{
$(this).removeClass('first-level-closed').addClass('first-level-open');
$firstLevel.not(this).css('height', '50px');
}
if($firstLevel.filter('.first-level-open').length===0){
$firstLevel.css('height', 'calc((97vh - 80px)/3)');
}
var dropdownSiblings=$allDropdowns.not($thisDropdown);
dropdownSiblings.slideUp();
var $thisFirstLevel=$(this);
var $firstLevelSiblings=$firstLevel.not($thisFirstLevel);
$firstLevelSiblings.removeClass('icon-switch').removeClass('first-level-open').addClass('first-level-closed').removeClass('other-menu-open');
});
if($(window).width() <=980){
$firstLevel.each(function (index){
$(this).addClass('first-level-' + (index + 1));
});
$firstLevel.addClass('first-level-closed');
}});
});