Wheel_of_Fortune_Plugin/admin/js/admin.js

285 lines
11 KiB
JavaScript

jQuery(document).ready(function($) {
'use strict';
// Only run on our plugin's admin pages
if (!$('.wheel-admin-page').length) {
return;
}
// Tab navigation
$('.nav-tab').on('click', function(e) {
e.preventDefault();
$('.nav-tab').removeClass('nav-tab-active');
$(this).addClass('nav-tab-active');
$('.wheel-tab-content').hide();
$('#' + $(this).data('target')).show();
});
// --- Modal Logic ---
var modal = $('#edit-prize-modal');
var span = modal.find('.close');
// Close modal
span.on('click', function() {
modal.hide();
});
$(window).on('click', function(event) {
if ($(event.target).is(modal)) {
modal.hide();
}
});
// --- Add New Prize ---
$('.add-new-prize').on('click', function(e) {
e.preventDefault();
// Clear form for new prize
$('#edit-prize-form')[0].reset();
$('#edit-prize-id').val(''); // Clear prize ID for new prize
// Get wheel_id from the modal's data attribute
var wheelId = $('#edit-prize-modal').data('wheel-id');
$('#edit-wheel-id').val(wheelId);
console.log('Opening modal for new prize with wheel_id:', wheelId);
modal.show();
});
// --- Edit Prize ---
$('.edit-prize').on('click', function(e) {
e.preventDefault();
var prizeId = $(this).data('id');
// AJAX call to get prize details
$.post(ajaxurl, {
action: 'wheel_get_prize_details',
_ajax_nonce: wheel_admin_nonce._ajax_nonce,
prize_id: prizeId
}, function(response) {
if (response.success) {
var prize = response.data;
// Populate form
$('#edit-prize-id').val(prize.id);
$('#edit-wheel-id').val(prize.wheel_id); // wheel_id je že v formi
$('#edit-prize-name').val(prize.name);
$('#edit-prize-description').val(prize.description);
$('#edit-prize-probability').val(prize.probability);
$('#edit-prize-is-active').prop('checked', parseInt(prize.is_active, 10));
$('#edit-prize-redemption-code').val(prize.redemption_code);
$('#edit-prize-is-discount').prop('checked', parseInt(prize.is_discount, 10));
$('#edit-prize-discount-value').val(prize.discount_value);
$('#edit-prize-email-subject').val(prize.email_subject);
$('#edit-prize-email-template').val(prize.email_template);
modal.show();
} else {
alert(response.data.message || 'An error occurred.');
}
}).fail(function() {
alert('An error occurred while loading prize details.');
});
});
// --- Save Prize (via AJAX) ---
$('#edit-prize-form').on('submit', function(e) {
e.preventDefault();
// Pripravimo podatke iz forme
const formData = {
action: 'wheel_save_prize',
_ajax_nonce: wheel_admin_nonce._ajax_nonce,
wheel_id: $('#edit-wheel-id').val(), // **KLJUČNO: pošljemo wheel_id**
prize_id: $('#edit-prize-id').val(),
name: $('#edit-prize-name').val(),
description: $('#edit-prize-description').val(),
probability: $('#edit-prize-probability').val(),
is_active: $('#edit-prize-is-active').is(':checked') ? 1 : 0,
redemption_code: $('#edit-prize-redemption-code').val(),
is_discount: $('#edit-prize-is-discount').is(':checked') ? 1 : 0,
discount_value: $('#edit-prize-discount-value').val(),
email_subject: $('#edit-prize-email-subject').val(),
email_template: $('#edit-prize-email-template').val(),
};
// Debug: preveri wheel_id
console.log('Wheel ID from form:', formData.wheel_id);
console.log('Form data:', formData);
// Validate form fields
if (!formData.name) {
alert('Prize name is required.');
return;
}
if (isNaN(parseFloat(formData.probability)) || formData.probability < 0 || formData.probability > 1) {
alert('Probability must be a number between 0 and 1.');
return;
}
if (!formData.wheel_id || formData.wheel_id == 0) {
alert('Wheel ID is missing. Please refresh the page and try again.');
return;
}
// AJAX call to save the prize
$.post(ajaxurl, formData, function(response) {
console.log('Server response:', response);
if (response.success) {
alert(response.data.message);
modal.hide();
location.reload(); // Refresh page to see changes
} else {
alert(response.data.message || 'An error occurred while saving.');
}
}).fail(function(xhr, status, error) {
console.error('AJAX error:', {xhr: xhr, status: status, error: error});
alert('A critical error occurred. Please try again.');
});
});
// --- Delete Prize ---
$('.delete-prize').on('click', function(e) {
e.preventDefault();
if (confirm('Are you sure you want to delete this prize?')) {
var prizeId = $(this).data('id');
$.post(ajaxurl, {
action: 'wheel_delete_prize',
_ajax_nonce: wheel_admin_nonce._ajax_nonce,
prize_id: prizeId
}, function(response) {
if (response.success) {
alert(response.data.message);
location.reload();
} else {
alert(response.data.message || 'An error occurred.');
}
});
}
});
// --- SETTINGS PAGE - TEST EMAIL ---
if ($('#test-email-button').length) {
$('#test-email-button').on('click', function(e) {
e.preventDefault();
const button = $(this);
const resultDiv = $('#test-email-result');
const recipientEmail = $('#test-email-recipient').val();
const spinner = button.siblings('.spinner');
if (!recipientEmail) {
alert(wheel_admin_i18n.enter_recipient_email || 'Please enter a recipient email address.');
return;
}
button.prop('disabled', true);
spinner.addClass('is-active');
resultDiv.slideUp().removeClass('notice notice-success notice-error is-dismissible').empty();
// Collect all settings from the form to test unsaved values
const data = {
action: 'wheel_test_email',
_ajax_nonce: wheel_admin_nonce._ajax_nonce,
recipient_email: recipientEmail,
smtp_enabled: $('#wheel_smtp_enabled').is(':checked') ? '1' : '0',
smtp_host: $('#wheel_smtp_host').val(),
smtp_port: $('#wheel_smtp_port').val(),
smtp_encryption: $('#wheel_smtp_encryption').val(),
smtp_username: $('#wheel_smtp_username').val(),
smtp_password: $('#wheel_smtp_password').val(),
from_name: $('#wheel_email_from_name').val(),
from_email: $('#wheel_email_from_email').val(),
};
$.ajax({
url: ajaxurl,
method: 'POST',
data: data,
success: function(response) {
if (response.success) {
resultDiv.addClass('notice notice-success is-dismissible').html('<p>' + response.data.message + '</p>').slideDown();
} else {
const errorMessage = response.data.message || (wheel_admin_i18n.error_sending_email || 'An unknown error occurred.');
resultDiv.addClass('notice notice-error is-dismissible').html('<p>' + errorMessage + '</p>').slideDown();
}
},
error: function(xhr) {
let errorMessage = wheel_admin_i18n.error_sending_email || 'A communication error occurred with the server.';
if(xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) {
errorMessage = xhr.responseJSON.data.message;
}
resultDiv.addClass('notice notice-error is-dismissible').html('<p>' + errorMessage + '</p>').slideDown();
},
complete: function() {
button.prop('disabled', false);
spinner.removeClass('is-active');
}
});
});
}
// Dodajanje/brisanje produkta za spine
$('#add-product-button').on('click', function() {
const button = $(this);
const productId = $('#new-product-id').val();
const spins = $('#new-product-spins').val();
const wheelId = button.data('wheel-id');
if (!productId || !spins) {
alert('Prosim vnesite ID produkta in število spinov.');
return;
}
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'ajax_update_wheel_product_spins',
_ajax_nonce: wheel_admin_nonce._ajax_nonce,
wheel_id: wheelId,
product_id: productId,
spins: spins
},
success: function(response) {
if(response.success) {
location.reload();
} else {
alert('Napaka: ' + response.data.message);
}
},
error: function() {
alert('Prišlo je do kritične napake.');
}
});
});
$('.delete-product-button').on('click', function() {
if(!confirm('Ste prepričani?')) return;
const button = $(this);
const productId = button.data('product-id');
const wheelId = button.data('wheel-id');
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'ajax_delete_wheel_product',
_ajax_nonce: wheel_admin_nonce._ajax_nonce,
wheel_id: wheelId,
product_id: productId
},
success: function(response) {
if(response.success) {
location.reload();
} else {
alert('Napaka: ' + response.data.message);
}
},
error: function() {
alert('Prišlo je do kritične napake.');
}
});
});
});