| @@ -1,47 +1,115 @@ | ||
| 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 | + // 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'); | |
| 4 | 14 | |
| 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 | - }); | |
| 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 | + }); | |
| 16 | 22 | |
| 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 | - }); | |
| 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 | + }); | |
| 25 | 39 | |
| 26 | - // Load transcripts | |
| 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 | + | |
| 27 | 47 | $.ajax({ |
| 28 | 48 | url: ajaxurl, |
| 29 | 49 | type: 'POST', |
| 30 | 50 | data: { |
| 31 | - action: 'mxchat_fetch_chat_history' | |
| 51 | + action: 'mxchat_fetch_chat_history', | |
| 52 | + page: page, | |
| 53 | + per_page: perPage, | |
| 54 | + search: searchTerm | |
| 32 | 55 | }, |
| 33 | 56 | success: function(response) { |
| 34 | - $('#mxchat-transcripts').html(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); | |
| 35 | 84 | } |
| 36 | 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 | + } | |
| 37 | 92 | |
| 38 | - // Your existing delete form submission code remains the same | |
| 39 | - $('#mxchat-delete-form').submit(function(e) { | |
| 93 | + // Delete form submission | |
| 94 | + $('#mxchat-delete-form').submit(function(e) { | |
| 40 | 95 | e.preventDefault(); |
| 96 | + | |
| 97 | + // Get all checked session IDs | |
| 41 | 98 | var checkedSessionIds = $('input[name="delete_session_ids[]"]:checked').map(function() { |
| 42 | 99 | return $(this).val(); |
| 43 | 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 | + | |
| 44 | 112 | $.ajax({ |
| 45 | 113 | url: ajaxurl, |
| 46 | 114 | type: 'POST', |
| 47 | 115 | data: { |
| @@ -57,26 +125,26 @@ | ||
| 57 | 125 | alert("Error: " + jsonResponse.error); |
| 58 | 126 | } else { |
| 59 | 127 | //console.log("Unexpected response format."); |
| 60 | 128 | } |
| 61 | - location.reload(); | |
| 129 | + | |
| 130 | + // Reload the current page of transcripts | |
| 131 | + loadTranscripts(currentPage); | |
| 62 | 132 | }, |
| 63 | 133 | error: function(xhr, status, error) { |
| 64 | 134 | //console.error("AJAX Error: " + status + " - " + error); |
| 65 | 135 | //console.log(xhr.responseText); |
| 136 | + alert("An error occurred while deleting chat sessions. Please try again."); | |
| 66 | 137 | } |
| 67 | 138 | }); |
| 68 | 139 | }); |
| 69 | 140 | |
| 70 | - | |
| 71 | - | |
| 72 | - | |
| 73 | - // Export functionality | |
| 141 | + // Export functionality - this remains unchanged as it should export all transcripts | |
| 74 | 142 | $('#mxchat-export-transcripts').on('click', function() { |
| 75 | 143 | var $button = $(this); |
| 76 | 144 | $button.prop('disabled', true).addClass('loading'); |
| 77 | 145 | |
| 78 | - // Create a form and submit it (this way handles large datasets better than AJAX) | |
| 146 | + // Create a form and submit it | |
| 79 | 147 | var $form = $('<form>', { |
| 80 | 148 | 'method': 'post', |
| 81 | 149 | 'action': ajaxurl |
| 82 | 150 | }); |
| @@ -100,6 +168,36 @@ | ||
| 100 | 168 | $button.prop('disabled', false).removeClass('loading'); |
| 101 | 169 | }, 2000); |
| 102 | 170 | }); |
| 103 | 171 | |
| 172 | + // Chat Email Notification Modal functionality | |
| 104 | 173 | |
| 105 | -}); | |
| 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 | +}); | |