/** * Admin JavaScript for Informatiq Smart Pricing */ (function($) { 'use strict'; var InformatiqSP = { /** * Initialize */ init: function() { this.bindEvents(); }, /** * Bind event handlers */ bindEvents: function() { $('#informatiq-sp-manual-sync').on('click', this.handleManualSync); $('#informatiq-sp-test-connection').on('click', this.handleTestConnection); $('#informatiq-sp-revoke-auth').on('click', this.handleRevokeAuth); $('#informatiq-sp-compare-products').on('click', this.handleCompareProducts); }, /** * Handle manual sync button click */ handleManualSync: function(e) { e.preventDefault(); var $button = $(this); var $status = $('#informatiq-sp-sync-status'); // Confirm action if (!confirm(informatiqSP.strings.confirmSync || 'Are you sure you want to run a manual price sync? This may take several minutes.')) { return; } // Disable button and show loading state $button.prop('disabled', true).addClass('informatiq-sp-loading'); // Show status message $status .removeClass('notice-success notice-error') .addClass('notice-info') .html('
' + (informatiqSP.strings.syncInProgress || 'Sync in progress...') + '
') .show(); // Make AJAX request $.ajax({ url: informatiqSP.ajaxUrl, type: 'POST', data: { action: 'informatiq_sp_manual_sync', nonce: informatiqSP.nonce }, success: function(response) { if (response.success) { $status .removeClass('notice-info notice-error') .addClass('notice-success') .html('' + response.data.message + '
'); // Reload page after 2 seconds to show updated logs setTimeout(function() { location.reload(); }, 2000); } else { $status .removeClass('notice-info notice-success') .addClass('notice-error') .html('Error: ' + (response.data.message || 'Unknown error') + '
'); } }, error: function(jqXHR, textStatus, errorThrown) { $status .removeClass('notice-info notice-success') .addClass('notice-error') .html('Error: ' + errorThrown + '
'); }, complete: function() { $button.prop('disabled', false).removeClass('informatiq-sp-loading'); } }); }, /** * Handle test connection button click */ handleTestConnection: function(e) { e.preventDefault(); var $button = $(this); var $status = $('#informatiq-sp-sync-status'); // Disable button and show loading state $button.prop('disabled', true).addClass('informatiq-sp-loading'); // Show status message $status .removeClass('notice-success notice-error') .addClass('notice-info') .html('' + (informatiqSP.strings.testInProgress || 'Testing connection...') + '
') .show(); // Make AJAX request $.ajax({ url: informatiqSP.ajaxUrl, type: 'POST', data: { action: 'informatiq_sp_test_connection', nonce: informatiqSP.nonce }, success: function(response) { if (response.success) { $status .removeClass('notice-info notice-error') .addClass('notice-success') .html('' + response.data.message + '
'); } else { $status .removeClass('notice-info notice-success') .addClass('notice-error') .html('Error: ' + (response.data.message || 'Unknown error') + '
'); } }, error: function(jqXHR, textStatus, errorThrown) { $status .removeClass('notice-info notice-success') .addClass('notice-error') .html('Error: ' + errorThrown + '
'); }, complete: function() { $button.prop('disabled', false).removeClass('informatiq-sp-loading'); // Hide status message after 5 seconds setTimeout(function() { $status.fadeOut(); }, 5000); } }); }, /** * Handle revoke authorization button click */ handleRevokeAuth: function(e) { e.preventDefault(); var $button = $(this); // Confirm action if (!confirm(informatiqSP.strings.revokeConfirm || 'Are you sure you want to revoke Google authorization?')) { return; } // Disable button and show loading state $button.prop('disabled', true).text(informatiqSP.strings.revokeInProgress || 'Revoking...'); // Make AJAX request $.ajax({ url: informatiqSP.ajaxUrl, type: 'POST', data: { action: 'informatiq_sp_revoke_auth', nonce: informatiqSP.nonce }, success: function(response) { if (response.success) { // Reload page to show updated status location.reload(); } else { alert('Error: ' + (response.data.message || 'Unknown error')); $button.prop('disabled', false).text('Revoke Authorization'); } }, error: function(jqXHR, textStatus, errorThrown) { alert('Error: ' + errorThrown); $button.prop('disabled', false).text('Revoke Authorization'); } }); }, // Store comparison data for pagination and bulk updates. comparisonData: null, currentPage: 1, perPage: 50, /** * Handle compare products button click */ handleCompareProducts: function(e) { e.preventDefault(); var self = InformatiqSP; var $button = $(this); var $spinner = $button.next('.spinner'); var $results = $('#informatiq-sp-comparison-results'); var $status = $('#informatiq-sp-sync-status'); // Show loading status $status .removeClass('notice-success notice-error') .addClass('notice-info') .html('Loading price insights from Google... This may take a moment.
') .show(); $button.prop('disabled', true); $spinner.addClass('is-active'); $.ajax({ url: informatiqSP.ajaxUrl, type: 'POST', timeout: 180000, // 3 minute timeout data: { action: 'informatiq_sp_compare_products', nonce: informatiqSP.nonce }, success: function(response) { console.log('Price insights response:', response); $status.hide(); if (response.success) { self.comparisonData = response.data; self.currentPage = 1; self.renderComparison(); $results.show(); } else { $status .removeClass('notice-info notice-success') .addClass('notice-error') .html('Error: ' + (response.data.message || 'Unknown error') + '
') .show(); } }, error: function(jqXHR, textStatus, errorThrown) { console.error('AJAX error:', textStatus, errorThrown); $status .removeClass('notice-info notice-success') .addClass('notice-error') .html('Error: ' + errorThrown + '
') .show(); }, complete: function() { $button.prop('disabled', false); $spinner.removeClass('is-active'); } }); }, /** * Render comparison table with pagination */ renderComparison: function() { var self = this; var data = this.comparisonData; var $tbody = $('#informatiq-sp-comparison-tbody'); var $summary = $('#informatiq-sp-comparison-summary'); var $pagination = $('#informatiq-sp-pagination'); if (!data || !data.products) return; var products = data.products; var totalPages = Math.ceil(products.length / this.perPage); var start = (this.currentPage - 1) * this.perPage; var end = start + this.perPage; var pageProducts = products.slice(start, end); // Stats var stats = { total: products.length, withInsight: 0, canIncrease: 0, shouldDecrease: 0, totalPotentialGain: 0 }; products.forEach(function(p) { if (p.has_insight) { stats.withInsight++; if (p.potential_gain > 0) { stats.canIncrease++; stats.totalPotentialGain += p.potential_gain; } else if (p.potential_gain < 0) { stats.shouldDecrease++; } } }); // Render summary var summaryHtml = 'Summary: ' + stats.withInsight + ' of ' + stats.total + ' products have Google price insights. '; summaryHtml += '' + stats.canIncrease + ' can increase price, '; summaryHtml += '' + stats.shouldDecrease + ' should decrease price. '; if (stats.totalPotentialGain > 0) { summaryHtml += '