/* Turbo Starter Templates Admin Scripts */

// Tab functionality
function showTab(tabName) {
    document.querySelectorAll('.tab-content').forEach(tab => tab.classList.remove('active'));
    document.querySelectorAll('.trstemp-tab-link').forEach(link => link.classList.remove('active'));
    document.getElementById(tabName).classList.add('active');
    document.querySelector(`[href="#${tabName}"]`).classList.add('active');
}

// Dashboard functions
function testImport() {
    if (typeof turbo_dashboard === 'undefined') {
        alert('Dashboard data not loaded');
        return;
    }
    
    fetch(turbo_dashboard.ajax_url, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=debug_import&nonce=' + turbo_dashboard.nonce
    }).then(() => {
        alert('Test import completed. Check debug log and refresh page.');
        location.reload();
    });
}

function activatePlugins() {
    if (typeof turbo_dashboard === 'undefined') {
        alert('Dashboard data not loaded');
        return;
    }
    
    fetch(turbo_dashboard.ajax_url, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=trstemp_activate_required_plugins&nonce=' + turbo_dashboard.nonce
    }).then(() => {
        alert('Plugins activated. Refresh page.');
        location.reload();
    });
}

function debugPages() {
    if (typeof turbo_dashboard === 'undefined') {
        alert('Dashboard data not loaded');
        return;
    }
    
    fetch(turbo_dashboard.ajax_url, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=trstemp_debug_pages&nonce=' + turbo_dashboard.nonce
    }).then(() => {
        alert('Debug completed. Check error log.');
    });
}

function debugWizardSession() {
    if (typeof turbo_dashboard === 'undefined') {
        alert('Dashboard data not loaded');
        return;
    }
    
    window.open(turbo_dashboard.ajax_url + '?action=trstemp_debug_wizard_session&nonce=' + turbo_dashboard.nonce, '_blank');
}

// Notice dismiss functionality
jQuery(document).ready(function($) {
    $('.trstemp-notice-dismiss').on('click', function() {
        $(this).closest('.trstemp-notice').fadeOut();
        if (typeof turbo_dashboard !== 'undefined') {
            fetch(turbo_dashboard.ajax_url, {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: 'action=turbo_dismiss_notice&nonce=' + turbo_dashboard.nonce
            });
        }
    });
});
