(function($) { // Hjälpfunktion: hämta cookie function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(';').shift(); return null; } $(document).ready(function() { // Visa popup endast om cookien är satt if (!getCookie('sexleksak_popup')) { return; } // Radera cookien så popup inte visas igen under 24h document.cookie = 'sexleksak_popup=; Max-Age=0; path=/'; // Konstruktion av popup med SweetAlert2 Swal.fire({ title: 'Lämna feedback', html: '
' + '' + '' + '' + '' + '' + '' + '' + '' + '
', showCloseButton: true, showCancelButton: true, confirmButtonText: 'Skicka', cancelButtonText: 'Avbryt', focusConfirm: false, preConfirm: () => { const email = $('#spp-email').val().trim(); const name = $('#spp-name').val().trim(); const rating = $('#spp-rating').val(); const comment = $('#spp-comment').val().trim(); if (!email || !rating) { Swal.showValidationMessage('Fyll i både e-post och ett betyg.'); return false; } // Förbered URL-encoded-body för admin-ajax.php const params = new URLSearchParams({ action: 'spp_send_feedback', email: email, name: name, rating: rating, comment: comment }); return fetch(SPP_Ajax.ajax_url, { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, body: params.toString() }) .then(response => { console.log('SPP feedback status:', response.status); if (!response.ok) { return response.json().then(err => { throw new Error(err.data || 'Serverfel'); }); } return response.json(); }) .then(json => { if (!json.success) { throw new Error(json.data || 'Okänt fel'); } return json; }) .catch(error => { Swal.showValidationMessage(`Request failed: ${error}`); }); } }).then((result) => { if (result.isConfirmed) { Swal.fire('Tack!', 'Din feedback har skickats.', 'success'); } }); }); })(jQuery);