if (typeof jQuery !== 'undefined') {
  jQuery(function() {
    // Toggle functionality is handled in protected-forms-table.php inline JavaScript
    
    // Function to show admin notices
    function showAdminNotice(message, type) {
        var noticeClass = 'notice notice-' + (type === 'success' ? 'success' : 'error');
        var noticeHtml = '<div class="' + noticeClass + ' is-dismissible"><p>' + message + '</p></div>';
        
        // Remove existing notices
        jQuery('.notice').remove();
        
        // Add new notice at the top of the page
        jQuery('.plugin-dashboard').first().prepend(noticeHtml);
        
        // Auto-dismiss after 5 seconds
        setTimeout(function() {
            jQuery('.notice').fadeOut();
        }, 5000);
    }

    // Handle rescan forms button
    jQuery('#rescan-forms-btn').on('click', function() {
        var $btn = jQuery(this);
        var originalText = $btn.text();
        $btn.prop('disabled', true).text('Rescanning...');
        
        // Show results area
        jQuery('#scan-results').show();
        jQuery('.scan-status').text('🔄 Rescanning all forms...');
        jQuery('.scan-details').text('Please wait while we rescan your site for forms...');
        
        // Send AJAX request to rescan forms
        jQuery.ajax({
            url: ajaxConfig.url,
            type: 'POST',
            data: {
                action: 'formsh_rescan_forms',
                nonce: ajaxConfig.rescanNonce
            },
            success: function(response) {
                if (response.success) {
                    jQuery('.scan-status').text('✅ Rescan completed!');
                    
                    // Show detailed results with new forms indicator
                    var detailsHtml = response.data.message + ':<br><br>';
                    
                    // Show current forms
                    if (response.data.detailed_results && response.data.detailed_results.length > 0) {
                        detailsHtml += '<div class="scan-detailed-results">';
                        detailsHtml += '<h4>Текущи форми:</h4>';
                        response.data.detailed_results.forEach(function(form) {
                            detailsHtml += '<div class="scan-form-result">';
                            if (form.is_new) {
                                detailsHtml += '<span class="new-form-indicator">🆕 </span>';
                            }
                            detailsHtml += '<span class="form-name">' + form.name + '</span>';
                            detailsHtml += '<span class="form-type">(' + form.type + ')</span>';
                            detailsHtml += '<span class="form-status ' + form.status + '">';
                            detailsHtml += form.icon + ' ' + form.message;
                            detailsHtml += '</span>';
                            detailsHtml += '</div>';
                        });
                        detailsHtml += '</div>';
                    }
                    
                    // Show deleted forms if any
                    if (response.data.deleted_forms && response.data.deleted_forms.length > 0) {
                        detailsHtml += '<div class="scan-deleted-results">';
                        detailsHtml += '<h4>Изтрити форми:</h4>';
                        response.data.deleted_forms.forEach(function(form) {
                            detailsHtml += '<div class="scan-form-result deleted-form">';
                            detailsHtml += '<span class="deleted-form-indicator">🗑️ </span>';
                            detailsHtml += '<span class="form-name">' + form.name + '</span>';
                            detailsHtml += '<span class="form-type">(' + form.type + ')</span>';
                            detailsHtml += '<span class="form-status deleted">Изтрита</span>';
                            detailsHtml += '</div>';
                        });
                        detailsHtml += '</div>';
                    }
                    
                    jQuery('.scan-details').html(detailsHtml);
                    
                    // Reload page after 5 seconds to show detailed results
                    setTimeout(function() {
                        location.reload();
                    }, 5000);
                } else {
                    jQuery('.scan-status').text('❌ Rescan failed');
                    jQuery('.scan-details').text('Error: ' + (response.data || 'Unknown error'));
                }
            },
            error: function() {
                jQuery('.scan-status').text('❌ Rescan failed');
                jQuery('.scan-details').text('Network error. Please try again.');
            },
            complete: function() {
                $btn.prop('disabled', false).text(originalText);
            }
        });
    });
  });
}