<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : function-testing.php
 * @location     : /modules/help/function-testing.php
 * @type         : Debug Tool
 * @scope        : Module Only
 * @allowed      : Function testing, development tools, debugging functions
 * @reusability  : Single Use
 * @notes        : Function testing interface for help module (dev-only)
 */

// Note: AJAX handlers are now registered in core/admin/ajax.php
// This file only contains the HTML interface for testing


?>
<div class="wrap">
    <h1>Test Page</h1>
    <p>This is a test page</p>
    
    
    <h2>AJAX Text Input Test</h2>
    <form id="ajax-text-form">
        <input type="hidden" id="ajax_text_nonce" name="ajax_text_nonce" value="<?php echo wp_create_nonce('ajax_text_action'); ?>" />
        
        <table class="form-table">
            <tr>
                <th scope="row">
                    <label for="ajax_text_input">Test Text Input</label>
                </th>
                <td>
                    <input type="text" id="ajax_text_input" name="ajax_text_input" value="<?php echo esc_attr(get_option('igny8_ajax_test_text', '')); ?>" class="regular-text" placeholder="Enter some text to save via AJAX" />
                    <p class="description">This text will be saved using AJAX without page reload</p>
                </td>
            </tr>
        </table>
        
        <button type="button" id="ajax-save-btn" class="button button-primary">Save Text via AJAX</button>
        <button type="button" id="ajax-test-btn" class="button button-secondary">Test AJAX</button>
        <span id="ajax-status" style="margin-left: 10px;"></span>
    </form>
    
    <hr>
    
    <script type="text/javascript">
    jQuery(document).ready(function($){
        // Test AJAX button
        $('#ajax-test-btn').click(function(e){
            e.preventDefault();
            $('#ajax-status').html('<span style="color: blue;">Testing AJAX...</span>');

            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'igny8_test_ajax'
                },
                success: function(response){
                    if (response.success) {
                        $('#ajax-status').html('<span style="color: green;">✓ ' + response.data + '</span>');
                    } else {
                        $('#ajax-status').html('<span style="color: red;">✗ Test failed: ' + response.data + '</span>');
                    }
                },
                error: function(){
                    $('#ajax-status').html('<span style="color: red;">✗ AJAX connection failed</span>');
                }
            });
        });

        // Save AJAX button
        $('#ajax-save-btn').click(function(e){
            e.preventDefault();

            var textValue = $('#ajax_text_input').val().trim();

            if (!textValue) {
                $('#ajax-status').html('<span style="color: red;">Please enter some text</span>');
                return;
            }

            // Show loading state
            $('#ajax-status').html('<span style="color: blue;">Saving...</span>');
            $('#ajax-save-btn').prop('disabled', true);

            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'igny8_save_ajax_text',
                    ajax_text_input: textValue,
                    ajax_text_nonce: $('#ajax_text_nonce').val()
                },
                success: function(response){
                    if (response.success) {
                        $('#ajax-status').html('<span style="color: green;">✓ ' + response.data + '</span>');
                        // Update the input field to show the saved value
                        $('#ajax_text_input').val(textValue);
                    } else {
                        $('#ajax-status').html('<span style="color: red;">✗ Error: ' + response.data + '</span>');
                    }
                },
                error: function(xhr, status, error){
                    $('#ajax-status').html('<span style="color: red;">✗ AJAX Error: ' + error + '</span>');
                },
                complete: function(){
                    $('#ajax-save-btn').prop('disabled', false);
                }
            });
        });
    });
    </script>
</div>

<?php
// Note: This file is included by help.php, so no need to capture content or include layout
// The content will be captured by the parent help.php file
?>

