<?php
/**
 * Simple test to verify form submission
 */

// Get the URL for this test
$test_url = admin_url('admin.php?page=pc-hfap-add-snippet');

echo '<h1>Form Submission Test</h1>';
echo '<p>Test URL: ' . $test_url . '</p>';

// Check if form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo '<h2>Form Submitted via POST</h2>';
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
    
    // Check if submit_snippet is set
    if (isset($_POST['submit_snippet'])) {
        echo '<p style="color: green;">✓ submit_snippet is set</p>';
    } else {
        echo '<p style="color: red;">✗ submit_snippet is NOT set</p>';
    }
    
    // Check if nonce is set
    if (isset($_POST['pc_hfap_nonce'])) {
        echo '<p style="color: green;">✓ nonce is set</p>';
        
        // Verify nonce
        if (wp_verify_nonce($_POST['pc_hfap_nonce'], 'pc_hfap_save_snippet')) {
            echo '<p style="color: green;">✓ nonce is valid</p>';
        } else {
            echo '<p style="color: red;">✗ nonce is invalid</p>';
        }
    } else {
        echo '<p style="color: red;">✗ nonce is NOT set</p>';
    }
    
    // Check required fields
    echo '<h3>Required Fields:</h3>';
    if (!empty($_POST['pc_hfap_title'])) {
        echo '<p style="color: green;">✓ Title: ' . esc_html($_POST['pc_hfap_title']) . '</p>';
    } else {
        echo '<p style="color: red;">✗ Title is empty</p>';
    }
    
    if (!empty($_POST['pc_hfap_location'])) {
        echo '<p style="color: green;">✓ Location: ' . esc_html($_POST['pc_hfap_location']) . '</p>';
    } else {
        echo '<p style="color: red;">✗ Location is empty</p>';
    }
    
    if (!empty($_POST['pc_hfap_code'])) {
        echo '<p style="color: green;">✓ Code length: ' . strlen($_POST['pc_hfap_code']) . ' chars</p>';
    } else {
        echo '<p style="color: red;">✗ Code is empty</p>';
    }
} else {
    echo '<h2>No POST request detected</h2>';
    echo '<p>This page is for testing form submission. Submit the form to see results.</p>';
    
    echo '<h3>Test Instructions:</h3>';
    echo '<ol>';
    echo '<li>Go to Headers & Footers → Add New</li>';
    echo '<li>Fill in the form</li>';
    echo '<li>Click "Save Snippet"</li>';
    echo '<li>Check this page for results</li>';
    echo '</ol>';
    
    echo '<h3>Or submit a test form:</h3>';
    echo '<form method="post" action="' . $test_url . '">';
    echo '<input type="text" name="pc_hfap_title" value="Test Snippet" required>';
    echo '<select name="pc_hfap_location">';
    echo '<option value="header">Header</option>';
    echo '<option value="footer">Footer</option>';
    echo '</select>';
    echo '<textarea name="pc_hfap_code">Test code</textarea>';
    echo '<input type="hidden" name="submit_snippet" value="1">';
    wp_nonce_field('pc_hfap_save_snippet', 'pc_hfap_nonce');
    echo '<button type="submit">Submit Test</button>';
    echo '</form>';
}
