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

// Get the URL for this test
$test_url = admin_url('admin-post.php?action=pc_hfap_save_snippet');

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

// Check if form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo '<h2>Form Submitted</h2>';
    
    // Check if submit_snippet is set
    if (isset($_POST['submit_snippet'])) {
        echo '<p style="color: green;">✓ submit_snippet is set</p>';
        
        // Check if nonce is set and valid
        if (isset($_POST['pc_hfap_nonce'])) {
            if (wp_verify_nonce($_POST['pc_hfap_nonce'], 'pc_hfap_save_snippet')) {
                echo '<p style="color: green;">✓ Nonce is valid</p>';
                
                // Try to save directly
                require_once dirname(__FILE__) . '/includes/class-database.php';
                require_once dirname(__FILE__) . '/includes/class-snippet.php';
                
                $data = array(
                    'title' => sanitize_text_field($_POST['pc_hfap_title']),
                    'location' => in_array($_POST['pc_hfap_location'], array('header', 'footer', 'body')) ? $_POST['pc_hfap_location'] : 'header',
                    'code' => wp_unslash($_POST['pc_hfap_code'])
                );
                
                echo '<p>Saving snippet...</p>';
                
                $snippet = new PC_HFAP_Snippet($data);
                $result = $snippet->save();
                
                if ($result) {
                    echo '<p style="color: green; font-size: 24px;">✓ SUCCESS! Snippet saved with ID: ' . $result . '</p>';
                    echo '<p><a href="' . admin_url('admin.php?page=pc-hfap-snippets') . '">View all snippets</a></p>';
                } else {
                    echo '<p style="color: red;">✗ FAILED to save snippet</p>';
                    global $wpdb;
                    echo '<p>Last error: ' . $wpdb->last_error . '</p>';
                }
            } else {
                echo '<p style="color: red;">✗ Nonce is invalid</p>';
            }
        } else {
            echo '<p style="color: red;">✗ Nonce not set</p>';
        }
    } else {
        echo '<p style="color: red;">✗ submit_snippet not set</p>';
    }
    
    echo '<h3>POST Data:</h3>';
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
} else {
    echo '<h2>No POST Request</h2>';
    echo '<p>To test, submit the form or use this test form:</p>';
    
    echo '<form method="post" action="' . $test_url . '" style="background: #f0f0f0; padding: 20px; border: 1px solid #ccc;">';
    echo '<p><label>Title: <input type="text" name="pc_hfap_title" value="Test ' . date('Y-m-d H:i:s') . '" size="50"></label></p>';
    echo '<p><label>Location: <select name="pc_hfap_location">';
    echo '<option value="header">Header</option>';
    echo '<option value="body">Body</option>';
    echo '<option value="footer">Footer</option>';
    echo '</select></label></p>';
    echo '<p><label>Code:<br><textarea name="pc_hfap_code" rows="5" cols="80"><!-- Test code --></textarea></label></p>';
    echo '<input type="hidden" name="submit_snippet" value="1">';
    wp_nonce_field('pc_hfap_save_snippet', 'pc_hfap_nonce');
    echo '<p><button type="submit" style="background: #0073aa; color: white; padding: 10px 20px; border: none; cursor: pointer;">Save Snippet</button></p>';
    echo '</form>';
}
