jQuery(document).ready(function($){
'use strict';
window.wiSessionKey=null;
$('#wi-btn-start').on('click', function(e){
e.preventDefault();
const audience=$('#target_audience').val().trim();
const goal=$('#primary_goal').val().trim();
const title=$('#webinar_title').val().trim();
const hostName=$('#host_name').val().trim();
if(!title||!audience||!goal){
alert(wiAiGen.fillFieldsmsg||'Please fill all fields');
return;
}
$(this).prop('disabled', true).text(wiAiGen.loadingmsg||'Loading...');
$.ajax({
url: wiAiGen.ajax_url,
type: 'POST',
dataType: 'json',
data: {
action: 'wi_start_wizard',
target_audience: audience,
primary_goal: goal,
webinar_title: title,
host_name: hostName,
nonce: wiAiGen.nonce
},
success: function(response){
if(response.success){
window.wiSessionKey=response.data.session_key;
console.log('Session created:', window.wiSessionKey);
$('#wi-step-1').fadeOut(300, function(){
$('#wi-step-2').fadeIn(300);
});
}else{
alert((wiAiGen.errorPrefix||'Error: ') + (response.data.message||(wiAiGen.unknownErrormsg||'Unknown error')));
$('#wi-btn-start').prop('disabled', false).text(wiAiGen.continueBtnmsg||'Continue →');
}},
error: function(xhr, status, error){
console.error('AJAX Error:', error, xhr.responseText);
alert((wiAiGen.connectionErrorPrefix||'Connection error: ') + error);
$('#wi-btn-start').prop('disabled', false).text(wiAiGen.continueBtnmsg||'Continue →');
}});
});
$('#wi-btn-generate').on('click', function(e){
e.preventDefault();
if(!window.wiSessionKey){
alert(wiAiGen.sessionExpiredmsg||'Session expired. Please start over.');
return;
}
const websiteUrl=$('#website_url').val().trim();
const socialUrls=$('#social_urls').val().trim();
const webinarType=$('#webinar_type').val()||'live';
const $btn=$(this);
$btn.prop('disabled', true).text(wiAiGen.generateBtnmsg||'🤖 Generating...');
$('#wi-loading').show();
$.ajax({
url: wiAiGen.ajax_url,
type: 'POST',
dataType: 'json',
data: {
action: 'wi_confirm_and_generate',
session_key: window.wiSessionKey,
website_url: websiteUrl,
webinar_type: webinarType,
social_urls: socialUrls,
nonce: wiAiGen.nonce
},
timeout: 120000,
success: function(response){
console.log('Generation response:', response);
if(response.success){
const jsonContent=response.data.content||null;
console.log('JSON content available:', !!jsonContent);
console.log('JSON content length:', jsonContent ? jsonContent.length:0);
if(!jsonContent||jsonContent.trim().length===0){
console.error('❌ No content received from server');
alert(wiAiGen.noContentmsg||'❌ Error: No content was generated. Please try again.');
$('#wi-loading').hide();
$btn.prop('disabled', false).text(wiAiGen.generatewebinarBtnmsg||'🤖 Generate Webinar →');
return;
}
$('#wi-loading').hide();
showEditButtonPopup(jsonContent);
}else{
const errorMsg=response.data.message||(wiAiGen.unknownErrormsg||'Unknown error occurred');
console.error('Generation failed:', errorMsg);
alert((wiAiGen.errorPrefix||'❌ Error: ') + errorMsg);
$('#wi-loading').hide();
$btn.prop('disabled', false).text(wiAiGen.generatewebinarBtnmsg||'🤖 Generate Webinar →');
}},
error: function(xhr, status, error){
console.error('AJAX Error:', error, xhr.responseText);
let errorMsg='Connection error: ' + error;
try {
const response=JSON.parse(xhr.responseText);
if(response.data&&response.data.message){
errorMsg=response.data.message;
}} catch (e){
}
alert((wiAiGen.errorPrefixShort||'❌ ') + errorMsg);
$('#wi-loading').hide();
$btn.prop('disabled', false).text(wiAiGen.generatewebinarBtnmsg||'🤖 Generate Webinar →');
}});
});
function isValidEmail(email){
const re=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
function showEditButtonPopup(jsonContent){
fillTextarea(jsonContent);
const popupHTML=`
<div id="wi-edit-popup" style="
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
z-index: 10000;
display: flex;
justify-content: center;
align-items: center;
">
<div style="
background: white;
padding: 30px;
border-radius: 12px;
max-width: 400px;
text-align: center;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
">
<h3 style="margin: 0 0 15px 0; color: #333;">${wiAiGen.popupSuccessTitle||'✅ Webinar Generated Successfully!'}</h3>
<p style="margin: 0 0 20px 0; color: #666;">${wiAiGen.popupSuccessBody||'Content has been filled. Would you like to edit?'}</p>
<div style="display: flex; gap: 10px; justify-content: center;">
<button id="wi-popup-edit" style="
padding: 10px 20px;
background: #2196F3;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: 600;
">${wiAiGen.popupEditBtn||'Edit'}</button>
<button id="wi-popup-no" style="
padding: 10px 20px;
background: #f5f5f5;
color: #333;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: 600;
">${wiAiGen.popupNoBtn||'No'}</button>
</div>
<p id="wi-popup-countdown" style="margin: 15px 0 0 0; color: #999; font-size: 12px;"></p>
</div>
</div>
`;
$('body').append(popupHTML);
let countdown=5;
const $countdownEl=$('#wi-popup-countdown');
$countdownEl.text((wiAiGen.popupCountdownPrefix||'Continuing in ') + countdown + (wiAiGen.popupCountdownSuffix||' seconds...'));
let editClicked=false;
const countdownInterval=setInterval(function(){
countdown--;
if(countdown > 0){
$countdownEl.text('Continuing in ' + countdown + ' seconds...');
}else{
clearInterval(countdownInterval);
if(!editClicked){
clickCreateNewAppBtn();
}
$('#wi-edit-popup').remove();
}}, 1000);
$('#wi-popup-edit').on('click', function(){
editClicked=true;
clearInterval(countdownInterval);
$('#wi-edit-popup').remove();
console.log('Edit clicked - textarea already filled, not clicking createnewappBTN');
});
$('#wi-popup-no').on('click', function(){
clearInterval(countdownInterval);
$('#wi-edit-popup').remove();
clickCreateNewAppBtn();
});
}
function clickCreateNewAppBtn(){
const $btn=$('#createnewappBTN');
if($btn.length){
$btn.trigger('click');
console.log('Clicked createnewappBTN button');
}else{
console.warn('Button #createnewappBTN not found');
}}
function fillTextarea(jsonContent){
console.log('fillTextarea called with:', {
hasContent: !!jsonContent,
contentLength: jsonContent ? jsonContent.length:0
});
if(!jsonContent||jsonContent.trim().length===0){
console.error('❌ No content provided to fillTextarea');
alert(wiAiGen.noContentToFillmsg||'Error: No content available to fill the textarea.');
return;
}
const $textarea=$('#importcode');
if($textarea.length){
$textarea.val(jsonContent);
$('#wi-success-message').html('<p style="color: green; font-weight: bold;">' + (wiAiGen.textareaSuccessTitle||'✅ Webinar generated successfully!') + '</p>' +
'<p>' + (wiAiGen.textareaSuccessBody||'JSON content has been filled in the importcode textarea.') + '</p>'
).show();
console.log('✅ Filled importcode textarea with JSON content');
}else{
console.warn('⚠️ Textarea #importcode not found, will retry after delay');
setTimeout(function(){
const $retryTextarea=$('#importcode');
if($retryTextarea.length){
$retryTextarea.val(jsonContent);
$('#wi-success-message').html('<p style="color: green; font-weight: bold;">' + (wiAiGen.textareaSuccessTitle||'✅ Webinar generated successfully!') + '</p>' +
'<p>' + (wiAiGen.textareaSuccessBody||'JSON content has been filled in the importcode textarea.') + '</p>'
).show();
console.log('✅ Filled importcode textarea on retry');
}else{
console.error('❌ Textarea #importcode still not found after retry');
alert(wiAiGen.textareaNotFoundmsg||'Textarea #importcode not found. Please check the page.');
}}, 500);
}}
});