function mxchatOpenEditModal(intentId, phrases) {
const modal = document.getElementById('mxchat-edit-modal');
if (!modal) return;
const intentIdField = document.getElementById('edit_intent_id');
const phrasesField = document.getElementById('edit_phrases');
intentIdField.value = intentId;
phrasesField.value = phrases;
modal.style.display = 'block';
}
jQuery(document).ready(function($) {
//console.log('Script loaded'); // Confirm script is loading
// --- Tab Navigation and Toggles ---
$('.mxchat-nav-tab').on('click', function(e) {
e.preventDefault();
$('.mxchat-nav-tab').removeClass('mxchat-nav-tab-active');
$(this).addClass('mxchat-nav-tab-active');
$('.mxchat-tab-content').removeClass('active').hide();
var activeTab = $(this).attr('href');
$(activeTab).addClass('active').show();
});
// Activate the first tab by default
$('.mxchat-nav-tab-active').trigger('click');
// Attach click event to dynamically call the function
$(document).on('click', '.mxchat-edit-button', function() {
const intentId = $(this).data('intent-id');
const phrases = $(this).data('phrases');
mxchatOpenEditModal(intentId, phrases);
});
// Toggle visibility of various API keys
function toggleVisibility(selector) {
$(selector).on('click', function() {
var inputField = $(this).prev('input');
if (inputField.attr('type') === 'password') {
inputField.attr('type', 'text');
$(this).text('Hide');
} else {
inputField.attr('type', 'password');
$(this).text('Show');
}
});
}
toggleVisibility('#toggleApiKeyVisibility');
toggleVisibility('#toggleWooCommerceSecretVisibility');
toggleVisibility('#toggleLoopsApiKeyVisibility');
toggleVisibility('#toggleXaiApiKeyVisibility');
toggleVisibility('#toggleClaudeApiKeyVisibility');
toggleVisibility('#toggleBraveApiKeyVisibility');
toggleVisibility('#toggleWebhookUrlVisibility');
toggleVisibility('#toggleSecretKeyVisibility');
toggleVisibility('#toggleBotTokenVisibility');
// --- Add Intent Form Submission ---
$('#mxchat-add-intent-form').on('submit', function(event) {
$('#mxchat-intent-loading').show();
$('#mxchat-intent-loading-text').show();
$(this).find('button[type="submit"]').hide(); // Hide the submit button to prevent multiple clicks
});
// --- Inline Edit Functionality ---
$('.edit-button').on('click', function() {
var row = $(this).closest('tr');
row.find('.content-view, .url-view').hide();
row.find('.content-edit, .url-edit').show();
row.find('.edit-button').hide();
row.find('.save-button').show();
});
$('.save-button').on('click', function() {
var button = $(this);
var row = button.closest('tr');
var id = button.data('id');
var newContent = row.find('.content-edit').val();
var newUrl = row.find('.url-edit').val();
// Send an AJAX request to save the changes
$.ajax({
url: ajaxurl, // ajaxurl is automatically available in WP admin
type: 'POST',
data: {
action: 'mxchat_save_inline_prompt',
id: id,
article_content: newContent,
article_url: newUrl,
_ajax_nonce: mxchatInlineEdit.nonce // Use localized nonce
},
success: function(response) {
if (response.success) {
row.find('.content-view').html(newContent.replace(/\n/g, "
"));
row.find('.url-view a').attr('href', newUrl).text(newUrl);
row.find('.content-edit, .url-edit').hide();
row.find('.content-view, .url-view').show();
row.find('.save-button').hide();
row.find('.edit-button').show();
} else {
alert('Error saving content.');
}
},
error: function() {
alert('An error occurred.');
}
});
});
// --- Activation Script ---
// Select activation-related elements
var form = $('#mxchat-activation-form');
var spinner = $('#mxchat-activation-spinner');
var submitButton = $('#activate_license_button');
var licenseStatus = $('#mxchat-license-status');
// Ensure essential elements exist before running activation-specific code
if (form.length && licenseStatus.length && submitButton.length) {
//console.log('Activation elements detected, running activation-specific code.');
// Function to handle the response from the activation AJAX request
function handleActivationResponse(response) {
spinner.hide(); // Hide the spinner
if (response.success) {
// Update UI on successful activation
licenseStatus.text('Active');
licenseStatus.removeClass('inactive').addClass('active');
form.hide(); // Hide the activation form after successful activation
} else {
licenseStatus.text('Inactive');
alert(response.data || 'Activation failed. Please check your input.');
submitButton.prop('disabled', false); // Re-enable button on failure
}
}
// Event listener for form submission to activate the license
form.on('submit', function(event) {
event.preventDefault(); // Prevent the default form submission
//console.log("Activating license...");
// Show spinner and disable the submit button to prevent multiple submissions
spinner.show();
submitButton.prop('disabled', true);
// Gather form data
var formData = {
action: 'mxchat_activate_license',
mxchat_pro_email: $('#mxchat_pro_email').val(),
mxchat_activation_key: $('#mxchat_activation_key').val(),
security: mxchatAdmin.nonce // Ensure this is localized in PHP
};
// Send the AJAX request using jQuery
$.post(mxchatAdmin.ajax_url, formData, function(response) {
//console.log("Activation response received:", response);
handleActivationResponse(response);
}).fail(function() {
alert('Server error. Please try again.');
spinner.hide();
submitButton.prop('disabled', false);
});
});
} else {
//console.log('Activation elements not found; skipping activation-specific code.');
}
// Add new question
$('.mxchat-add-question').on('click', function () {
const container = $('#mxchat-additional-questions-container');
const questionCount = container.find('.mxchat-question-row').length + 4;
const newQuestion = `