<?php
/**
 * Simple Snippet Saver - Direct save without any complications
 */

if (!defined('ABSPATH')) exit;

class PC_Simple_Snippet_Saver {
    
    public function __construct() {
        add_action('admin_menu', array($this, 'add_menu'));
        add_action('admin_post_pc_save_snippet', array($this, 'handle_save'));
    }
    
    public function add_menu() {
        add_menu_page(
            'Simple Snippets',
            'Simple Snippets',
            'manage_options',
            'pc-simple-snippets',
            array($this, 'render_page'),
            'dashicons-admin-generic',
            30
        );
    }
    
    public function render_page() {
        global $wpdb;
        $table_name = $wpdb->prefix . 'pc_simple_snippets';
        
        // Create table if needed
        $wpdb->query("CREATE TABLE IF NOT EXISTS $table_name (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            title varchar(255) NOT NULL,
            code longtext NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id)
        )");
        
        // Handle form submission
        if (isset($_POST['save_snippet']) && isset($_POST['title']) && isset($_POST['code'])) {
            $title = sanitize_text_field($_POST['title']);
            $code = $_POST['code'];
            
            if (!empty($title) && !empty($code)) {
                $result = $wpdb->insert($table_name, array(
                    'title' => $title,
                    'code' => $code
                ), array('%s', '%s'));
                
                if ($result) {
                    echo '<div class="notice notice-success"><p>✓ Snippet saved successfully! ID: ' . $wpdb->insert_id . '</p></div>';
                } else {
                    echo '<div class="notice notice-error"><p>✗ Failed to save. Error: ' . $wpdb->last_error . '</p></div>';
                }
            } else {
                echo '<div class="notice notice-error"><p>Please fill in all fields.</p></div>';
            }
        }
        
        // Show form
        echo '<div class="wrap">';
        echo '<h1>Simple Snippet Saver</h1>';
        echo '<form method="post">';
        echo '<table class="form-table">';
        echo '<tr><th>Title:</th><td><input type="text" name="title" size="50" required></td></tr>';
        echo '<tr><th>Code:</th><td><textarea name="code" rows="10" cols="80" required></textarea></td></tr>';
        echo '</table>';
        echo '<p><input type="submit" name="save_snippet" class="button button-primary" value="Save Snippet"></p>';
        echo '</form>';
        
        // Show existing snippets
        echo '<h2>Existing Snippets</h2>';
        $snippets = $wpdb->get_results("SELECT * FROM $table_name ORDER BY id DESC");
        
        if ($snippets) {
            echo '<table class="widefat striped">';
            echo '<thead><tr><th>ID</th><th>Title</th><th>Code Preview</th><th>Created</th></tr></thead>';
            echo '<tbody>';
            foreach ($snippets as $snippet) {
                echo '<tr>';
                echo '<td>' . $snippet->id . '</td>';
                echo '<td>' . esc_html($snippet->title) . '</td>';
                echo '<td><code>' . esc_html(substr($snippet->code, 0, 100)) . (strlen($snippet->code) > 100 ? '...' : '') . '</code></td>';
                echo '<td>' . $snippet->created_at . '</td>';
                echo '</tr>';
            }
            echo '</tbody></table>';
        } else {
            echo '<p>No snippets yet.</p>';
        }
        
        echo '</div>';
    }
}

// Initialize
new PC_Simple_Snippet_Saver();
