/** * WooCommerce Business Central - Admin JavaScript */ (function($) { 'use strict'; var WBC_Admin = { /** * Initialize */ init: function() { this.bindEvents(); }, /** * Bind event handlers */ bindEvents: function() { // Test connection $('#wbc-test-connection').on('click', this.testConnection); // Manual sync $('#wbc-manual-sync').on('click', this.manualSync); // Clear logs $('#wbc-clear-logs').on('click', this.clearLogs); // Load companies $('#wbc-load-companies').on('click', this.loadCompanies); // Select company $('#wbc-company-select').on('change', this.selectCompany); // Toggle log data $(document).on('click', '.wbc-toggle-data', this.toggleLogData); }, /** * Test connection to Business Central */ testConnection: function(e) { e.preventDefault(); var $btn = $(this); var $status = $('#wbc-connection-status'); $btn.prop('disabled', true); $status.removeClass('success error').addClass('loading').text(wbc_admin.strings.testing); $.ajax({ url: wbc_admin.ajax_url, type: 'POST', data: { action: 'wbc_test_connection', nonce: wbc_admin.nonce }, success: function(response) { $btn.prop('disabled', false); $status.removeClass('loading'); if (response.success) { $status.addClass('success').text(response.data.message); } else { $status.addClass('error').text(response.data.message || 'Connection failed'); } }, error: function(xhr, status, error) { $btn.prop('disabled', false); $status.removeClass('loading').addClass('error').text('Request failed: ' + error); } }); }, /** * Run manual sync */ manualSync: function(e) { e.preventDefault(); var $btn = $(this); var $status = $('#wbc-sync-status'); $btn.prop('disabled', true); $status.removeClass('success error').addClass('loading').text(wbc_admin.strings.syncing); $.ajax({ url: wbc_admin.ajax_url, type: 'POST', data: { action: 'wbc_manual_sync', nonce: wbc_admin.nonce }, success: function(response) { $btn.prop('disabled', false); $status.removeClass('loading'); if (response.success) { $status.addClass('success').text(response.data.message); } else { $status.addClass('error').text(response.data.message || 'Sync failed'); } }, error: function(xhr, status, error) { $btn.prop('disabled', false); $status.removeClass('loading').addClass('error').text('Request failed: ' + error); } }); }, /** * Clear all logs */ clearLogs: function(e) { e.preventDefault(); if (!confirm(wbc_admin.strings.confirm_clear)) { return; } var $btn = $(this); var originalText = $btn.text(); $btn.prop('disabled', true).text(wbc_admin.strings.clearing); $.ajax({ url: wbc_admin.ajax_url, type: 'POST', data: { action: 'wbc_clear_logs', nonce: wbc_admin.nonce }, success: function(response) { if (response.success) { // Reload the page to show empty logs location.reload(); } else { alert(response.data.message || 'Failed to clear logs'); $btn.prop('disabled', false).text(originalText); } }, error: function(xhr, status, error) { alert('Request failed: ' + error); $btn.prop('disabled', false).text(originalText); } }); }, /** * Load companies from Business Central */ loadCompanies: function(e) { e.preventDefault(); var $btn = $(this); var $select = $('#wbc-company-select'); var $list = $('#wbc-companies-list'); var originalText = $btn.text(); $btn.prop('disabled', true).text(wbc_admin.strings.loading); $.ajax({ url: wbc_admin.ajax_url, type: 'POST', data: { action: 'wbc_get_companies', nonce: wbc_admin.nonce }, success: function(response) { $btn.prop('disabled', false).text(originalText); if (response.success && response.data.companies) { // Clear existing options except the first one $select.find('option:not(:first)').remove(); // Add companies $.each(response.data.companies, function(i, company) { $select.append( $('