/** * Payment Scheduler Admin JavaScript * * Handles the admin interface for the automated payment scheduler * * 🤖 Generated with Claude Code (https://claude.ai/code) */ jQuery(document).ready(function($) { // Save scheduler settings $('#scheduler-settings-form').on('submit', function(e) { e.preventDefault(); var $form = $(this); var $button = $form.find('button[type="submit"]'); var originalText = $button.text(); $button.text('Saving...').prop('disabled', true); var formData = { action: 'save_scheduler_settings', nonce: ebScheduler.nonce, days_before: $('#days_before').val(), percentage: $('#percentage').val(), expiry_hours: $('#expiry_hours').val(), admin_notifications: $('#admin_notifications').is(':checked') ? 'yes' : 'no' }; $.ajax({ url: ebScheduler.ajaxurl, type: 'POST', data: formData, success: function(response) { if (response.success) { showNotice('Settings saved successfully!', 'success'); } else { showNotice('Error: ' + response.data, 'error'); } }, error: function() { showNotice('An error occurred while saving settings.', 'error'); }, complete: function() { $button.text(originalText).prop('disabled', false); } }); }); // Manual payment check $('#manual-payment-check').on('click', function() { if (!confirm(ebScheduler.strings.confirmManualRun)) { return; } var $button = $(this); var originalText = $button.text(); $button.text(ebScheduler.strings.processing).prop('disabled', true); $.ajax({ url: ebScheduler.ajaxurl, type: 'POST', data: { action: 'manual_payment_check', nonce: ebScheduler.nonce }, success: function(response) { if (response.success) { var results = response.data; var message = 'Manual check completed!\n\n' + 'Processed: ' + results.processed + ' bookings\n' + 'Sent: ' + results.sent + ' payment requests\n' + 'Skipped: ' + results.skipped + ' bookings\n' + 'Errors: ' + results.errors; showNotice(message.replace(/\n/g, '
'), 'success'); // Reload page after 3 seconds to show updated activity setTimeout(function() { location.reload(); }, 3000); } else { showNotice('Error: ' + response.data, 'error'); } }, error: function() { showNotice('An error occurred during the manual check.', 'error'); }, complete: function() { $button.text(originalText).prop('disabled', false); } }); }); // Form validation $('#days_before').on('input', function() { var value = parseInt($(this).val()); if (value < 1) $(this).val(1); if (value > 365) $(this).val(365); }); $('#percentage').on('input', function() { var value = parseInt($(this).val()); if (value < 1) $(this).val(1); if (value > 100) $(this).val(100); }); // Show admin notices function showNotice(message, type) { var noticeClass = type === 'success' ? 'notice-success' : 'notice-error'; var $notice = $('

' + message + '

'); $('.wrap h1').after($notice); // Auto-dismiss after 8 seconds setTimeout(function() { $notice.fadeOut(function() { $(this).remove(); }); }, 8000); // Handle manual dismiss $notice.find('.notice-dismiss').on('click', function() { $notice.fadeOut(function() { $(this).remove(); }); }); } // Real-time activity updates (every 60 seconds if on the page) if (window.location.href.includes('eb-payment-scheduler')) { setInterval(function() { // Only refresh if no forms are currently being submitted if (!$('button:disabled').length) { $('.activity-log').load(location.href + ' .activity-log > *'); } }, 60000); } });