/* Accordion für Amazon‑Shorts */
document.querySelectorAll('.accordion-title-amz-shorts').forEach(title => {
    title.addEventListener('click', () => {
        const content = title.nextElementSibling;
        const isOpen = (content.style.display === 'block');
        document.querySelectorAll('.accordion-content-amz-shorts').forEach(item => {
            item.style.display = 'none';
        });
        content.style.display = isOpen ? 'none' : 'block';
    });
});

/* Tab-Navigation */
function openTab(event, tabId) {
    document.querySelectorAll('.tab-content').forEach(tab => tab.classList.remove('active'));
    document.querySelectorAll('.tab-link').forEach(link => link.classList.remove('active'));
    document.getElementById(tabId).classList.add('active');
    event.currentTarget.classList.add('active');
}
	document.addEventListener('DOMContentLoaded', function() {
});

/* Shortcode-Kopierfunktion */
function copyToClipboard(element) {
	const text = element.getAttribute('data-copy');
	navigator.clipboard.writeText(text).then(() => {
		const originalText = element.innerHTML;
		element.innerHTML = 'wurde kopiert!';		
		setTimeout(() => {
			element.innerHTML = originalText;
		}, 1500);
	}).catch(err => {
		console.error('Fehler beim Kopieren: ', err);
	});
}

/* AJAX-Script für Datenbankbereinigung */
jQuery(document).ready(function($) {
    $('#delete_old_data_button').on('click', function() {
        var period = $('#delete_period').val(); // Zeitraum abrufen
        var confirmMessage = backendSettings.deleteConfirmMessage;
        confirmMessage = confirmMessage.replace("%s", period);
        if (!confirm(confirmMessage)) {
            return;
        }
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            dataType: 'json',
            data: {
                action: 'delete_old_data',
                delete_period: period,
                nonce: backendSettings.deleteDataNonce
            },
            success: function(response) {
                if (response.success) {
                    $('#delete_old_data_message').html(
                        '<div class="notice notice-success is-dismissible"><p>' + response.data.message + '</p></div>'
                    );
                } else {
                    $('#delete_old_data_message').html(
                        '<div class="notice notice-error is-dismissible"><p>' + response.data.message + '</p></div>'
                    );
                }
            },
            error: function() {
                $('#delete_old_data_message').html(
                    '<div class="notice notice-error is-dismissible"><p>' + backendSettings.ajaxError + '</p></div>'
                );
            }
        });
    });
});

/* AJAX-Script für den "Amazon‑Preise manuell aktualisieren"-Button */
jQuery(document).ready(function($) {
    $('#boxyfy-manual-update-button').on('click', function() {
        var button = $(this);
        var messageDiv = $('#boxyfy-update-message');
        button.prop('disabled', true).text(backendSettings.manualUpdateInProgress);
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                action: 'boxyfy_manual_update',
                nonce: backendSettings.manualUpdateNonce
            },
            success: function(response) {
                if (response.success) {
                    messageDiv.html('<div class="notice notice-success is-dismissible"><p>' + response.data.message + '</p></div>');
                } else {
                    messageDiv.html('<div class="notice notice-error is-dismissible"><p>' + response.data.message + '</p></div>');
                }
            },
            error: function() {
                messageDiv.html('<div class="notice notice-error is-dismissible"><p>' + backendSettings.updateError + '</p></div>');
            },
            complete: function() {
                button.prop('disabled', false).text(backendSettings.manualUpdateComplete);
            }
        });
    });
});

document.addEventListener('DOMContentLoaded', function () {
    const color1 = document.getElementById('boxyfy_ai_gradient_color1');
    const color2 = document.getElementById('boxyfy_ai_gradient_color2');
    const preview = document.getElementById('boxyfy-ai-gradient-preview');

    function updateGradientPreview() {
        const val1 = color1.value;
        const val2 = color2.value;
        preview.style.background = `linear-gradient(141deg, ${val1} 0%, ${val2} 100%)`;
    }

    // Initial anzeigen
    updateGradientPreview();

    // Beim Ändern live aktualisieren
    color1.addEventListener('input', updateGradientPreview);
    color2.addEventListener('input', updateGradientPreview);
});