/** * Admin JavaScript for Studiou WC Product Category Manager */ (function($) { 'use strict'; // Initialize admin scripts $(document).ready(function() { // Debug: Log that script is loaded console.log('STUDIOU WC: Admin script loaded'); console.log('STUDIOU WC: studiouWcpcm object:', studiouWcpcm); // Import form handling if ($('#studiou-wcpcm-import-form').length) { console.log('STUDIOU WC: Import form found'); initImportForm(); } // Export form handling if ($('#studiou-wcpcm-export-form').length) { console.log('STUDIOU WC: Export form found'); initExportForm(); } // Move products form handling if ($('#studiou-wcpcm-move-form').length) { console.log('STUDIOU WC: Move form found'); initMoveForm(); } else { console.log('STUDIOU WC: Move form NOT found'); } // Batch description form handling if ($('#studiou-wcpcm-batch-description-form').length) { console.log('STUDIOU WC: Batch description form found'); initBatchDescriptionForm(); } // Sample CSV download handler if ($('#studiou-wcpcm-download-sample').length) { initSampleDownload(); } }); /** * Initialize import form */ function initImportForm() { $('#studiou-wcpcm-import-form').on('submit', function(e) { e.preventDefault(); // Show spinner var $submitBtn = $(this).find('#studiou-wcpcm-import-button'); var $spinner = $(this).find('.spinner'); $submitBtn.prop('disabled', true); $spinner.css('visibility', 'visible'); // Clear previous notices $('.studiou-wcpcm-notice-area').empty(); // Create form data var formData = new FormData(this); formData.append('action', 'studiou_wcpcm_import'); formData.append('nonce', studiouWcpcm.nonce); // Send AJAX request $.ajax({ url: studiouWcpcm.ajaxUrl, type: 'POST', data: formData, processData: false, contentType: false, success: function(response) { if (response.success) { showNotice('success', studiouWcpcm.i18n.importSuccess); // Show results $('#studiou-wcpcm-import-results').text(response.data.message); $('.studiou-wcpcm-import-results').show(); } else { showNotice('error', response.data.message); } }, error: function() { showNotice('error', studiouWcpcm.i18n.importError); }, complete: function() { // Hide spinner $submitBtn.prop('disabled', false); $spinner.css('visibility', 'hidden'); } }); }); } /** * Initialize export form */ function initExportForm() { $('#studiou-wcpcm-export-form').on('submit', function(e) { e.preventDefault(); // Show spinner var $submitBtn = $(this).find('#studiou-wcpcm-export-button'); var $spinner = $(this).find('.spinner'); $submitBtn.prop('disabled', true); $spinner.css('visibility', 'visible'); // Clear previous notices $('.studiou-wcpcm-notice-area').empty(); // Send AJAX request $.ajax({ url: studiouWcpcm.ajaxUrl, type: 'POST', data: { action: 'studiou_wcpcm_export', nonce: studiouWcpcm.nonce }, success: function(response) { if (response.success) { showNotice('success', studiouWcpcm.i18n.exportSuccess); // Show download link $('#studiou-wcpcm-export-message').text(response.data.message); $('#studiou-wcpcm-download-export').attr('href', response.data.download_url); $('.studiou-wcpcm-export-results').show(); } else { showNotice('error', response.data.message); } }, error: function() { showNotice('error', studiouWcpcm.i18n.exportError); }, complete: function() { // Hide spinner $submitBtn.prop('disabled', false); $spinner.css('visibility', 'hidden'); } }); }); } /** * Initialize move products form */ function initMoveForm() { console.log('STUDIOU WC: Initializing move form handlers'); // Add debug button click handler first $('#studiou-wcpcm-move-button').on('click', function(e) { console.log('STUDIOU WC: Move button clicked directly'); }); $('#studiou-wcpcm-move-form').on('submit', function(e) { e.preventDefault(); // Debug: Log form submission console.log('STUDIOU WC: Move form submitted'); // Validate that different categories are selected var sourceCategory = $('#source_category').val(); var targetCategory = $('#target_category').val(); console.log('STUDIOU WC: Source category:', sourceCategory, 'Target category:', targetCategory); if (!sourceCategory || !targetCategory) { console.log('STUDIOU WC: Missing category selection'); showNotice('error', 'Please select both source and target categories'); return; } if (sourceCategory === targetCategory) { console.log('STUDIOU WC: Same category selected'); showNotice('error', studiouWcpcm.i18n.selectDifferentCategories); return; } // Show spinner var $submitBtn = $(this).find('#studiou-wcpcm-move-button'); var $spinner = $(this).find('.spinner'); $submitBtn.prop('disabled', true); $spinner.css('visibility', 'visible'); // Clear previous notices $('.studiou-wcpcm-notice-area').empty(); // Debug: Log AJAX request details console.log('STUDIOU WC: Sending AJAX request to:', studiouWcpcm.ajaxUrl); console.log('STUDIOU WC: Nonce:', studiouWcpcm.nonce); // Send AJAX request $.ajax({ url: studiouWcpcm.ajaxUrl, type: 'POST', data: { action: 'studiou_wcpcm_move_products', source_category: sourceCategory, target_category: targetCategory, nonce: studiouWcpcm.nonce }, success: function(response) { console.log('STUDIOU WC: AJAX response:', response); if (response.success) { showNotice('success', studiouWcpcm.i18n.moveSuccess); // Show results $('#studiou-wcpcm-move-message').html('
' + response.data.message + '
'); $('.studiou-wcpcm-move-results').show(); // Refresh the page to update category counts setTimeout(function() { location.reload(); }, 3000); } else { showNotice('error', response.data.message); } }, error: function(xhr, status, error) { console.error('STUDIOU WC: AJAX error:', status, error); console.error('STUDIOU WC: Response text:', xhr.responseText); showNotice('error', studiouWcpcm.i18n.moveError + ': ' + error); }, complete: function() { // Hide spinner $submitBtn.prop('disabled', false); $spinner.css('visibility', 'hidden'); } }); }); } /** * Initialize batch description form */ function initBatchDescriptionForm() { console.log('STUDIOU WC: Initializing batch description form handlers'); // Check all button handler $('#studiou-wcpcm-check-all').on('click', function(e) { e.preventDefault(); console.log('STUDIOU WC: Check all button clicked'); $('#categories_list input[type="checkbox"]').prop('checked', true); }); // Uncheck all button handler $('#studiou-wcpcm-uncheck-all').on('click', function(e) { e.preventDefault(); console.log('STUDIOU WC: Uncheck all button clicked'); $('#categories_list input[type="checkbox"]').prop('checked', false); }); // Form submit handler $('#studiou-wcpcm-batch-description-form').on('submit', function(e) { e.preventDefault(); console.log('STUDIOU WC: Batch description form submitted'); // Get selected categories var selectedCategories = []; $('#categories_list input[type="checkbox"]:checked').each(function() { selectedCategories.push($(this).val()); }); var description = $('#category_description').val(); console.log('STUDIOU WC: Selected categories:', selectedCategories); console.log('STUDIOU WC: Description:', description); // Validate input if (selectedCategories.length === 0) { console.log('STUDIOU WC: No categories selected'); showNotice('error', 'Please select at least one category'); return; } // Show spinner var $submitBtn = $(this).find('#studiou-wcpcm-batch-description-button'); var $spinner = $(this).find('.spinner'); $submitBtn.prop('disabled', true); $spinner.css('visibility', 'visible'); // Clear previous notices $('.studiou-wcpcm-notice-area').empty(); // Debug: Log AJAX request details console.log('STUDIOU WC: Sending batch description AJAX request to:', studiouWcpcm.ajaxUrl); console.log('STUDIOU WC: Nonce:', studiouWcpcm.nonce); // Send AJAX request $.ajax({ url: studiouWcpcm.ajaxUrl, type: 'POST', data: { action: 'studiou_wcpcm_batch_description', selected_categories: selectedCategories, description: description, nonce: studiouWcpcm.nonce }, success: function(response) { console.log('STUDIOU WC: Batch description AJAX response:', response); if (response.success) { showNotice('success', studiouWcpcm.i18n.batchDescriptionSuccess || 'Batch description operation successful'); // Show results $('#studiou-wcpcm-batch-description-message').html('' + response.data.message + '
'); $('.studiou-wcpcm-batch-description-results').show(); } else { showNotice('error', response.data.message); } }, error: function(xhr, status, error) { console.error('STUDIOU WC: Batch description AJAX error:', status, error); console.error('STUDIOU WC: Response text:', xhr.responseText); showNotice('error', (studiouWcpcm.i18n.batchDescriptionError || 'Batch description operation error') + ': ' + error); }, complete: function() { // Hide spinner $submitBtn.prop('disabled', false); $spinner.css('visibility', 'hidden'); } }); }); } /** * Initialize sample CSV download */ function initSampleDownload() { $('#studiou-wcpcm-download-sample').on('click', function(e) { e.preventDefault(); // Sample CSV content var csvContent = 'name,url-name,parent-name,description,display-type,visibility,protected-passwords,operation\n' + '"Clothing","clothing","","Top quality clothing","default","public","","A"\n' + '"Men","men","Clothing","Men\'s clothing","products","public","","A"\n' + '"Women","women","Clothing","Women\'s clothing","products","protected","password123","A"\n' + '"Kids","kids","Clothing","Kids clothing","products","public","","U"\n' + '"Accessories","accessories","","All accessories","default","public","","D"'; // Create download link var blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); var url = URL.createObjectURL(blob); var link = document.createElement('a'); link.setAttribute('href', url); link.setAttribute('download', 'sample-product-categories.csv'); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); }); } /** * Show notice message * * @param {string} type Notice type (success, error) * @param {string} message Notice message */ function showNotice(type, message) { var $notice = $('').text(message); $('.studiou-wcpcm-notice-area').append($notice); // Scroll to notice $('html, body').animate({ scrollTop: $('.studiou-wcpcm-notice-area').offset().top - 50 }, 500); } // Additional debug: Check if elements exist after DOM load $(window).on('load', function() { console.log('STUDIOU WC: Window loaded'); console.log('STUDIOU WC: Move form exists:', $('#studiou-wcpcm-move-form').length > 0); console.log('STUDIOU WC: Move button exists:', $('#studiou-wcpcm-move-button').length > 0); console.log('STUDIOU WC: Source select exists:', $('#source_category').length > 0); console.log('STUDIOU WC: Target select exists:', $('#target_category').length > 0); console.log('STUDIOU WC: Batch description form exists:', $('#studiou-wcpcm-batch-description-form').length > 0); console.log('STUDIOU WC: Batch description button exists:', $('#studiou-wcpcm-batch-description-button').length > 0); }); })(jQuery);