jQuery(document).ready(function($) {
    // Handle scan all button
    $('#formshield-scan-all').on('click', function() {
        var $btn = $(this);
        var originalText = $btn.text();
        $btn.prop('disabled', true).text('Scanning...');
        
        $('#formshield-scan-status').html('<div class="scan-status">🔍 Scanning all pages for forms...</div>');
        
        // Real scan process via AJAX
        fetch(ajaxConfig.url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: new URLSearchParams({
                action: 'formsh_scan_forms',
                nonce: ajaxConfig.nonce
            })
        })
        .then(response => response.json())
        .then(result => {
            if (result.success) {
                $('#formshield-scan-status').html('<div class="scan-status">✅ Scan completed!</div>');
                $('<div class="notice notice-success"><p>Form scan completed successfully!</p></div>')
                    .insertAfter('#formshield-scan-status')
                    .delay(3000)
                    .fadeOut();
            } else {
                $('#formshield-scan-status').html('<div class="scan-status">❌ Scan failed: ' + (result.data || 'Unknown error') + '</div>');
            }
            $btn.prop('disabled', false).text(originalText);
        })
        .catch(error => {
            $('#formshield-scan-status').html('<div class="scan-status">❌ Scan error: ' + error.message + '</div>');
            $btn.prop('disabled', false).text(originalText);
        });
    });
    
    // Handle toggle switches
    $('.formshield-toggle').on('change', function() {
        var $toggle = $(this);
        var formId = $toggle.data('form-id');
        var isEnabled = $toggle.is(':checked');
        
        // Update status text
        var $status = $toggle.closest('tr').find('.status');
        if (isEnabled) {
            $status.text('Enabled').removeClass('disabled').addClass('enabled');
        } else {
            $status.text('Disabled').removeClass('enabled').addClass('disabled');
        }
        
        // Show notification
        var message = isEnabled ? 'Form protection enabled' : 'Form protection disabled';
        $('<div class="formshield-notification">' + message + '</div>')
            .appendTo('body')
            .delay(2000)
            .fadeOut();
    });
    
    // Handle bulk actions
    $('#formshield-bulk-actions').on('change', function() {
        var action = $(this).val();
        if (action) {
            var selectedForms = $('.form-checkbox:checked').length;
            if (selectedForms === 0) {
                alert('Please select at least one form.');
                $(this).val('');
                return;
            }
            
            if (confirm('Are you sure you want to ' + action + ' ' + selectedForms + ' form(s)?')) {
                // Process bulk action
                processBulkAction(action);
            }
            
            $(this).val('');
        }
    });
    
    function processBulkAction(action) {
        var selectedForms = [];
        $('.form-checkbox:checked').each(function() {
            selectedForms.push($(this).val());
        });
        
        // Show loading
        $('#formshield-scan-status').html('<div class="scan-status">Processing bulk action...</div>');
        
        // Real bulk action processing via AJAX
        fetch(ajaxConfig.url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: new URLSearchParams({
                action: 'formshield_bulk_action',
                bulk_action: selectedAction,
                selected_forms: selectedForms,
                nonce: ajaxConfig.nonce
            })
        })
        .then(response => response.json())
        .then(result => {
            if (result.success) {
                $('#formshield-scan-status').html('<div class="scan-status">✅ Bulk action completed!</div>');
                $('<div class="notice notice-success"><p>Bulk action completed successfully!</p></div>')
                    .insertAfter('#formshield-scan-status')
                    .delay(3000)
                    .fadeOut();
            } else {
                $('#formshield-scan-status').html('<div class="scan-status">❌ Bulk action failed: ' + (result.data || 'Unknown error') + '</div>');
            }
        })
        .catch(error => {
            $('#formshield-scan-status').html('<div class="scan-status">❌ Bulk action error: ' + error.message + '</div>');
        });
    }
});
