// Swiss Football Admin JavaScript jQuery(document).ready(function($) { 'use strict'; // Settings form with Save and Test functionality $('#swi-foot-settings-form').on('submit', function(e) { var $form = $(this); var $status = $('#connection-status'); var action = $('[name="swi_foot_action"]:focus').val() || 'save_only'; e.preventDefault(); // Get form data var formData = new FormData($form[0]); // Submit form via WordPress options.php fetch($form.attr('action'), { method: 'POST', credentials: 'same-origin', body: formData }).then(function(resp) { // After form saves successfully, test if requested if (action === 'save_and_test') { return test_api_connection($status); } else { $('

Settings saved!

') .insertAfter('.wrap h1'); } }).catch(function(err) { $('

Error saving settings: ' + err.message + '

') .insertAfter('.wrap h1'); }); }); // Function to test API connection function test_api_connection($statusElement) { // Get current field values from form var apiUrl = $('input[name="swi_foot_api_base_url"]').val(); var username = $('input[name="swi_foot_api_username"]').val(); var password = $('input[name="swi_foot_api_password"]').val(); // Validate credentials first if (!username || !password) { $('

API credentials not configured. Please configure Username and Password.

') .insertAfter('.wrap h1'); return; } if (!apiUrl) { $('

API Base URL not configured.

') .insertAfter('.wrap h1'); return; } // Step 1: Check if URL is reachable with a HEAD request var urlTest = apiUrl.replace(/\/$/, '') + '/'; fetch(urlTest, { method: 'HEAD', credentials: 'same-origin', mode: 'no-cors' }).then(function(resp) { // Step 2: Make actual API call via REST endpoint return fetch(swi_foot_ajax.rest_url.replace(/\/$/, '') + '/admin/test-connection', { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': swi_foot_ajax.rest_nonce }, body: JSON.stringify({ test: true }) }); }).then(function(resp) { return resp.json(); }).then(function(response) { if (response && response.success) { $('

Settings saved and connection test successful!

') .insertAfter('.wrap h1'); } else { var errorMsg = (response.error || 'Connection failed'); var details = response.details ? ': ' + response.details : ''; $('

' + errorMsg + details + '

') .insertAfter('.wrap h1'); } }).catch(function(err) { $('

API URL not reachable: ' + apiUrl + '

') .insertAfter('.wrap h1'); }); } // Handle button clicks to set which action was used $('button[name="swi_foot_action"]').on('click', function() { $(this).focus(); }); // Refresh teams functionality $('#refresh-teams').on('click', function() { var $button = $(this); var $status = $('#refresh-status'); $button.prop('disabled', true).text('Refreshing...'); $status.text('').removeClass('success error'); fetch(swi_foot_ajax.rest_url.replace(/\/$/, '') + '/admin/refresh-teams', { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': swi_foot_ajax.rest_nonce } }).then(function(resp) { return resp.json(); }).then(function(response) { if (response && response.success) { $status.text('✓ Teams refreshed successfully!').addClass('success'); setTimeout(function() { location.reload(); }, 1500); } else { $status.text('✗ Error: ' + (response.error || 'Unknown')).addClass('error'); } }).catch(function() { $status.text('✗ Network error occurred.').addClass('error'); }).finally(function() { $button.prop('disabled', false).text('Refresh Teams List'); }); }); // Clear cache functionality $('#clear-cache').on('click', function() { var $button = $(this); var $status = $('#cache-status'); if (!confirm('Are you sure you want to clear the match data cache?')) { return; } $button.prop('disabled', true); $status.text('Clearing cache...').removeClass('success error'); fetch(swi_foot_ajax.rest_url.replace(/\/$/, '') + '/admin/clear-cache', { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': swi_foot_ajax.rest_nonce } }).then(function(resp) { return resp.json(); }).then(function(response) { if (response && response.success) { $status.text('✓ Cache cleared successfully!').addClass('success'); setTimeout(function() { location.reload(); }, 1000); } else { $status.text('✗ Error clearing cache.').addClass('error'); } }).catch(function() { $status.text('✗ Error clearing cache.').addClass('error'); }).finally(function() { $button.prop('disabled', false); }); }); });