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 // --- AJAX Auto-Save --- // Only target elements within the autosave sections const $autosaveSections = $('.mxchat-autosave-section'); if ($autosaveSections.length) { // Handle real-time range slider value updates $autosaveSections.find('input[type="range"]').on('input', function() { const value = $(this).val(); $('#threshold_value').text(value); }); // Handle all input changes (including range slider) $autosaveSections.find('input, textarea, select').on('change', function() { const $field = $(this); const name = $field.attr('name'); let value; // Handle different input types if ($field.attr('type') === 'checkbox') { value = $field.is(':checked') ? 'on' : 'off'; } else { value = $field.val(); } // Create feedback container const feedbackContainer = $('
'); const spinner = $(''); const successIcon = $(''); // Position feedback container based on input type if ($field.closest('.toggle-switch').length) { $field.closest('.toggle-switch').after(feedbackContainer); } else if ($field.closest('.slider-container').length) { $field.closest('.slider-container').after(feedbackContainer); } else { $field.after(feedbackContainer); } feedbackContainer.append(spinner); // AJAX save request $.ajax({ url: mxchatAdmin.ajax_url, type: 'POST', data: { action: 'mxchat_save_setting', name: name, value: value, _ajax_nonce: mxchatAdmin.setting_nonce }, success: function(response) { if (response.success) { spinner.fadeOut(200, function() { feedbackContainer.append(successIcon); successIcon.fadeIn(200).delay(1000).fadeOut(200, function() { feedbackContainer.remove(); }); }); } else { alert('Error saving: ' + response.data.message); if ($field.attr('type') === 'checkbox') { $field.prop('checked', !$field.is(':checked')); } feedbackContainer.remove(); } }, error: function() { alert('An error occurred while saving.'); if ($field.attr('type') === 'checkbox') { $field.prop('checked', !$field.is(':checked')); } feedbackContainer.remove(); } }); }); // Initialize color pickers with debouncing $autosaveSections.find('.my-color-field').wpColorPicker({ change: _.debounce(function(event, ui) { const $field = $(this); const name = $field.attr('name'); const value = $field.val(); // Create feedback container const feedbackContainer = $(''); const spinner = $(''); const successIcon = $(''); // Position feedback container $field.closest('.wp-picker-container').after(feedbackContainer); feedbackContainer.append(spinner); // AJAX save request $.ajax({ url: mxchatAdmin.ajax_url, type: 'POST', data: { action: 'mxchat_save_setting', name: name, value: value, _ajax_nonce: mxchatAdmin.setting_nonce }, success: function(response) { if (response.success) { spinner.fadeOut(200, function() { feedbackContainer.append(successIcon); successIcon.fadeIn(200).delay(1000).fadeOut(200, function() { feedbackContainer.remove(); }); }); } else { alert('Error saving: ' + response.data.message); feedbackContainer.remove(); } }, error: function() { alert('An error occurred while saving.'); feedbackContainer.remove(); } }); }, 500) }); } // --- 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'); toggleVisibility('#toggleDeepSeekApiKeyVisibility'); // --- 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(); // Add loading state button.prop('disabled', true); button.text('Saving...'); $.ajax({ url: mxchatAdmin.ajax_url, type: 'POST', data: { action: 'mxchat_save_inline_prompt', id: id, article_content: newContent, article_url: newUrl, _ajax_nonce: mxchatAdmin.inline_edit_nonce }, success: function(response) { button.prop('disabled', false); button.text('Save'); if (response.success) { // Update the view row.find('.content-view').html(newContent.replace(/\n/g, "