| @@ -1,53 +1,173 @@ | ||
| 1 | - jQuery(document).ready(function($) { | |
| 2 | - const selectButton = $('#mxchat-select-all-transcripts'); | |
| 3 | - let isSelected = false; | |
| 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 | + // Track selected sessions for bulk delete | |
| 8 | + let selectedSessions = new Set(); | |
| 9 | + | |
| 10 | + // Select/Deselect All functionality | |
| 11 | + const selectButton = $('#mxchat-select-all-transcripts'); | |
| 12 | + let isSelected = false; | |
| 13 | + | |
| 14 | + selectButton.click(function() { | |
| 15 | + isSelected = !isSelected; | |
| 16 | + $(this).toggleClass('selected'); | |
| 4 | 17 | |
| 5 | - selectButton.click(function() { | |
| 6 | - isSelected = !isSelected; | |
| 7 | - $(this).toggleClass('selected'); | |
| 8 | - | |
| 9 | - // Update button text | |
| 10 | - const buttonText = $(this).find('.button-text'); | |
| 11 | - buttonText.text(isSelected ? 'Deselect All' : 'Select All'); | |
| 12 | - | |
| 13 | - // Update checkboxes | |
| 14 | - $('#mxchat-transcripts').find('input[type=checkbox]').prop('checked', isSelected); | |
| 15 | - }); | |
| 18 | + // Update button text | |
| 19 | + const buttonText = $(this).find('.button-text'); | |
| 20 | + buttonText.text(isSelected ? 'Deselect All' : 'Select All'); | |
| 21 | + | |
| 22 | + // Update session selection (for current page) | |
| 23 | + $('.mxchat-session-header').each(function() { | |
| 24 | + const sessionId = $(this).data('session-id'); | |
| 25 | + const sessionContainer = $(this).closest('.mxchat-session'); | |
| 26 | + if (isSelected) { | |
| 27 | + selectedSessions.add(sessionId); | |
| 28 | + sessionContainer.addClass('selected'); | |
| 29 | + } else { | |
| 30 | + selectedSessions.delete(sessionId); | |
| 31 | + sessionContainer.removeClass('selected'); | |
| 32 | + } | |
| 33 | + }); | |
| 34 | + | |
| 35 | + updateDeleteButtonState(); | |
| 36 | + }); | |
| 16 | 37 | |
| 17 | - // Search functionality | |
| 18 | - $('#mxchat-search-transcripts').on('input', function() { | |
| 19 | - var searchTerm = $(this).val().toLowerCase(); | |
| 20 | - $('.mxchat-session').each(function() { | |
| 21 | - var sessionText = $(this).text().toLowerCase(); | |
| 22 | - $(this).toggle(sessionText.includes(searchTerm)); | |
| 23 | - }); | |
| 24 | - }); | |
| 38 | + // Search functionality | |
| 39 | + $('#mxchat-search-transcripts').on('input', function() { | |
| 40 | + var searchTerm = $(this).val().toLowerCase(); | |
| 41 | + | |
| 42 | + if (searchTerm.length > 0) { | |
| 43 | + // Reset to first page when searching | |
| 44 | + currentPage = 1; | |
| 45 | + | |
| 46 | + // Load with search filter | |
| 47 | + loadTranscripts(currentPage, searchTerm); | |
| 48 | + } else { | |
| 49 | + // Reset to first page with no search term | |
| 50 | + currentPage = 1; | |
| 51 | + loadTranscripts(currentPage, ''); | |
| 52 | + } | |
| 53 | + }); | |
| 25 | 54 | |
| 26 | - // Load transcripts | |
| 55 | + // Initial load of transcripts | |
| 56 | + loadTranscripts(currentPage, ''); | |
| 57 | + | |
| 58 | + // Function to load transcripts with pagination | |
| 59 | + function loadTranscripts(page, searchTerm = '') { | |
| 60 | + $('#mxchat-transcripts').html('<div class="mxchat-loading">Loading transcripts...</div>'); | |
| 61 | + | |
| 27 | 62 | $.ajax({ |
| 28 | 63 | url: ajaxurl, |
| 29 | 64 | type: 'POST', |
| 30 | 65 | data: { |
| 31 | - action: 'mxchat_fetch_chat_history' | |
| 66 | + action: 'mxchat_fetch_chat_history', | |
| 67 | + page: page, | |
| 68 | + per_page: perPage, | |
| 69 | + search: searchTerm | |
| 32 | 70 | }, |
| 33 | 71 | success: function(response) { |
| 34 | - $('#mxchat-transcripts').html(response); | |
| 72 | + $('#mxchat-transcripts').html(response.html); | |
| 73 | + currentPage = response.page; | |
| 74 | + totalPages = response.total_pages; | |
| 75 | + | |
| 76 | + // Reset selection state when page changes (but keep selectedSessions for bulk operations) | |
| 77 | + isSelected = false; | |
| 78 | + selectButton.removeClass('selected'); | |
| 79 | + selectButton.find('.button-text').text('Select All'); | |
| 80 | + | |
| 81 | + // Restore selection state for sessions on this page | |
| 82 | + $('.mxchat-session-header').each(function() { | |
| 83 | + const sessionId = $(this).data('session-id'); | |
| 84 | + const sessionContainer = $(this).closest('.mxchat-session'); | |
| 85 | + if (selectedSessions.has(sessionId)) { | |
| 86 | + sessionContainer.addClass('selected'); | |
| 87 | + } | |
| 88 | + }); | |
| 89 | + | |
| 90 | + updateDeleteButtonState(); | |
| 91 | + | |
| 92 | + // Add click handlers to pagination buttons | |
| 93 | + $('.mxchat-pagination-button').on('click', function() { | |
| 94 | + var pageNum = $(this).data('page'); | |
| 95 | + loadTranscripts(pageNum, searchTerm); | |
| 96 | + | |
| 97 | + // Scroll to top of transcripts | |
| 98 | + $('html, body').animate({ | |
| 99 | + scrollTop: $('#mxchat-transcripts').offset().top - 50 | |
| 100 | + }, 300); | |
| 101 | + }); | |
| 102 | + | |
| 103 | + // Re-attach event handlers for newly loaded content | |
| 104 | + attachDynamicEventHandlers(); | |
| 105 | + }, | |
| 106 | + error: function(xhr, status, error) { | |
| 107 | + $('#mxchat-transcripts').html('<div class="mxchat-error">Error loading chat transcripts. Please try again.</div>'); | |
| 108 | + console.error("AJAX Error: " + status + " - " + error); | |
| 35 | 109 | } |
| 36 | 110 | }); |
| 37 | - | |
| 38 | - // Your existing delete form submission code remains the same | |
| 39 | - $('#mxchat-delete-form').submit(function(e) { | |
| 40 | - e.preventDefault(); | |
| 41 | - var checkedSessionIds = $('input[name="delete_session_ids[]"]:checked').map(function() { | |
| 42 | - return $(this).val(); | |
| 43 | - }).get(); | |
| 111 | + } | |
| 112 | + | |
| 113 | + // Attach event handlers to dynamically loaded content | |
| 114 | + function attachDynamicEventHandlers() { | |
| 115 | + // Handle individual delete button clicks | |
| 116 | + $('.mxchat-delete-btn').off('click').on('click', function(e) { | |
| 117 | + e.preventDefault(); | |
| 118 | + e.stopPropagation(); | |
| 119 | + | |
| 120 | + const sessionId = $(this).data('session-id'); | |
| 121 | + | |
| 122 | + if (!confirm("Are you sure you want to delete this chat session? This action cannot be undone.")) { | |
| 123 | + return; | |
| 124 | + } | |
| 125 | + | |
| 126 | + // Delete single session | |
| 127 | + deleteSessions([sessionId]); | |
| 128 | + }); | |
| 129 | + | |
| 130 | + // Handle session header clicks for selection (bulk delete) | |
| 131 | + $('.mxchat-session-header').off('click').on('click', function(e) { | |
| 132 | + // Don't trigger if clicking the delete button | |
| 133 | + if ($(e.target).hasClass('mxchat-delete-btn') || $(e.target).closest('.mxchat-delete-btn').length) { | |
| 134 | + return; | |
| 135 | + } | |
| 136 | + | |
| 137 | + const sessionId = $(this).data('session-id'); | |
| 138 | + const sessionContainer = $(this).closest('.mxchat-session'); | |
| 139 | + | |
| 140 | + if (selectedSessions.has(sessionId)) { | |
| 141 | + selectedSessions.delete(sessionId); | |
| 142 | + sessionContainer.removeClass('selected'); | |
| 143 | + } else { | |
| 144 | + selectedSessions.add(sessionId); | |
| 145 | + sessionContainer.addClass('selected'); | |
| 146 | + } | |
| 147 | + | |
| 148 | + updateDeleteButtonState(); | |
| 149 | + }); | |
| 150 | + } | |
| 151 | + | |
| 152 | + // Update delete button state based on selections | |
| 153 | + function updateDeleteButtonState() { | |
| 154 | + const deleteButton = $('.delete-chats-button'); | |
| 155 | + if (selectedSessions.size > 0) { | |
| 156 | + deleteButton.prop('disabled', false); | |
| 157 | + } else { | |
| 158 | + deleteButton.prop('disabled', true); | |
| 159 | + } | |
| 160 | + } | |
| 161 | + | |
| 162 | + // Function to delete sessions | |
| 163 | + function deleteSessions(sessionIds) { | |
| 44 | 164 | $.ajax({ |
| 45 | 165 | url: ajaxurl, |
| 46 | 166 | type: 'POST', |
| 47 | 167 | data: { |
| 48 | 168 | action: 'mxchat_delete_chat_history', |
| 49 | - delete_session_ids: checkedSessionIds, | |
| 169 | + delete_session_ids: sessionIds, | |
| 50 | 170 | security: $('#mxchat_delete_chat_nonce').val() |
| 51 | 171 | }, |
| 52 | 172 | success: function(response) { |
| 53 | 173 | var jsonResponse = JSON.parse(response); |
| @@ -52,18 +172,106 @@ | ||
| 52 | 172 | success: function(response) { |
| 53 | 173 | var jsonResponse = JSON.parse(response); |
| 54 | 174 | if (jsonResponse.success) { |
| 55 | 175 | alert("Success: " + jsonResponse.success); |
| 176 | + | |
| 177 | + // Remove deleted sessions from selectedSessions | |
| 178 | + sessionIds.forEach(id => selectedSessions.delete(id)); | |
| 179 | + | |
| 56 | 180 | } else if (jsonResponse.error) { |
| 57 | 181 | alert("Error: " + jsonResponse.error); |
| 58 | 182 | } else { |
| 59 | 183 | //console.log("Unexpected response format."); |
| 60 | 184 | } |
| 61 | - location.reload(); | |
| 185 | + | |
| 186 | + // Reload the current page of transcripts | |
| 187 | + loadTranscripts(currentPage); | |
| 62 | 188 | }, |
| 63 | 189 | error: function(xhr, status, error) { |
| 64 | 190 | //console.error("AJAX Error: " + status + " - " + error); |
| 65 | 191 | //console.log(xhr.responseText); |
| 192 | + alert("An error occurred while deleting chat sessions. Please try again."); | |
| 66 | 193 | } |
| 67 | 194 | }); |
| 195 | + } | |
| 196 | + | |
| 197 | + // Delete form submission (bulk delete) | |
| 198 | + $('#mxchat-delete-form').submit(function(e) { | |
| 199 | + e.preventDefault(); | |
| 200 | + | |
| 201 | + if (selectedSessions.size === 0) { | |
| 202 | + alert("Please select at least one chat session to delete."); | |
| 203 | + return; | |
| 204 | + } | |
| 205 | + | |
| 206 | + // Confirm deletion | |
| 207 | + if (!confirm(`Are you sure you want to delete the selected ${selectedSessions.size} chat session(s)? This action cannot be undone.`)) { | |
| 208 | + return; | |
| 209 | + } | |
| 210 | + | |
| 211 | + // Convert Set to Array and delete | |
| 212 | + deleteSessions(Array.from(selectedSessions)); | |
| 68 | 213 | }); |
| 69 | -}); | |
| 214 | + | |
| 215 | + // Export functionality - this remains unchanged as it should export all transcripts | |
| 216 | + $('#mxchat-export-transcripts').on('click', function() { | |
| 217 | + var $button = $(this); | |
| 218 | + $button.prop('disabled', true).addClass('loading'); | |
| 219 | + | |
| 220 | + // Create a form and submit it | |
| 221 | + var $form = $('<form>', { | |
| 222 | + 'method': 'post', | |
| 223 | + 'action': ajaxurl | |
| 224 | + }); | |
| 225 | + | |
| 226 | + $form.append($('<input>', { | |
| 227 | + 'type': 'hidden', | |
| 228 | + 'name': 'action', | |
| 229 | + 'value': 'mxchat_export_transcripts' | |
| 230 | + })); | |
| 231 | + | |
| 232 | + $form.append($('<input>', { | |
| 233 | + 'type': 'hidden', | |
| 234 | + 'name': 'security', | |
| 235 | + 'value': mxchatAdmin.export_nonce | |
| 236 | + })); | |
| 237 | + | |
| 238 | + $form.appendTo('body').submit(); | |
| 239 | + | |
| 240 | + // Re-enable the button after a short delay | |
| 241 | + setTimeout(function() { | |
| 242 | + $button.prop('disabled', false).removeClass('loading'); | |
| 243 | + }, 2000); | |
| 244 | + }); | |
| 245 | + | |
| 246 | + // Chat Email Notification Modal functionality | |
| 247 | + | |
| 248 | + // Open modal | |
| 249 | + $('#mxchat-chat-email-notification-btn').on('click', function(e) { | |
| 250 | + e.preventDefault(); | |
| 251 | + $('#mxchat-chat-email-notification-modal').fadeIn(300); | |
| 252 | + }); | |
| 253 | + | |
| 254 | + // Close modal | |
| 255 | + $('.mxchat-chat-notification-modal-close, .mxchat-chat-notification-modal-cancel').on('click', function() { | |
| 256 | + $('#mxchat-chat-email-notification-modal').fadeOut(300); | |
| 257 | + }); | |
| 258 | + | |
| 259 | + // Close modal on outside click | |
| 260 | + $('#mxchat-chat-email-notification-modal').on('click', function(e) { | |
| 261 | + if ($(e.target).is('#mxchat-chat-email-notification-modal')) { | |
| 262 | + $(this).fadeOut(300); | |
| 263 | + } | |
| 264 | + }); | |
| 265 | + | |
| 266 | + // Handle form submission - Let WordPress handle it normally for settings | |
| 267 | + $('#mxchat-chat-email-notification-form').on('submit', function(e) { | |
| 268 | + // Don't prevent default - let the form submit normally to WordPress options.php | |
| 269 | + var $submitButton = $(this).find('button[type="submit"]'); | |
| 270 | + var originalText = $submitButton.text(); | |
| 271 | + | |
| 272 | + // Just show a loading state | |
| 273 | + $submitButton.text('Saving...').prop('disabled', true); | |
| 274 | + | |
| 275 | + // The form will submit normally and reload the page | |
| 276 | + }); | |
| 277 | +}); | |