';
$('h1').after(errorHtml);
// Re-enable button
$saveBtn.prop('disabled', false).text('Save Credentials');
// Scroll to error
$('html, body').animate({ scrollTop: 0 }, 'slow');
}
});
});
// =====================================================
// OAuth authorization handler
// =====================================================
$('#start-oauth').on('click', function () {
var clientId = $('#zoho_client_id').val();
var clientSecret = $('#zoho_client_secret').val();
if (!clientId || !clientSecret) {
alert('Please save your credentials first before starting OAuth authorization.');
return;
}
// Use server-generated OAuth URL with CSRF state parameter
var oauthUrl = hvacZoho.oauthUrl || '';
if (!oauthUrl) {
alert('OAuth URL not available. Please save your credentials first and refresh the page.');
return;
}
// Open OAuth URL in the same window to handle callback properly
window.location.href = oauthUrl;
});
// =====================================================
// 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 += '
';
$status.html(progressHtml);
// Check if there are more batches
if (result.has_more && result.next_offset > offset) {
// Continue with next batch
syncWithProgress($button, type, $status, result.next_offset, accumulated);
} else {
// All done! Show final results
displaySyncResults($button, type, $status, accumulated, result);
}
} else {
$status.html('
' +
'';
}
// Show API responses preview
if (accumulated.responses && accumulated.responses.length > 0) {
html += '' +
'📡 Raw API Responses (first 10)' +
'
';
$status.html(html);
$button.prop('disabled', false).text('Sync ' + type.charAt(0).toUpperCase() + type.slice(1));
}
// Sync button click handler
$('.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('
Starting sync for ' + type + '...
');
// Start sync with batch progress
syncWithProgress($button, type, $status, 0, null);
});
// 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) {
// Reload page to show updated status
window.location.reload();
} 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);
}
$button.prop('disabled', false).text('Save Settings');
}
},
error: function () {
// Use toast notification instead of alert
if (window.HVACToast) {
HVACToast.error('Error saving settings');
} else {
alert('Error saving settings');
}
$button.prop('disabled', false).text('Save Settings');
}
});
});
// =====================================================
// Run Scheduled Sync Now Handler
// =====================================================
$('#run-scheduled-sync-now').on('click', function () {
var $button = $(this);
var $status = $('#scheduled-sync-status');
$button.prop('disabled', true).text('Running...');
$status.html('
Starting scheduled sync...
');
$.ajax({
url: hvacZoho.ajaxUrl,
method: 'POST',
data: {
action: 'hvac_zoho_run_scheduled_sync',
nonce: hvacZoho.nonce
},
success: function (response) {
if (response.success) {
var result = response.data.result;
var html = '
';
if (result.events && result.events.staging_mode) {
html += '