| 1 |
jQuery(document).ready(function($) { |
| 2 |
// Current page state |
| 3 |
let currentPage = 1; |
| 4 |
const perPage = 50; // Display 50 sessions per page |
| 5 |
let totalPages = 1; |
| 6 |
|
| 7 |
// Select/Deselect All functionality |
| 8 |
const selectButton = $('#mxchat-select-all-transcripts'); |
| 9 |
let isSelected = false; |
| 10 |
|
| 11 |
selectButton.click(function() { |
| 12 |
isSelected = !isSelected; |
| 13 |
$(this).toggleClass('selected'); |
| 14 |
|
| 15 |
// Update button text |
| 16 |
const buttonText = $(this).find('.button-text'); |
| 17 |
buttonText.text(isSelected ? 'Deselect All' : 'Select All'); |
| 18 |
|
| 19 |
// Update checkboxes (only for current page) |
| 20 |
$('#mxchat-transcripts').find('input[type=checkbox]').prop('checked', isSelected); |
| 21 |
}); |
| 22 |
|
| 23 |
// Search functionality |
| 24 |
$('#mxchat-search-transcripts').on('input', function() { |
| 25 |
var searchTerm = $(this).val().toLowerCase(); |
| 26 |
|
| 27 |
if (searchTerm.length > 0) { |
| 28 |
// Reset to first page when searching |
| 29 |
currentPage = 1; |
| 30 |
|
| 31 |
// Load with search filter |
| 32 |
loadTranscripts(currentPage, searchTerm); |
| 33 |
} else { |
| 34 |
// Reset to first page with no search term |
| 35 |
currentPage = 1; |
| 36 |
loadTranscripts(currentPage, ''); |
| 37 |
} |
| 38 |
}); |
| 39 |
|
| 40 |
// Initial load of transcripts |
| 41 |
loadTranscripts(currentPage, ''); |
| 42 |
|
| 43 |
// Function to load transcripts with pagination |
| 44 |
function loadTranscripts(page, searchTerm = '') { |
| 45 |
$('#mxchat-transcripts').html('<div class="mxchat-loading">Loading transcripts...</div>'); |
| 46 |
|
| 47 |
$.ajax({ |
| 48 |
url: ajaxurl, |
| 49 |
type: 'POST', |
| 50 |
data: { |
| 51 |
action: 'mxchat_fetch_chat_history', |
| 52 |
page: page, |
| 53 |
per_page: perPage, |
| 54 |
search: searchTerm |
| 55 |
}, |
| 56 |
success: function(response) { |
| 57 |
$('#mxchat-transcripts').html(response.html); |
| 58 |
currentPage = response.page; |
| 59 |
totalPages = response.total_pages; |
| 60 |
|
| 61 |
// Reset selection state when page changes |
| 62 |
isSelected = false; |
| 63 |
selectButton.removeClass('selected'); |
| 64 |
selectButton.find('.button-text').text('Select All'); |
| 65 |
|
| 66 |
// Add click handlers to pagination buttons |
| 67 |
$('.mxchat-pagination-button').on('click', function() { |
| 68 |
var pageNum = $(this).data('page'); |
| 69 |
loadTranscripts(pageNum, searchTerm); |
| 70 |
|
| 71 |
// Scroll to top of transcripts |
| 72 |
$('html, body').animate({ |
| 73 |
scrollTop: $('#mxchat-transcripts').offset().top - 50 |
| 74 |
}, 300); |
| 75 |
}); |
| 76 |
|
| 77 |
// Re-attach event handlers for newly loaded content if needed |
| 78 |
// This is important because the content is dynamically loaded |
| 79 |
attachDynamicEventHandlers(); |
| 80 |
}, |
| 81 |
error: function(xhr, status, error) { |
| 82 |
$('#mxchat-transcripts').html('<div class="mxchat-error">Error loading chat transcripts. Please try again.</div>'); |
| 83 |
console.error("AJAX Error: " + status + " - " + error); |
| 84 |
} |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
// Attach event handlers to dynamically loaded content |
| 89 |
function attachDynamicEventHandlers() { |
| 90 |
// Add any event handlers for dynamically loaded content here if needed |
| 91 |
} |
| 92 |
|
| 93 |
// Delete form submission |
| 94 |
$('#mxchat-delete-form').submit(function(e) { |
| 95 |
e.preventDefault(); |
| 96 |
|
| 97 |
// Get all checked session IDs |
| 98 |
var checkedSessionIds = $('input[name="delete_session_ids[]"]:checked').map(function() { |
| 99 |
return $(this).val(); |
| 100 |
}).get(); |
| 101 |
|
| 102 |
if (checkedSessionIds.length === 0) { |
| 103 |
alert("Please select at least one chat session to delete."); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
// Confirm deletion |
| 108 |
if (!confirm("Are you sure you want to delete the selected chat sessions? This action cannot be undone.")) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
$.ajax({ |
| 113 |
url: ajaxurl, |
| 114 |
type: 'POST', |
| 115 |
data: { |
| 116 |
action: 'mxchat_delete_chat_history', |
| 117 |
delete_session_ids: checkedSessionIds, |
| 118 |
security: $('#mxchat_delete_chat_nonce').val() |
| 119 |
}, |
| 120 |
success: function(response) { |
| 121 |
var jsonResponse = JSON.parse(response); |
| 122 |
if (jsonResponse.success) { |
| 123 |
alert("Success: " + jsonResponse.success); |
| 124 |
} else if (jsonResponse.error) { |
| 125 |
alert("Error: " + jsonResponse.error); |
| 126 |
} else { |
| 127 |
//console.log("Unexpected response format."); |
| 128 |
} |
| 129 |
|
| 130 |
// Reload the current page of transcripts |
| 131 |
loadTranscripts(currentPage); |
| 132 |
}, |
| 133 |
error: function(xhr, status, error) { |
| 134 |
//console.error("AJAX Error: " + status + " - " + error); |
| 135 |
//console.log(xhr.responseText); |
| 136 |
alert("An error occurred while deleting chat sessions. Please try again."); |
| 137 |
} |
| 138 |
}); |
| 139 |
}); |
| 140 |
|
| 141 |
// Export functionality - this remains unchanged as it should export all transcripts |
| 142 |
$('#mxchat-export-transcripts').on('click', function() { |
| 143 |
var $button = $(this); |
| 144 |
$button.prop('disabled', true).addClass('loading'); |
| 145 |
|
| 146 |
// Create a form and submit it |
| 147 |
var $form = $('<form>', { |
| 148 |
'method': 'post', |
| 149 |
'action': ajaxurl |
| 150 |
}); |
| 151 |
|
| 152 |
$form.append($('<input>', { |
| 153 |
'type': 'hidden', |
| 154 |
'name': 'action', |
| 155 |
'value': 'mxchat_export_transcripts' |
| 156 |
})); |
| 157 |
|
| 158 |
$form.append($('<input>', { |
| 159 |
'type': 'hidden', |
| 160 |
'name': 'security', |
| 161 |
'value': mxchatAdmin.export_nonce |
| 162 |
})); |
| 163 |
|
| 164 |
$form.appendTo('body').submit(); |
| 165 |
|
| 166 |
// Re-enable the button after a short delay |
| 167 |
setTimeout(function() { |
| 168 |
$button.prop('disabled', false).removeClass('loading'); |
| 169 |
}, 2000); |
| 170 |
}); |
| 171 |
|
| 172 |
// Chat Email Notification Modal functionality |
| 173 |
|
| 174 |
// Open modal |
| 175 |
$('#mxchat-chat-email-notification-btn').on('click', function(e) { |
| 176 |
e.preventDefault(); |
| 177 |
$('#mxchat-chat-email-notification-modal').fadeIn(300); |
| 178 |
}); |
| 179 |
|
| 180 |
// Close modal |
| 181 |
$('.mxchat-chat-notification-modal-close, .mxchat-chat-notification-modal-cancel').on('click', function() { |
| 182 |
$('#mxchat-chat-email-notification-modal').fadeOut(300); |
| 183 |
}); |
| 184 |
|
| 185 |
// Close modal on outside click |
| 186 |
$('#mxchat-chat-email-notification-modal').on('click', function(e) { |
| 187 |
if ($(e.target).is('#mxchat-chat-email-notification-modal')) { |
| 188 |
$(this).fadeOut(300); |
| 189 |
} |
| 190 |
}); |
| 191 |
|
| 192 |
// Handle form submission - Let WordPress handle it normally for settings |
| 193 |
$('#mxchat-chat-email-notification-form').on('submit', function(e) { |
| 194 |
// Don't prevent default - let the form submit normally to WordPress options.php |
| 195 |
var $submitButton = $(this).find('button[type="submit"]'); |
| 196 |
var originalText = $submitButton.text(); |
| 197 |
|
| 198 |
// Just show a loading state |
| 199 |
$submitButton.text('Saving...').prop('disabled', true); |
| 200 |
|
| 201 |
// The form will submit normally and reload the page |
| 202 |
}); |
| 203 |
}); |