/** * Zoho CRM Admin JavaScript */ jQuery(document).ready(function($) { // Test connection $('#test-connection').on('click', function() { var $button = $(this); var $status = $('#connection-status'); $button.prop('disabled', true).text('Testing...'); $status.html(''); $.ajax({ url: hvacZoho.ajaxUrl, method: 'POST', data: { action: 'hvac_zoho_test_connection', nonce: hvacZoho.nonce }, success: function(response) { if (response.success) { var successHtml = '
'; successHtml += '

' + response.data.message + '

'; // Show credential details if (response.data.client_id) { successHtml += '

Client ID: ' + response.data.client_id + '

'; } if (response.data.client_secret_exists) { successHtml += '

Client Secret: ✓ Loaded

'; } if (response.data.refresh_token_exists) { successHtml += '

Refresh Token: ✓ Found

'; } else { successHtml += '

Refresh Token: ❌ Missing (OAuth required)

'; } // Show debug info if available if (response.data.debug) { successHtml += '
'; successHtml += 'Debug Information'; successHtml += '
';
                        successHtml += JSON.stringify(response.data.debug, null, 2);
                        successHtml += '
'; } successHtml += '
'; $status.html(successHtml); } else { // Debug: Log the response to see what we're getting console.log('Error response:', response); console.log('Message:', response.data.message); console.log('Has auth_url:', !!response.data.auth_url); // Handle OAuth authorization case specially if (response.data.message === 'OAuth Authorization Required' && response.data.auth_url) { var authHtml = '
'; authHtml += '

🔐 OAuth Authorization Required

'; authHtml += '

' + response.data.details + '

'; if (response.data.next_steps) { authHtml += '
    '; response.data.next_steps.forEach(function(step) { authHtml += '
  1. ' + step + '
  2. '; }); authHtml += '
'; } authHtml += '

🚀 Authorize with Zoho CRM

'; // Show credential status if (response.data.credentials_status) { authHtml += '

Current Status:

'; authHtml += ''; } authHtml += '

After authorization, come back and test the connection again.

'; authHtml += '
'; $status.html(authHtml); return; } else { console.log('OAuth conditions not met:'); console.log('Message matches:', response.data.message === 'OAuth Authorization Required'); console.log('Auth URL exists:', !!response.data.auth_url); } // Create detailed error display for other errors var errorHtml = '
'; errorHtml += '

' + response.data.message + ': ' + response.data.error + '

'; // Add error code if available if (response.data.code) { errorHtml += '

Error Code: ' + response.data.code + '

'; } // Add details if available if (response.data.details) { errorHtml += '

Details: ' + response.data.details + '

'; } // Add debugging info errorHtml += '
'; errorHtml += '

Debug Information:

'; errorHtml += '

Check the PHP error log for more details.

'; errorHtml += '

Log location: wp-content/plugins/hvac-community-events/logs/zoho-debug.log

'; // Add raw response data if available if (response.data.raw) { errorHtml += '
'; errorHtml += 'Raw Response Data (click to expand)'; errorHtml += '
' + 
                                     JSON.stringify(JSON.parse(response.data.raw), null, 2) + 
                                     '
'; errorHtml += '
'; } // Add file/line info if available (for exceptions) if (response.data.file) { errorHtml += '

File: ' + response.data.file + '

'; } // Add trace if available if (response.data.trace) { errorHtml += '
'; errorHtml += 'Stack Trace (click to expand)'; errorHtml += '
' + 
                                     response.data.trace + 
                                     '
'; errorHtml += '
'; } errorHtml += '
'; // Close debug info errorHtml += '
'; // Close notice $status.html(errorHtml); } }, error: function(xhr, status, error) { var errorHtml = '
'; errorHtml += '

AJAX Error: Connection test failed

'; errorHtml += '

Status: ' + status + '

'; errorHtml += '

Error: ' + error + '

'; errorHtml += '
'; $status.html(errorHtml); }, complete: function() { $button.prop('disabled', false).text('Test Connection'); } }); }); // Sync data $('.sync-button').on('click', function() { var $button = $(this); var type = $button.data('type'); var $status = $('#' + type + '-status'); $button.prop('disabled', true).text('Syncing...'); $status.html('

Syncing ' + type + '...

'); $.ajax({ url: hvacZoho.ajaxUrl, method: 'POST', data: { action: 'hvac_zoho_sync_data', type: type, nonce: hvacZoho.nonce }, success: function(response) { if (response.success) { var result = response.data; var html = '
'; if (result.staging_mode) { html += '

🔧 STAGING MODE - Simulation Results

'; html += '

' + result.message + '

'; } else { html += '

Sync completed successfully!

'; } html += ''; if (result.test_data && result.test_data.length > 0) { html += '
' + 'View test data (first 5 records)' + '
' +
                            JSON.stringify(result.test_data.slice(0, 5), null, 2) +
                            '
' + '
'; } html += '
'; $status.html(html); } else { $status.html('

' + response.data.message + ': ' + response.data.error + '

'); } }, error: function() { $status.html('

Sync failed

'); }, complete: function() { $button.prop('disabled', false).text('Sync ' + type.charAt(0).toUpperCase() + type.slice(1)); } }); }); // Save settings $('#zoho-settings-form').on('submit', function(e) { e.preventDefault(); var $form = $(this); var $button = $form.find('button[type="submit"]'); $button.prop('disabled', true).text('Saving...'); $.ajax({ url: hvacZoho.ajaxUrl, method: 'POST', data: { action: 'hvac_zoho_save_settings', nonce: hvacZoho.nonce, auto_sync: $form.find('input[name="auto_sync"]').is(':checked') ? '1' : '0', sync_frequency: $form.find('select[name="sync_frequency"]').val() }, success: function(response) { if (response.success) { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.success('Settings saved successfully!'); } else { alert('Settings saved successfully!'); } } else { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error('Error saving settings: ' + response.data.message); } else { alert('Error saving settings: ' + response.data.message); } } }, error: function() { // Use toast notification instead of alert if (window.HVACToast) { HVACToast.error('Error saving settings'); } else { alert('Error saving settings'); } }, complete: function() { $button.prop('disabled', false).text('Save Settings'); } }); }); });