/** * HVAC Trainer Profile Admin JavaScript * Handles admin functionality for trainer profile management * * @package HVAC_Community_Events * @version 1.0.0 */ jQuery(document).ready(function($) { // Test Geocoding Functionality $('#test-geocoding').on('click', function(e) { e.preventDefault(); const $button = $(this); const originalText = $button.text(); // Get test address from user or use default const testAddress = prompt('Enter an address to test geocoding:', 'New York, NY, USA'); if (!testAddress) { return; } $button.prop('disabled', true).text('Testing...'); $.ajax({ url: hvacProfileAdmin.ajax_url, type: 'POST', data: { action: 'hvac_test_geocoding', nonce: hvacProfileAdmin.nonce, address: testAddress }, success: function(response) { if (response.success) { const data = response.data; let message = `Geocoding Test Results:\n\n`; message += `Address: ${data.address}\n`; message += `Coordinates: ${data.coordinates.lat}, ${data.coordinates.lng}\n`; message += `Formatted: ${data.formatted_address}\n`; message += `Confidence: ${data.confidence}`; alert(message); } else { alert('Geocoding test failed: ' + response.data); } }, error: function() { alert('Network error occurred during geocoding test'); }, complete: function() { $button.prop('disabled', false).text(originalText); } }); }); // Bulk Geocoding let bulkGeocodeRunning = false; $('#bulk-geocode').on('click', function(e) { e.preventDefault(); if (bulkGeocodeRunning) { return; } const $button = $(this); const originalText = $button.text(); if (!confirm('This will geocode addresses for profiles that don\'t have coordinates yet. Continue?')) { return; } bulkGeocodeRunning = true; $button.prop('disabled', true).text('Processing...'); performBulkGeocode($button, originalText); }); function performBulkGeocode($button, originalText) { $.ajax({ url: hvacProfileAdmin.ajax_url, type: 'POST', data: { action: 'hvac_bulk_geocode', nonce: hvacProfileAdmin.nonce }, success: function(response) { if (response.success) { const processed = response.data.processed; if (processed > 0) { $button.text(`Processed ${processed} profiles...`); // Continue processing if there are more profiles setTimeout(() => { performBulkGeocode($button, originalText); }, 2000); } else { // Finished alert('Bulk geocoding completed!'); bulkGeocodeRunning = false; $button.prop('disabled', false).text(originalText); // Refresh the page to update statistics setTimeout(() => { location.reload(); }, 1000); } } else { alert('Bulk geocoding failed: ' + response.data); bulkGeocodeRunning = false; $button.prop('disabled', false).text(originalText); } }, error: function() { alert('Network error occurred during bulk geocoding'); bulkGeocodeRunning = false; $button.prop('disabled', false).text(originalText); } }); } // Force Sync All Profiles $('#sync-profiles').on('click', function(e) { e.preventDefault(); const $button = $(this); const originalText = $button.text(); if (!confirm('This will force synchronization between user data and trainer profiles. Continue?')) { return; } $button.prop('disabled', true).text('Syncing...'); $.ajax({ url: hvacProfileAdmin.ajax_url, type: 'POST', data: { action: 'hvac_sync_profiles', nonce: hvacProfileAdmin.nonce }, success: function(response) { if (response.success) { alert('Profile synchronization completed successfully!'); // Refresh the page to update statistics setTimeout(() => { location.reload(); }, 1000); } else { alert('Profile synchronization failed: ' + response.data); } }, error: function() { alert('Network error occurred during profile synchronization'); }, complete: function() { $button.prop('disabled', false).text(originalText); } }); }); // API Key Field Enhancement const $apiKeyField = $('input[name="hvac_google_maps_api_key"]'); if ($apiKeyField.length) { // Add show/hide toggle for API key const $toggleButton = $(''); $apiKeyField.after($toggleButton); $toggleButton.on('click', function(e) { e.preventDefault(); if ($apiKeyField.attr('type') === 'password') { $apiKeyField.attr('type', 'text'); $(this).text('Hide'); } else { $apiKeyField.attr('type', 'password'); $(this).text('Show'); } }); } // Statistics Auto-refresh function refreshStatistics() { $.ajax({ url: hvacProfileAdmin.ajax_url, type: 'POST', data: { action: 'hvac_get_profile_stats', nonce: hvacProfileAdmin.nonce }, success: function(response) { if (response.success) { const stats = response.data; // Update statistics display $('.hvac-stat-item').each(function() { const $this = $(this); const $number = $this.find('.hvac-stat-number'); const $label = $this.find('.hvac-stat-label'); const label = $label.text().toLowerCase(); if (label.includes('total')) { $number.text(stats.total_profiles); } else if (label.includes('public')) { $number.text(stats.public_profiles); } else if (label.includes('geocoded')) { $number.text(stats.geocoded_profiles); } else if (label.includes('sync')) { $number.text(stats.sync_issues); } }); } } }); } // Auto-refresh statistics every 30 seconds setInterval(refreshStatistics, 30000); // Migration Status Check function checkMigrationStatus() { $.ajax({ url: hvacProfileAdmin.ajax_url, type: 'POST', data: { action: 'hvac_get_migration_status', nonce: hvacProfileAdmin.nonce }, success: function(response) { if (response.success && response.data.status) { const status = response.data; if (status.status === 'in_progress') { showMigrationProgress(status); } else if (status.status === 'not_started') { showMigrationPrompt(); } } } }); } function showMigrationProgress(status) { const message = `Migration in progress... Processed: ${status.processed || 0}/${status.total_records || 0}`; if (!$('#migration-progress').length) { const $progressDiv = $('
' + message + '
Trainer Profile Migration Available
A new trainer profile system is available. Would you like to migrate existing user data to the new system?