<?php
/*
Plugin Name: WP Ingame Shop Pro
Plugin URI:https://git.viper.ipv64.net/M_Viper/WP-Ingame-Shop-Pro
Description: Vollautomatischer Shop mit Warenkorb (kein echtgeld Handel)
Version: 2.1.3
Author: M_Viper
Author URI: https://m-viper.de
Requires at least: 6.9.1
Tested up to: 6.9.1
PHP Version: 7.4
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wp-ingame-shop-pro
Tags: shop, items, minecraft, coupons, deals, categories
Support: [Discord Support](https://discord.com/invite/FdRs4BRd8D)
Support: [Telegram Support](https://t.me/M_Viper04)
*/

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

// Plugin Constants
define('WIS_VERSION', '2.1.3');
define('WIS_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WIS_PLUGIN_URL', plugin_dir_url(__FILE__));

// ===========================================================
// DASHBOARD WIDGET & UPDATES
// ===========================================================
class WIS_Dashboard {
    private static $update_url = 'https://git.viper.ipv64.net/M_Viper/WP-Ingame-Shop-Pro/releases';
    private static $api_url = 'https://git.viper.ipv64.net/api/v1/repos/M_Viper/WP-Ingame-Shop-Pro/releases/latest';
    private static $transient_name = 'wis_update_check';

    public static function init() {
        add_action('wp_dashboard_setup', [__CLASS__, 'add_dashboard_widget']);
        add_action('admin_init', [__CLASS__, 'check_for_update']);
        
        // AJAX Handler für Cache leeren
        add_action('wp_ajax_wis_clear_cache', [__CLASS__, 'ajax_clear_cache']);
    }

    // Button im Dashboard: Cache leeren
    public static function ajax_clear_cache() {
        check_ajax_referer('wis_clear_cache_nonce', 'nonce');
        
        if (!current_user_can('manage_options')) {
            wp_send_json_error('Keine Berechtigung.');
        }

        // Leere den Object Cache
        if (function_exists('wp_cache_flush')) {
            wp_cache_flush();
        }
        
        // Optional: Leere auch unseren Transienten für den Update-Check, damit er neu lädt
        delete_transient(self::$transient_name);

        wp_send_json_success(['message' => 'Cache erfolgreich geleert!']);
    }

    // Prüfe auf Updates (läuft im Admin-Hintergrund)
    public static function check_for_update() {
        $update_data = get_transient(self::$transient_name);

        if (false === $update_data) {
            $response = wp_remote_get(self::$api_url, ['timeout' => 10]);
            
            if (is_wp_error($response)) {
                return; // Fehler beim Abrufen, nichts tun
            }

            $body = wp_remote_retrieve_body($response);
            $data = json_decode($body);

            if (isset($data->tag_name) && isset($data->html_url)) {
                // Entferne 'v' vom Tag (z.B. v2.1.3 -> 2.1.3)
                $remote_version = ltrim($data->tag_name, 'v');
                
                $update_data = [
                    'new_version' => $remote_version,
                    'url'         => self::$update_url,
                    'package_url' => isset($data->assets[0]->browser_download_url) ? $data->assets[0]->browser_download_url : '',
                    'changelog'   => isset($data->body) ? substr($data->body, 0, 200) . '...' : ''
                ];

                // Speichere für 12 Stunden
                set_transient(self::$transient_name, $update_data, 12 * HOUR_IN_SECONDS);
            }
        }
    }

    // Zeige Admin Notice wenn Update verfügbar
    public static function show_update_notice() {
        $update_data = get_transient(self::$transient_name);
        if ($update_data && version_compare(WIS_VERSION, $update_data['new_version'], '<')) {
            ?>
            <div class="notice notice-info is-dismissible">
                <p>
                    <strong>🚀 WP Ingame Shop Pro Update verfügbar!</strong><br>
                    Neue Version: <?php echo esc_html($update_data['new_version']); ?> (Du hast <?php echo WIS_VERSION; ?>)<br>
                    <a href="<?php echo esc_url($update_data['url']); ?>" target="_blank">Hier im Git Repository ansehen</a> oder unten im Widget Details prüfen.
                </p>
            </div>
            <?php
        }
    }

    // Widget registrieren
    public static function add_dashboard_widget() {
        wp_add_dashboard_widget(
            'wis_dashboard_widget',
            '🛒 WP Ingame Shop Pro Dashboard',
            [__CLASS__, 'render_widget']
        );
    }

    // Widget Inhalt rendern
    public static function render_widget() {
        global $wpdb;
        $update_data = get_transient(self::$transient_name);
        
        // Statistiken holen
        $stats = [
            'items'   => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}wis_items WHERE status = 'publish'"),
            'orders'  => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}wis_orders WHERE status = 'completed'"),
            'pending' => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}wis_orders WHERE status IN ('pending', 'claimed')"),
            'revenue' => $wpdb->get_var("SELECT SUM(price) FROM {$wpdb->prefix}wis_orders WHERE status = 'completed'"),
        ];
        $currency = get_option('wis_currency_name', 'Coins');
        
        // Update Status bestimmen
        $update_html = '<span style="color:green;">✅ Aktuell (v' . WIS_VERSION . ')</span>';
        if ($update_data && version_compare(WIS_VERSION, $update_data['new_version'], '<')) {
            $update_html = '<span style="color:#d63638; font-weight:bold;">⚠️ Update verfügbar!</span>';
        }

        ?>
        <style>
            .wis-dash-stats { display: flex; gap: 10px; margin-bottom: 15px; flex-wrap: wrap; }
            .wis-stat-box { flex: 1; min-width: 100px; background: #f9f9f9; padding: 10px; border-radius: 5px; text-align: center; border: 1px solid #eee; }
            .wis-stat-num { display: block; font-size: 1.5em; font-weight: bold; color: #333; }
            .wis-stat-label { font-size: 0.85em; color: #666; }
            .wis-dash-actions { margin-top: 15px; padding-top: 15px; border-top: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; }
        </style>

        <div class="wis-dash-stats">
            <div class="wis-stat-box">
                <span class="wis-stat-num"><?php echo intval($stats['items']); ?></span>
                <span class="wis-stat-label">Items</span>
            </div>
            <div class="wis-stat-box">
                <span class="wis-stat-num"><?php echo intval($stats['orders']); ?></span>
                <span class="wis-stat-label">Bestellungen</span>
            </div>
            <div class="wis-stat-box">
                <span class="wis-stat-num"><?php echo intval($stats['pending']); ?></span>
                <span class="wis-stat-label">Offen</span>
            </div>
            <div class="wis-stat-box">
                <span class="wis-stat-num"><?php echo number_format($stats['revenue']); ?></span>
                <span class="wis-stat-label">Umsatz (<?php echo esc_html($currency); ?>)</span>
            </div>
        </div>

        <p style="margin-bottom: 10px;">
            <strong>Status:</strong> <?php echo $update_html; ?>
        </p>
        
        <?php if ($update_data && version_compare(WIS_VERSION, $update_data['new_version'], '<')): ?>
            <div style="background:#fff3cd; color:#856404; padding:10px; border-radius:4px; margin-bottom:10px; font-size:13px;">
                <strong>Neue Version <?php echo esc_html($update_data['new_version']); ?></strong><br>
                <a href="<?php echo esc_url($update_data['url']); ?>" target="_blank">Updates ansehen</a>
            </div>
        <?php endif; ?>

        <div class="wis-dash-actions">
            <a href="<?php echo admin_url('admin.php?page=wis_orders'); ?>" class="button">Bestellungen ansehen</a>
            
            <button type="button" id="wis-clear-cache-btn" class="button button-secondary button-small">
                🔄 Update-Cache jetzt leeren
            </button>
        </div>

        <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('#wis-clear-cache-btn').on('click', function() {
                var $btn = $(this);
                $btn.prop('disabled', true).text('Leere...');

                $.ajax({
                    url: ajaxurl,
                    type: 'POST',
                    data: {
                        action: 'wis_clear_cache',
                        nonce: '<?php echo wp_create_nonce('wis_clear_cache_nonce'); ?>'
                    },
                    success: function(response) {
                        if (response.success) {
                            $btn.removeClass('button-secondary').addClass('button-primary').text('✅ ' + response.data.message);
                            setTimeout(function() {
                                $btn.prop('disabled', false).removeClass('button-primary').addClass('button-secondary').text('🔄 Update-Cache jetzt leeren');
                            }, 3000);
                        } else {
                            alert('Fehler: ' + (response.data || 'Unbekannt'));
                            $btn.prop('disabled', false).text('🔄 Update-Cache jetzt leeren');
                        }
                    },
                    error: function() {
                        alert('Verbindungsfehler.');
                        $btn.prop('disabled', false).text('🔄 Update-Cache jetzt leeren');
                    }
                });
            });
        });
        </script>
        <?php
    }
}
WIS_Dashboard::init();
// Zeige Notice auch im Admin-Bereich (außer auf dem Dashboard selbst, da dort das Widget ist)
add_action('admin_notices', [WIS_Dashboard::class, 'show_update_notice']);


// ===========================================================
// ACTIVATION & DATABASE
// ===========================================================
class WIS_Activator {
    public static function activate() {
        // Spalte custom_image_url nachrüsten (für bestehende Installationen)
        global $wpdb;
        $table = $wpdb->prefix . 'wis_items';
        $col   = $wpdb->get_var("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND COLUMN_NAME = 'custom_image_url'");
        if (!$col) {
            $wpdb->query("ALTER TABLE $table ADD COLUMN custom_image_url varchar(500) DEFAULT NULL AFTER categories");
        }

        // Ankauf-Spalten nachrüsten (ab v6.5)
        $col_sell = $wpdb->get_var("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND COLUMN_NAME = 'sell_enabled'");
        if (!$col_sell) {
            $wpdb->query("ALTER TABLE $table ADD COLUMN sell_enabled tinyint(1) NOT NULL DEFAULT 0 AFTER status");
            $wpdb->query("ALTER TABLE $table ADD COLUMN sell_price_mode varchar(20) NOT NULL DEFAULT 'percent' AFTER sell_enabled");
            $wpdb->query("ALTER TABLE $table ADD COLUMN sell_price_value int(11) NOT NULL DEFAULT 80 AFTER sell_price_mode");
        }

        // Sell-Log-Tabelle anlegen
        $wpdb->query("CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wis_sell_log (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            player_name varchar(100) NOT NULL,
            server varchar(100) NOT NULL,
            item_id varchar(100) NOT NULL,
            item_name varchar(255) NOT NULL,
            quantity int(11) NOT NULL DEFAULT 1,
            price_per_item decimal(10,2) NOT NULL,
            total_paid decimal(10,2) NOT NULL,
            sold_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY player_name (player_name),
            KEY sold_at (sold_at)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");

        self::create_tables();
        self::set_default_options();
        self::create_default_categories();
        
        if (!wp_next_scheduled('wis_daily_deal_event')) {
            wp_schedule_event(time(), 'daily', 'wis_daily_deal_event');
        }
        // Fly-Abo Renewal: täglich prüfen (läuft nur durch am 1. des Monats)
        if (!wp_next_scheduled('wis_abo_renewal_event')) {
            // Nächsten Mitternacht-Zeitstempel berechnen
            $midnight = strtotime('tomorrow midnight');
            wp_schedule_event($midnight, 'daily', 'wis_abo_renewal_event');
        }
        flush_rewrite_rules();
    }
    
    public static function deactivate() {
        wp_clear_scheduled_hook('wis_daily_deal_event');
        wp_clear_scheduled_hook('wis_abo_renewal_event');
        flush_rewrite_rules();
    }
    
    private static function create_tables() {
        global $wpdb;
        $charset_collate = $wpdb->get_charset_collate();
        
        $tables = [];
        
        // Items
        $tables[] = "CREATE TABLE {$wpdb->prefix}wis_items (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            item_id varchar(100) NOT NULL,
            name varchar(255) NOT NULL,
            description text,
            price int(11) DEFAULT 0,
            offer_price int(11) DEFAULT 0,
            is_offer tinyint(1) DEFAULT 0,
            is_daily_deal tinyint(1) DEFAULT 0,
            servers text,
            categories text,
            custom_image_url varchar(500) DEFAULT NULL,
            status varchar(20) DEFAULT 'draft',
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            UNIQUE KEY item_id (item_id),
            KEY status (status)
        ) $charset_collate;";
        
        // Orders
        $tables[] = "CREATE TABLE {$wpdb->prefix}wis_orders (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            player_name varchar(100) NOT NULL,
            server varchar(100) NOT NULL,
            item_id varchar(100) NOT NULL,
            item_title varchar(255) NOT NULL,
            price int(11) NOT NULL,
            quantity int(11) DEFAULT 1,
            status varchar(20) DEFAULT 'pending',
            response text,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            KEY player_name (player_name),
            KEY status (status)
        ) $charset_collate;";
        
        // Coupons
        $tables[] = "CREATE TABLE {$wpdb->prefix}wis_coupons (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            code varchar(50) NOT NULL,
            value int(11) NOT NULL,
            type varchar(10) DEFAULT 'fixed',
            usage_limit int(11) DEFAULT 1,
            used_count int(11) DEFAULT 0,
            expiry date DEFAULT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            UNIQUE KEY code (code)
        ) $charset_collate;";
        
        // Servers
        $tables[] = "CREATE TABLE {$wpdb->prefix}wis_servers (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            slug varchar(100) NOT NULL,
            name varchar(255) NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            UNIQUE KEY slug (slug)
        ) $charset_collate;";
        
        // Categories
        $tables[] = "CREATE TABLE {$wpdb->prefix}wis_categories (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            name varchar(255) NOT NULL,
            slug varchar(100) NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            UNIQUE KEY slug (slug)
        ) $charset_collate;";
        
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        foreach ($tables as $sql) {
            dbDelta($sql);
        }
    }
    
    private static function set_default_options() {
        $defaults = [
            'wis_currency_name'         => 'Coins',
            'wis_image_base_url'        => 'https://git.viper.ipv64.net/M_Viper/minecraft-items/raw/branch/main/images/',
            'wis_header_text'           => '✅ Auto-Bilder | 💰 Sicherer Checkout | 🎮 Ingame-Bestätigung',
            'wis_coupon_exclude_offers' => '0',
            'wis_daily_deal_enabled'    => '0',
            'wis_daily_deal_discount'   => '20',
            'wis_api_key'               => bin2hex(random_bytes(24)),
        ];

        foreach ($defaults as $key => $value) {
            if (get_option($key) === false) {
                add_option($key, $value);
            }
        }
    }

    public static function check_api_key($request) {
        $key = $request->get_header('X-WIS-Key');
        if (empty($key)) {
            $key = $request->get_param('api_key');
        }
        $stored = get_option('wis_api_key', '');
        return ($stored !== '' && hash_equals($stored, (string) $key));
    }

    public static function spigot_permission($request) {
        if (!self::check_api_key($request)) {
            return new WP_Error('wis_unauthorized', 'Ungültiger oder fehlender API-Key.', ['status' => 401]);
        }
        return true;
    }
    
    private static function create_default_categories() {
        $default_categories = [
            ['name' => 'Baublöcke', 'slug' => 'baublocke'],
            ['name' => 'Dekorationsblöcke', 'slug' => 'dekorationsblocke'],
            ['name' => 'Redstone', 'slug' => 'redstone'],
            ['name' => 'Transport', 'slug' => 'transport'],
            ['name' => 'Natur', 'slug' => 'natur'],
            ['name' => 'Werkzeuge & Hilfsmittel', 'slug' => 'werkzeuge-hilfsmittel'],
            ['name' => 'Kampf', 'slug' => 'kampf'],
            ['name' => 'Nahrung & Tränke', 'slug' => 'nahrung-tranke'],
            ['name' => 'Zutaten', 'slug' => 'zutaten'],
            ['name' => 'Spawn-Eier', 'slug' => 'spawn-eier']
        ];
        
        global $wpdb;
        foreach ($default_categories as $cat) {
            $exists = $wpdb->get_var($wpdb->prepare(
                "SELECT id FROM {$wpdb->prefix}wis_categories WHERE slug = %s",
                $cat['slug']
            ));
            
            if (!$exists) {
                $wpdb->insert($wpdb->prefix . 'wis_categories', [
                    'name' => $cat['name'],
                    'slug' => $cat['slug']
                ]);
            }
        }
    }
    
    public static function run_daily_deal() {
        if (get_option('wis_daily_deal_enabled') !== '1') return;
        
        global $wpdb;
        $table = $wpdb->prefix . 'wis_items';
        $discount = intval(get_option('wis_daily_deal_discount', 20));
        
        $wpdb->update($table, ['is_daily_deal' => 0], ['is_daily_deal' => 1]);
        
        $item = $wpdb->get_row("SELECT * FROM $table WHERE status = 'publish' AND price > 0 ORDER BY RAND() LIMIT 1");
        
        if ($item) {
            $offer_price = max(0, floor($item->price - ($item->price * ($discount / 100))));
            $wpdb->update($table, [
                'is_daily_deal' => 1,
                'is_offer' => 1,
                'offer_price' => $offer_price
            ], ['id' => $item->id]);
        }
    }

    /**
     * Fly-Abo Renewal – läuft täglich, handelt aber nur am 1. des Monats.
     * Verlängert alle aktiven (nicht gekündigten) Abos automatisch um 30 Tage
     * und legt je einen Order-Eintrag als Buchungsnachweis an.
     * Gekündigte Abos laufen bis zum letzten Tag des laufenden Monats und werden dann entfernt.
     */
    public static function run_abo_renewal() {
        // Nur am 1. des Monats ausführen
        if (date('j') !== '1') return;

        global $wpdb;
        $subs_table   = $wpdb->prefix . 'wis_fly_abo_subs';
        $orders_table = $wpdb->prefix . 'wis_orders';

        // Tabelle anlegen falls noch nicht vorhanden (Migration)
        self::create_abo_subs_table();

        // Alle aktiven, nicht gekündigten Abos verlängern
        $active = $wpdb->get_results(
            "SELECT * FROM {$subs_table} WHERE cancelled = 0 AND status = 'active'"
        );

        foreach ($active as $sub) {
            // expires_at um 30 Tage verlängern
            $wpdb->query($wpdb->prepare(
                "UPDATE {$subs_table}
                 SET expires_at = DATE_ADD(expires_at, INTERVAL 30 DAY),
                     renewed_at = NOW(),
                     renewal_count = renewal_count + 1
                 WHERE id = %d",
                $sub->id
            ));

            // Buchungsnachweis als Order anlegen (status: completed)
            $wpdb->insert($orders_table, [
                'player_name' => $sub->player_name,
                'server'      => $sub->server,
                'item_id'     => 'fly_abo_renewal',
                'item_title'  => '✈ Fly-Abo Verlängerung: ' . $sub->label,
                'price'       => $sub->price,
                'quantity'    => 1,
                'status'      => 'completed',
                'response'    => json_encode([
                    'commands' => [[
                        'type'  => 'fly_abo',
                        'days'  => 30,
                        'label' => $sub->label,
                    ]],
                ]),
            ]);
        }

        // Gekündigte Abos die abgelaufen sind deaktivieren
        $wpdb->query(
            "UPDATE {$subs_table}
             SET status = 'expired'
             WHERE cancelled = 1
               AND expires_at < NOW()
               AND status = 'active'"
        );

        // Log
        $count = count($active);
        error_log("[WIS] Fly-Abo Renewal: {$count} Abo(s) verlängert am " . date('d.m.Y'));
    }

    public static function create_abo_subs_table() {
        global $wpdb;
        $table = $wpdb->prefix . 'wis_fly_abo_subs';
        $wpdb->query("CREATE TABLE IF NOT EXISTS {$table} (
            id            INT AUTO_INCREMENT PRIMARY KEY,
            player_name   VARCHAR(64)  NOT NULL,
            server        VARCHAR(64)  NOT NULL DEFAULT '',
            label         VARCHAR(128) NOT NULL,
            price         INT          NOT NULL DEFAULT 0,
            status        VARCHAR(20)  NOT NULL DEFAULT 'active',
            cancelled     TINYINT(1)   NOT NULL DEFAULT 0,
            cancelled_at  DATETIME     DEFAULT NULL,
            expires_at    DATETIME     NOT NULL,
            renewed_at    DATETIME     DEFAULT NULL,
            renewal_count INT          NOT NULL DEFAULT 0,
            created_at    DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
            UNIQUE KEY uq_player_server (player_name, server)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
    }
    
    public static function reset_shop() {
        global $wpdb;
        
        $tables = [
            'wis_items', 'wis_orders', 'wis_coupons', 
            'wis_servers', 'wis_categories'
        ];
        
        foreach ($tables as $table) {
            $wpdb->query("TRUNCATE TABLE {$wpdb->prefix}$table");
        }
        
        self::set_default_options();
        self::create_default_categories();
        return true;
    }
}

// ===========================================================
// ITEM CATEGORIZER
// ===========================================================
class WIS_Item_Categorizer {
    public static function auto_categorize($item_id) {
        $item_id = strtolower($item_id);
        $item_id = str_replace('minecraft:', '', $item_id);
        
        if (strpos($item_id, 'spawn_egg') !== false) {
            return ['spawn-eier'];
        }
        
        if (preg_match('/(wooden|stone|iron|golden|diamond|netherite|copper)_(pickaxe|axe|shovel|hoe)/', $item_id)) {
            return ['werkzeuge-hilfsmittel'];
        }
        
        if (in_array($item_id, [
            'shears', 'fishing_rod', 'flint_and_steel', 'bucket', 'water_bucket', 'lava_bucket',
            'milk_bucket', 'powder_snow_bucket', 'axolotl_bucket', 'cod_bucket', 'pufferfish_bucket',
            'salmon_bucket', 'tadpole_bucket', 'tropical_fish_bucket',
            'compass', 'recovery_compass', 'clock', 'spyglass', 'map', 'filled_map',
            'brush', 'lead', 'name_tag', 'saddle', 'carrot_on_a_stick', 'warped_fungus_on_a_stick'
        ])) {
            return ['werkzeuge-hilfsmittel'];
        }
        
        if (preg_match('/(wooden|stone|iron|golden|diamond|netherite|copper)_(sword|spear)/', $item_id)) {
            return ['kampf'];
        }
        
        if (preg_match('/(leather|chainmail|iron|golden|diamond|netherite|copper|turtle)_(helmet|chestplate|leggings|boots|cap|tunic|pants|shell)/', $item_id)) {
            return ['kampf'];
        }
        
        if (in_array($item_id, [
            'bow', 'crossbow', 'arrow', 'spectral_arrow', 'tipped_arrow',
            'shield', 'trident', 'mace',
            'totem_of_undying', 'elytra',
            'horse_armor', 'iron_horse_armor', 'golden_horse_armor', 'diamond_horse_armor',
            'wolf_armor'
        ]) || strpos($item_id, 'horse_armor') !== false) {
            return ['kampf'];
        }
        
        if (preg_match('/(raw_|cooked_)?(beef|porkchop|mutton|chicken|rabbit|cod|salmon)/', $item_id)) {
            return ['nahrung-tranke'];
        }
        
        if (in_array($item_id, [
            'apple', 'golden_apple', 'enchanted_golden_apple',
            'melon_slice', 'glow_berries', 'sweet_berries', 'chorus_fruit',
            'carrot', 'golden_carrot', 'potato', 'baked_potato', 'poisonous_potato',
            'beetroot',
            'bread', 'cookie', 'cake', 'pumpkin_pie',
            'dried_kelp',
            'tropical_fish', 'pufferfish', 'rotten_flesh', 'spider_eye'
        ])) {
            return ['nahrung-tranke'];
        }
        
        if (strpos($item_id, '_stew') !== false || strpos($item_id, '_soup') !== false) {
            return ['nahrung-tranke'];
        }
        
        if (strpos($item_id, 'potion') !== false || in_array($item_id, [
            'honey_bottle', 'milk_bucket', 'glass_bottle', 'dragon_breath',
            'experience_bottle', 'ominous_bottle'
        ])) {
            return ['nahrung-tranke'];
        }
        
        if (strpos($item_id, '_boat') !== false || strpos($item_id, '_raft') !== false) {
            return ['transport'];
        }
        
        if (strpos($item_id, 'minecart') !== false) {
            return ['transport'];
        }
        
        if (in_array($item_id, ['elytra', 'saddle', 'lead'])) {
            return ['transport'];
        }
        
        if (strpos($item_id, 'redstone') !== false && $item_id !== 'redstone_ore') {
            return ['redstone'];
        }
        
        if (in_array($item_id, [
            'repeater', 'comparator', 'observer',
            'piston', 'sticky_piston',
            'dispenser', 'dropper', 'hopper',
            'lever', 'tripwire_hook', 'daylight_detector',
            'tnt', 'target', 'lightning_rod'
        ]) || strpos($item_id, 'button') !== false || strpos($item_id, 'pressure_plate') !== false) {
            return ['redstone'];
        }
        
        if (strpos($item_id, '_rail') !== false || $item_id === 'rail') {
            return ['redstone'];
        }
        
        $pure_materials = [
            'stick', 'coal', 'charcoal', 'diamond', 'emerald', 'lapis_lazuli',
            'iron_ingot', 'gold_ingot', 'copper_ingot', 'netherite_ingot',
            'iron_nugget', 'gold_nugget', 'copper_nugget',
            'raw_iron', 'raw_gold', 'raw_copper',
            'netherite_scrap', 'netherite_upgrade',
            'amethyst_shard', 'prismarine_shard', 'prismarine_crystals',
            'quartz', 'nether_quartz', 'echo_shard', 'disc_fragment',
            'string', 'feather', 'leather', 'rabbit_hide',
            'slimeball', 'ender_pearl', 'ender_eye',
            'blaze_rod', 'blaze_powder', 'magma_cream', 'ghast_tear',
            'nether_star', 'nether_brick',
            'nautilus_shell', 'heart_of_the_sea', 'scute', 'turtle_scute', 'armadillo_scute',
            'bone', 'bone_meal', 'gunpowder', 'glowstone_dust', 'sugar',
            'phantom_membrane', 'ink_sac', 'glow_ink_sac',
            'paper', 'book', 'flint',
            'fermented_spider_eye', 'glistering_melon_slice', 'rabbit_foot',
            'nether_wart', 'breeze_rod',
            'clay_ball', 'brick', 'firework_star',
            'shulker_shell', 'popped_chorus_fruit'
        ];
        
        if (in_array($item_id, $pure_materials)) {
            return ['zutaten'];
        }
        
        if (strpos($item_id, '_pottery_sherd') !== false || strpos($item_id, '_armor_trim') !== false) {
            return ['zutaten'];
        }
        
        if (in_array($item_id, [
            'dirt', 'coarse_dirt', 'rooted_dirt', 'grass_block', 'podzol', 'mycelium',
            'farmland', 'dirt_path',
            'sand', 'red_sand', 'gravel', 'clay',
            'suspicious_sand', 'suspicious_gravel'
        ])) {
            return ['natur'];
        }
        
        if (strpos($item_id, '_leaves') !== false || strpos($item_id, '_sapling') !== false ||
            strpos($item_id, 'azalea') !== false) {
            return ['natur'];
        }
        
        $flowers = [
            'dandelion', 'poppy', 'blue_orchid', 'allium', 'azure_bluet',
            'red_tulip', 'orange_tulip', 'white_tulip', 'pink_tulip',
            'oxeye_daisy', 'cornflower', 'lily_of_the_valley', 'wither_rose',
            'sunflower', 'lilac', 'rose_bush', 'peony', 
            'pitcher_plant', 'pitcher_pod', 'torchflower', 'torchflower_seeds',
            'pink_petals', 'spore_blossom'
        ];
        if (in_array($item_id, $flowers)) {
            return ['natur'];
        }
        
        if (strpos($item_id, 'mushroom') !== false || strpos($item_id, 'fungus') !== false ||
            in_array($item_id, ['short_grass', 'tall_grass', 'fern', 'large_fern', 'dead_bush'])) {
            return ['natur'];
        }
        
        if (in_array($item_id, [
            'seagrass', 'tall_seagrass', 'kelp', 'dried_kelp', 'sea_pickle',
            'vine', 'weeping_vines', 'twisting_vines', 'cave_vines', 'glow_lichen',
            'hanging_roots', 'mangrove_roots', 'muddy_mangrove_roots'
        ])) {
            return ['natur'];
        }
        
        if (strpos($item_id, '_seeds') !== false || in_array($item_id, [
            'wheat', 'beetroot', 'carrot', 'potato',
            'melon', 'pumpkin', 'carved_pumpkin',
            'sugar_cane', 'bamboo', 'cocoa_beans',
            'sweet_berries', 'glow_berries', 'sweet_berry_bush',
            'nether_wart', 'cactus',
            'mangrove_propagule'
        ])) {
            return ['natur'];
        }
        
        if (in_array($item_id, [
            'crimson_roots', 'warped_roots', 'nether_sprouts',
            'crimson_nylium', 'warped_nylium'
        ])) {
            return ['natur'];
        }
        
        if (in_array($item_id, [
            'moss_block', 'moss_carpet',
            'big_dripleaf', 'small_dripleaf',
            'lily_pad', 'bee_nest', 'honeycomb', 'honeycomb_block',
            'snow', 'snowball', 'powder_snow'
        ])) {
            return ['natur'];
        }
        
        if (strpos($item_id, 'glass') !== false && strpos($item_id, '_pane') === false) {
            return ['dekorationsblocke'];
        }
        if (strpos($item_id, 'glass_pane') !== false || strpos($item_id, 'stained_glass_pane') !== false) {
            return ['dekorationsblocke'];
        }
        
        if (strpos($item_id, '_door') !== false || strpos($item_id, '_trapdoor') !== false ||
            strpos($item_id, '_fence_gate') !== false) {
            return ['dekorationsblocke'];
        }
        
        if ((strpos($item_id, '_fence') !== false && strpos($item_id, '_fence_gate') === false) ||
            (strpos($item_id, '_wall') !== false && !in_array($item_id, ['wall_banner', 'wall_sign', 'wall_torch'])) ||
            $item_id === 'iron_bars') {
            return ['dekorationsblocke'];
        }
        
        if (strpos($item_id, '_stairs') !== false || strpos($item_id, '_slab') !== false) {
            return ['dekorationsblocke'];
        }
        
        if (strpos($item_id, '_carpet') !== false) {
            return ['dekorationsblocke'];
        }
        
        if (in_array($item_id, [
            'torch', 'soul_torch', 'lantern', 'soul_lantern',
            'campfire', 'soul_campfire', 'end_rod',
            'shroomlight', 'froglight', 'sea_lantern'
        ]) || strpos($item_id, '_candle') !== false || $item_id === 'candle') {
            return ['dekorationsblocke'];
        }
        
        if (in_array($item_id, [
            'crafting_table', 'furnace', 'blast_furnace', 'smoker',
            'chest', 'trapped_chest', 'ender_chest', 'barrel',
            'enchanting_table', 'anvil', 'chipped_anvil', 'damaged_anvil',
            'grindstone', 'smithing_table', 'cartography_table', 'fletching_table',
            'loom', 'stonecutter', 'brewing_stand', 'cauldron', 'composter',
            'lectern', 'bookshelf', 'chiseled_bookshelf',
            'bell', 'beacon', 'conduit', 'lodestone', 'respawn_anchor'
        ]) || strpos($item_id, '_bed') !== false || strpos($item_id, 'shulker_box') !== false) {
            return ['dekorationsblocke'];
        }
        
        if (strpos($item_id, '_sign') !== false || strpos($item_id, '_hanging_sign') !== false ||
            strpos($item_id, '_banner') !== false || 
            in_array($item_id, ['item_frame', 'glow_item_frame', 'painting', 'armor_stand'])) {
            return ['dekorationsblocke'];
        }
        
        if (in_array($item_id, [
            'ladder', 'scaffolding', 'chain',
            'flower_pot', 'decorated_pot',
            'dragon_egg', 'dragon_head',
            'note_block', 'jukebox'
        ]) || strpos($item_id, '_head') !== false || strpos($item_id, '_skull') !== false ||
            strpos($item_id, 'coral') !== false) {
            return ['dekorationsblocke'];
        }
        
        if (in_array($item_id, [
            'stone', 'cobblestone', 'mossy_cobblestone',
            'granite', 'polished_granite', 'diorite', 'polished_diorite',
            'andesite', 'polished_andesite', 'calcite', 'tuff',
            'smooth_stone'
        ]) || strpos($item_id, 'stone_brick') !== false) {
            return ['baublocke'];
        }
        
        if (strpos($item_id, 'deepslate') !== false) {
            return ['baublocke'];
        }
        
        if (strpos($item_id, 'brick') !== false && $item_id !== 'brick' && $item_id !== 'nether_brick') {
            return ['baublocke'];
        }
        
        if (strpos($item_id, 'sandstone') !== false) {
            return ['baublocke'];
        }
        
        if (strpos($item_id, 'quartz_') !== false && strpos($item_id, 'nether_quartz') === false) {
            return ['baublocke'];
        }
        
        if (strpos($item_id, 'prismarine') !== false || strpos($item_id, 'purpur') !== false) {
            return ['baublocke'];
        }
        
        if (strpos($item_id, 'concrete') !== false || strpos($item_id, 'terracotta') !== false) {
            return ['baublocke'];
        }
        
        if (strpos($item_id, '_wool') !== false) {
            return ['baublocke'];
        }
        
        if (strpos($item_id, '_planks') !== false || 
            (strpos($item_id, '_log') !== false && strpos($item_id, 'stripped') === false) ||
            (strpos($item_id, '_wood') !== false && strpos($item_id, 'stripped') === false)) {
            return ['baublocke'];
        }
        
        if (strpos($item_id, 'stripped_') !== false) {
            return ['baublocke'];
        }
        
        if (in_array($item_id, ['bamboo_block', 'bamboo_mosaic', 'crimson_stem', 'warped_stem',
                                 'crimson_hyphae', 'warped_hyphae'])) {
            return ['baublocke'];
        }
        
        if (strpos($item_id, 'copper_') !== false && strpos($item_id, '_ingot') === false && 
            strpos($item_id, '_nugget') === false && $item_id !== 'copper_door' && $item_id !== 'copper_trapdoor') {
            return ['baublocke'];
        }
        
        if (strpos($item_id, '_block') !== false) {
            return ['baublocke'];
        }
        
        if (in_array($item_id, [
            'netherrack', 'soul_sand', 'soul_soil',
            'basalt', 'polished_basalt', 'smooth_basalt',
            'end_stone', 'obsidian', 'crying_obsidian'
        ]) || strpos($item_id, 'blackstone') !== false || strpos($item_id, 'end_stone_brick') !== false) {
            return ['baublocke'];
        }
        
        if (in_array($item_id, [
            'glowstone', 'sponge', 'wet_sponge',
            'ice', 'packed_ice', 'blue_ice',
            'magma_block', 'slime_block', 'honey_block',
            'hay_block', 'dried_kelp_block',
            'mud', 'packed_mud', 'mud_bricks',
            'dripstone_block', 'amethyst_block', 'budding_amethyst'
        ])) {
            return ['baublocke'];
        }
        
        return ['baublocke'];
    }
}

// ===========================================================
// DATABASE HELPER
// ===========================================================
class WIS_DB {

    /**
     * Gibt die Bild-URL eines Items zurück.
     * Priorität: custom_image_url > automatisch generierte URL aus item_id.
     */
    public static function get_item_image($item) {
        if (!empty($item->custom_image_url)) {
            return esc_url($item->custom_image_url);
        }
        $img_base = get_option('wis_image_base_url', '');
        $img_name = str_replace(':', '_', $item->item_id) . '.png';
        return $img_base . $img_name;
    }

    public static function get_items($args = []) {
        global $wpdb;
        $table = $wpdb->prefix . 'wis_items';
        
        $where_parts = ["1=1"];
        
        if (isset($args['status'])) {
            $where_parts[] = $wpdb->prepare("status = %s", $args['status']);
        }
        
        if (isset($args['category_slug']) && !empty($args['category_slug'])) {
            $search_pattern = '%"' . $args['category_slug'] . '"%';
            $where_parts[] = $wpdb->prepare("categories LIKE %s", $search_pattern);
        }
        
        if (isset($args['ids']) && is_array($args['ids']) && !empty($args['ids'])) {
            $ids = array_map('intval', $args['ids']);
            $placeholders = implode(',', array_fill(0, count($ids), '%d'));
            $where_parts[] = sprintf("id IN ($placeholders)", ...$ids);
        }

        if (isset($args['search']) && !empty($args['search'])) {
            $search_like = '%' . $wpdb->esc_like($args['search']) . '%';
            $where_parts[] = $wpdb->prepare("(name LIKE %s OR item_id LIKE %s)", $search_like, $search_like);
        }
        
        $where = implode(" AND ", $where_parts);
        $limit = isset($args['limit']) ? "LIMIT " . intval($args['limit']) : "";
        $orderby = isset($args['orderby']) ? "ORDER BY " . sanitize_text_field($args['orderby']) : "ORDER BY name ASC";
        
        return $wpdb->get_results("SELECT * FROM $table WHERE $where $orderby $limit");
    }

    public static function count_items($args = []) {
        global $wpdb;
        $table = $wpdb->prefix . 'wis_items';
        $where_parts = ["1=1"];

        if (isset($args['status'])) {
            $where_parts[] = $wpdb->prepare("status = %s", $args['status']);
        }
        if (isset($args['category_slug']) && !empty($args['category_slug'])) {
            $search_pattern = '%"' . $args['category_slug'] . '"%';
            $where_parts[] = $wpdb->prepare("categories LIKE %s", $search_pattern);
        }
        if (isset($args['search']) && !empty($args['search'])) {
            $search_like = '%' . $wpdb->esc_like($args['search']) . '%';
            $where_parts[] = $wpdb->prepare("(name LIKE %s OR item_id LIKE %s)", $search_like, $search_like);
        }

        $where = implode(" AND ", $where_parts);
        return (int) $wpdb->get_var("SELECT COUNT(*) FROM $table WHERE $where");
    }
    
    public static function get_item($id) {
        global $wpdb;
        return $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}wis_items WHERE id = %d", 
            $id
        ));
    }

    public static function get_item_by_item_id($item_id) {
        global $wpdb;
        return $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}wis_items WHERE item_id = %s",
            $item_id
        ));
    }
    
    public static function insert_item($data) {
        global $wpdb;
        
        if (empty($data['categories']) || $data['categories'] === '[]') {
            $auto_cats = WIS_Item_Categorizer::auto_categorize($data['item_id']);
            $data['categories'] = json_encode($auto_cats);
        }
        
        return $wpdb->insert($wpdb->prefix . 'wis_items', $data);
    }
    
    public static function update_item($id, $data) {
        global $wpdb;
        return $wpdb->update($wpdb->prefix . 'wis_items', $data, ['id' => $id]);
    }
    
    public static function delete_item($id) {
        global $wpdb;
        return $wpdb->delete($wpdb->prefix . 'wis_items', ['id' => $id]);
    }
    
    public static function get_servers() {
        global $wpdb;
        return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wis_servers ORDER BY name ASC");
    }
    
    public static function insert_server($slug, $name) {
        global $wpdb;
        return $wpdb->insert($wpdb->prefix . 'wis_servers', [
            'slug' => sanitize_title($slug),
            'name' => sanitize_text_field($name)
        ]);
    }
    
    public static function delete_server($id) {
        global $wpdb;
        return $wpdb->delete($wpdb->prefix . 'wis_servers', ['id' => $id]);
    }
    
    public static function get_categories() {
        global $wpdb;
        return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wis_categories ORDER BY name ASC");
    }
    
    public static function insert_category($name) {
        global $wpdb;
        return $wpdb->insert($wpdb->prefix . 'wis_categories', [
            'name' => sanitize_text_field($name),
            'slug' => sanitize_title($name)
        ]);
    }
    
    public static function delete_category($id) {
        global $wpdb;
        return $wpdb->delete($wpdb->prefix . 'wis_categories', ['id' => $id]);
    }
    
    public static function get_coupons() {
        global $wpdb;
        return $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wis_coupons ORDER BY created_at DESC");
    }
    
    public static function get_coupon_by_code($code) {
        global $wpdb;
        return $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}wis_coupons WHERE code = %s",
            strtoupper($code)
        ));
    }
    
    public static function insert_coupon($data) {
        global $wpdb;
        $data['code'] = strtoupper($data['code']);
        return $wpdb->insert($wpdb->prefix . 'wis_coupons', $data);
    }
    
    public static function update_coupon($id, $data) {
        global $wpdb;
        if (isset($data['code'])) {
            $data['code'] = strtoupper($data['code']);
        }
        return $wpdb->update($wpdb->prefix . 'wis_coupons', $data, ['id' => $id]);
    }
    
    public static function delete_coupon($id) {
        global $wpdb;
        return $wpdb->delete($wpdb->prefix . 'wis_coupons', ['id' => $id]);
    }
    
    public static function get_orders($limit = 100) {
        global $wpdb;
        return $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}wis_orders ORDER BY created_at DESC LIMIT %d",
            $limit
        ));
    }
    
    public static function get_order($id) {
        global $wpdb;
        return $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}wis_orders WHERE id = %d",
            $id
        ));
    }
    
    public static function insert_order($data) {
        global $wpdb;
        return $wpdb->insert($wpdb->prefix . 'wis_orders', $data);
    }
    
    public static function update_order_status($id, $status) {
        global $wpdb;
        return $wpdb->update(
            $wpdb->prefix . 'wis_orders',
            ['status' => $status],
            ['id' => $id]
        );
    }
    
    public static function delete_order($id) {
        global $wpdb;
        return $wpdb->delete($wpdb->prefix . 'wis_orders', ['id' => $id]);
    }
}

// ===========================================================
// ADMIN PAGES
// ===========================================================
class WIS_Admin {
    // Wird auf admin_init gefeuert – vor jeder HTML-Ausgabe, damit wp_redirect() funktioniert
    public static function handle_save_item() {
        if (!isset($_POST['wis_save_item'])) return;
        if (!current_user_can('manage_options')) return;
        check_admin_referer('wis_item_form');

        global $wpdb;

        $item_type = sanitize_text_field($_POST['item_type'] ?? 'minecraft');
        if ($item_type === 'fly') {
            $resolved_item_id = sanitize_text_field($_POST['fly_duration'] ?? 'fly_5min');
        } elseif ($item_type === 'rank') {
            $rank_id       = preg_replace('/[^a-z0-9_\-]/', '', strtolower($_POST['rank_id']       ?? 'vip'));
            $lp_group      = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_POST['lp_group']              ?? $rank_id);
            $default_group = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_POST['default_group']         ?? 'default');
            $rank_days     = max(0, intval($_POST['rank_days'] ?? 30));
            if (empty($lp_group))      $lp_group      = $rank_id;
            if (empty($default_group)) $default_group = 'default';
            $resolved_item_id = 'rank_' . $rank_id . '_' . $lp_group . '_' . $default_group . '_' . $rank_days;
        } elseif ($item_type === 'fly_abo') {
            $resolved_item_id = 'fly_abo';
        } elseif ($item_type === 'plot_slots') {
            $plot_extra_slots = max(1, intval($_POST['plot_extra_slots'] ?? 1));
            $resolved_item_id = 'plot_slots_' . $plot_extra_slots;
        } elseif ($item_type === 'plot_abo') {
            $plot_abo_slots = max(1, intval($_POST['plot_abo_slots'] ?? 1));
            $resolved_item_id = 'plot_abo_' . $plot_abo_slots;
        } else {
            $resolved_item_id = sanitize_text_field($_POST['item_id'] ?? '');
        }

        if (empty($resolved_item_id) || empty(trim($_POST['name'] ?? ''))) {
            // Fehler als transient speichern, damit page_items() ihn anzeigen kann
            set_transient('wis_save_error_' . get_current_user_id(), '❌ Name und Item-ID sind Pflichtfelder.', 30);
            wp_redirect(wp_get_referer() ?: admin_url('admin.php?page=wis_items'));
            exit;
        }

        $data = [
            'item_id'          => $resolved_item_id,
            'name'             => sanitize_text_field($_POST['name']),
            'description'      => sanitize_textarea_field($_POST['description'] ?? ''),
            'price'            => intval($_POST['price'] ?? 0),
            'offer_price'      => intval($_POST['offer_price'] ?? 0),
            'is_offer'         => isset($_POST['is_offer']) ? 1 : 0,
            'servers'          => isset($_POST['servers']) ? json_encode(array_map('sanitize_text_field', $_POST['servers'])) : '[]',
            'categories'       => isset($_POST['categories']) ? json_encode(array_map('sanitize_text_field', $_POST['categories'])) : '[]',
            'custom_image_url'  => esc_url_raw($_POST['custom_image_url'] ?? ''),
            'sell_enabled'      => isset($_POST['sell_enabled']) ? 1 : 0,
            'sell_price_mode'   => in_array($_POST['sell_price_mode'] ?? '', ['percent','fixed','minus']) ? $_POST['sell_price_mode'] : 'percent',
            'sell_price_value'  => max(0, intval($_POST['sell_price_value'] ?? 80)),
            'status'           => (intval($_POST['price'] ?? 0) > 0 || $item_type === 'fly' || $item_type === 'rank' || $item_type === 'fly_abo' || $item_type === 'plot_slots' || $item_type === 'plot_abo') ? 'publish' : 'draft',
        ];

        if (isset($_GET['edit'])) {
            $result = WIS_DB::update_item(intval($_GET['edit']), $data);
            if ($result !== false) {
                wp_redirect(admin_url('admin.php?page=wis_items&edit=' . intval($_GET['edit']) . '&saved=1'));
                exit;
            } else {
                set_transient('wis_save_error_' . get_current_user_id(), '❌ Fehler beim Speichern: ' . $wpdb->last_error, 30);
                wp_redirect(admin_url('admin.php?page=wis_items&edit=' . intval($_GET['edit'])));
                exit;
            }
        } else {
            $existing = WIS_DB::get_item_by_item_id($resolved_item_id);
            if ($existing) {
                WIS_DB::update_item($existing->id, $data);
                wp_redirect(admin_url('admin.php?page=wis_items&edit=' . $existing->id . '&saved=1'));
                exit;
            }
            $result = WIS_DB::insert_item($data);
            if ($result) {
                $new_id = $wpdb->insert_id;
                wp_redirect(admin_url('admin.php?page=wis_items&edit=' . $new_id . '&created=1'));
                exit;
            } else {
                $err = $wpdb->last_error ?: 'Unbekannter Fehler.';
                set_transient('wis_save_error_' . get_current_user_id(), '❌ Fehler beim Erstellen: ' . $err, 30);
                wp_redirect(admin_url('admin.php?page=wis_items&add=1'));
                exit;
            }
        }
    }

    public static function register_menu() {
        add_menu_page(
            'Ingame Shop',
            'Ingame Shop',
            'manage_options',
            'wis_shop',
            [self::class, 'page_overview'],
            'dashicons-cart',
            6
        );
        
        add_submenu_page('wis_shop', 'Einstellungen', 'Einstellungen', 'manage_options', 'wis_shop');
        add_submenu_page('wis_shop', 'Items', 'Items', 'manage_options', 'wis_items', [self::class, 'page_items']);
        add_submenu_page('wis_shop', 'Bestellungen', 'Bestellungen', 'manage_options', 'wis_orders', [self::class, 'page_orders']);
        add_submenu_page('wis_shop', 'Server', 'Server', 'manage_options', 'wis_servers', [self::class, 'page_servers']);
        add_submenu_page('wis_shop', 'Kategorien', 'Kategorien', 'manage_options', 'wis_categories', [self::class, 'page_categories']);
        add_submenu_page('wis_shop', 'Gutscheine', 'Gutscheine', 'manage_options', 'wis_coupons', [self::class, 'page_coupons']);
        add_submenu_page('wis_shop', 'JSON Export/Import', 'JSON Tools', 'manage_options', 'wis_json', [self::class, 'page_json']);
        add_submenu_page('wis_shop', 'Shop Reset', '🔄 Reset', 'manage_options', 'wis_reset', [self::class, 'page_reset']);
        add_submenu_page('wis_shop', 'Top Spender', 'Top Spender', 'manage_options', 'wis_top_spenders', [self::class, 'page_top_spenders']);
    }
    
    public static function page_overview() {
        if (isset($_POST['wis_save_settings']) && check_admin_referer('wis_settings')) {
            update_option('wis_currency_name', sanitize_text_field($_POST['wis_currency_name']));
            update_option('wis_image_base_url', esc_url_raw($_POST['wis_image_base_url']));
            update_option('wis_header_text', wp_kses_post($_POST['wis_header_text']));
            update_option('wis_coupon_exclude_offers', isset($_POST['wis_coupon_exclude_offers']) ? '1' : '0');
            update_option('wis_daily_deal_enabled', isset($_POST['wis_daily_deal_enabled']) ? '1' : '0');
            update_option('wis_daily_deal_discount', intval($_POST['wis_daily_deal_discount']));
            update_option('wis_offline_queue_enabled', isset($_POST['wis_offline_queue_enabled']) ? '1' : '0');
            echo '<div class="updated"><p>✅ Einstellungen gespeichert!</p></div>';
        }

        if (isset($_POST['wis_regen_key']) && check_admin_referer('wis_regen_key')) {
            update_option('wis_api_key', bin2hex(random_bytes(24)));
            echo '<div class="updated"><p>🔑 Neuer API-Key generiert! Bitte in der config.yml des Spigot-Plugins aktualisieren.</p></div>';
        }
        
        global $wpdb;
        $stats = [
            'items' => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}wis_items"),
            'orders' => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}wis_orders"),
            'servers' => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}wis_servers"),
            'coupons' => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}wis_coupons")
        ];
        
        ?>
        <div class="wrap">
            <h1>🛒 WP Ingame Shop Pro <span style="color:#28a745;">v<?php echo WIS_VERSION; ?></span></h1>
            
            <div class="card" style="max-width:800px; margin-top:20px;">
                <h2>📊 Statistiken</h2>
                <table class="widefat">
                    <tr><th>Items im Shop:</th><td><strong><?php echo $stats['items']; ?></strong></td></tr>
                    <tr><th>Bestellungen:</th><td><strong><?php echo $stats['orders']; ?></strong></td></tr>
                    <tr><th>Server:</th><td><strong><?php echo $stats['servers']; ?></strong></td></tr>
                    <tr><th>Gutscheine:</th><td><strong><?php echo $stats['coupons']; ?></strong></td></tr>
                </table>
            </div>
            
            <div class="card" style="max-width:800px; margin-top:20px; padding:20px;">
                <h2>⚙️ Einstellungen</h2>
                <form method="post">
                    <?php wp_nonce_field('wis_settings'); ?>
                    <table class="form-table">
                        <tr>
                            <th><label>Shop Header Text</label></th>
                            <td>
                                <?php
                                wp_editor(
                                    get_option('wis_header_text', ''),
                                    'wis_header_text',
                                    [
                                        'textarea_name' => 'wis_header_text',
                                        'textarea_rows' => 5,
                                        'media_buttons' => false,
                                        'teeny'         => true,
                                        'quicktags'     => true,
                                        'tinymce'       => [
                                            'toolbar1' => 'bold,italic,underline,bullist,numlist,link,unlink,separator,alignleft,aligncenter,alignright,alignjustify,separator,undo,redo',
                                            'toolbar2' => '',
                                        ],
                                    ]
                                );
                                ?>
                                <p class="description" style="margin-top:8px;"><strong>Live-Vorschau:</strong></p>
                                <div id="wis-header-preview" style="margin-top:6px;padding:12px 18px;background:#d4edda;border:1px solid #b8dbc0;border-radius:6px;color:#155724;font-size:14px;min-height:40px;"></div>
                                <script>
                                jQuery(function($) {
                                    function updatePreview() {
                                        var content = '';
                                        if (typeof tinymce !== 'undefined' && tinymce.get('wis_header_text')) {
                                            content = tinymce.get('wis_header_text').getContent();
                                        } else {
                                            content = $('#wis_header_text').val();
                                        }
                                        $('#wis-header-preview').html(content || '<em style="color:#888;">Vorschau erscheint hier\u2026</em>');
                                    }
                                    $(document).on('tinymce-editor-init', function(event, editor) {
                                        if (editor.id === 'wis_header_text') {
                                            editor.on('keyup change NodeChange', updatePreview);
                                            updatePreview();
                                        }
                                    });
                                    $(document).on('input change', '#wis_header_text', updatePreview);
                                    setTimeout(updatePreview, 800);
                                });
                                </script>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="currency">Währungsname</label></th>
                            <td>
                                <input type="text" id="currency" name="wis_currency_name" value="<?php echo esc_attr(get_option('wis_currency_name')); ?>" class="regular-text">
                            </td>
                        </tr>
                        <tr>
                            <th><label for="image_url">Bilder Basis-URL</label></th>
                            <td>
                                <input type="text" id="image_url" name="wis_image_base_url" value="<?php echo esc_attr(get_option('wis_image_base_url')); ?>" class="large-text code">
                                <p class="description">Muss mit / enden! Z.B.: https://git.viper.ipv64.net/.../images/</p>
                            </td>
                        </tr>
                        <tr>
                            <th>Gutscheine bei Angeboten</th>
                            <td>
                                <label>
                                    <input type="checkbox" name="wis_coupon_exclude_offers" value="1" <?php checked(get_option('wis_coupon_exclude_offers'), '1'); ?>>
                                    Gutscheine NICHT auf Angebote anwenden
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <th>Daily Deal</th>
                            <td>
                                <label>
                                    <input type="checkbox" name="wis_daily_deal_enabled" value="1" <?php checked(get_option('wis_daily_deal_enabled'), '1'); ?>>
                                    Automatisches Tagesangebot aktivieren
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="discount">Daily Deal Rabatt (%)</label></th>
                            <td>
                                <input type="number" id="discount" name="wis_daily_deal_discount" value="<?php echo esc_attr(get_option('wis_daily_deal_discount', 20)); ?>" min="1" max="99">
                            </td>
                        </tr>
                        <tr>
                            <th>Offline-Queue</th>
                            <td>
                                <label>
                                    <input type="checkbox" name="wis_offline_queue_enabled" value="1" <?php checked(get_option('wis_offline_queue_enabled', '0'), '1'); ?>>
                                    Items auch an offline Spieler liefern (beim nächsten Login)
                                </label>
                                <p class="description">Erfordert Spigot-Plugin v6.3+ mit Offline-Queue-Unterstützung</p>
                            </td>
                        </tr>
                    </table>
                    <p class="submit">
                        <input type="submit" name="wis_save_settings" class="button button-primary" value="Einstellungen speichern">
                    </p>
                </form>
            </div>

            <div class="card" style="max-width:800px; margin-top:20px; padding:20px; border-left:4px solid #0073aa;">
                <h2>🔑 Spigot API-Key</h2>
                <p>Dieser Key muss in der <code>config.yml</code> des Spigot-Plugins als <code>api-key</code> eingetragen werden.</p>
                <div style="display:flex; align-items:center; gap:10px; margin:10px 0;">
                    <input type="text"
                           id="wis-api-key-field"
                           value="<?php echo esc_attr(get_option('wis_api_key', '')); ?>"
                           class="large-text code"
                           readonly
                           style="font-family:monospace; background:#f0f4f8;">
                    <button type="button" class="button" onclick="
                        const f = document.getElementById('wis-api-key-field');
                        f.type='text'; f.select();
                        document.execCommand('copy');
                        this.textContent='✅ Kopiert!';
                        setTimeout(()=>this.textContent='📋 Kopieren',2000);
                    ">📋 Kopieren</button>
                </div>
                <form method="post" onsubmit="return confirm('Neuen Key generieren? Das Spigot-Plugin muss danach neu konfiguriert werden!');">
                    <?php wp_nonce_field('wis_regen_key'); ?>
                    <input type="submit" name="wis_regen_key" class="button button-secondary" value="🔄 Neuen Key generieren">
                </form>
                <p class="description" style="margin-top:10px;">
                    Geschützte Endpunkte: <code>pending_orders</code>, <code>execute_order</code>, <code>complete_order</code>, <code>cancel_order</code>, <code>pending_offline</code>
                </p>
            </div>
        </div>
        <?php
    }
    
    public static function page_items() {
        if (isset($_POST['wis_bulk_save']) && check_admin_referer('wis_bulk_edit')) {
            $ids = isset($_POST['item_ids']) ? array_map('intval', $_POST['item_ids']) : [];
            $action = sanitize_text_field($_POST['bulk_action_type']);
            
            if (!empty($ids) && !empty($action)) {
                $count = 0;
                foreach ($ids as $id) {
                    $data = [];
                    
                    switch ($action) {
                        case 'price':
                            if (isset($_POST['item_prices'][$id])) {
                                $new_price = intval($_POST['item_prices'][$id]);
                                if ($new_price >= 0) {
                                    $data['price'] = $new_price;
                                }
                            }
                            break;
                            
                        case 'offer':
                            $is_offer = isset($_POST['item_offers'][$id]) ? 1 : 0;
                            $data['is_offer'] = $is_offer;
                            
                            if (isset($_POST['item_offer_prices'][$id])) {
                                $offer_price = intval($_POST['item_offer_prices'][$id]);
                                $data['offer_price'] = $is_offer ? $offer_price : 0;
                            } else if (!$is_offer) {
                                $data['offer_price'] = 0;
                            }
                            break;
                            
                        case 'status':
                            $new_status = sanitize_text_field($_POST['bulk_status']);
                            if (in_array($new_status, ['publish', 'draft'])) {
                                $data['status'] = $new_status;
                            }
                            break;
                            
                        case 'server':
                            if (isset($_POST['item_servers'][$id])) {
                                $data['servers'] = json_encode($_POST['item_servers'][$id]);
                            } else {
                                $data['servers'] = '[]';
                            }
                            break;
                            
                        case 'category':
                            if (isset($_POST['item_cat_assignments'][$id])) {
                                $data['categories'] = json_encode($_POST['item_cat_assignments'][$id]);
                            }
                            break;

                        case 'sell':
                            $data['sell_enabled']     = isset($_POST['item_sell_enabled'][$id]) ? 1 : 0;
                            $mode = sanitize_text_field($_POST['item_sell_mode'][$id] ?? 'percent');
                            if (!in_array($mode, ['percent', 'fixed', 'minus'])) $mode = 'percent';
                            $data['sell_price_mode']  = $mode;
                            $data['sell_price_value'] = max(0, intval($_POST['item_sell_value'][$id] ?? 80));
                            break;
                    }
                    
                    if (!empty($data)) {
                        WIS_DB::update_item($id, $data);
                        $count++;
                    }
                }
                echo '<div class="updated"><p>✅ ' . $count . ' Items erfolgreich aktualisiert!</p></div>';
            }
        }
        
        if (isset($_POST['wis_bulk_apply']) && !empty($_POST['wis_bulk_action']) && isset($_POST['item_ids'])) {
            $action = sanitize_text_field($_POST['wis_bulk_action']);
            $selected_ids = array_map('intval', $_POST['item_ids']);
            
            $all_servers = WIS_DB::get_servers();
            $all_categories = WIS_DB::get_categories();
            $currency = get_option('wis_currency_name', 'Coins');
            
            $items_to_edit = WIS_DB::get_items(['ids' => $selected_ids]);
            
            ?>
            <div class="wrap">
                <h1>📦 Mehrfachbearbeitung</h1>
                <p><strong>Aktion:</strong> <?php echo esc_html(ucfirst($action)); ?> für <?php echo count($selected_ids); ?> Items.</p>
                
                <div class="card" style="max-width:2000px; padding:20px; margin-top:20px;">
                    <form method="post">
                        <?php wp_nonce_field('wis_bulk_edit'); ?>
                        <input type="hidden" name="bulk_action_type" value="<?php echo esc_attr($action); ?>">
                        
                        <?php foreach ($selected_ids as $id): ?>
                            <input type="hidden" name="item_ids[]" value="<?php echo $id; ?>">
                        <?php endforeach; ?>
                        
                        <table class="form-table">
                            <?php if ($action === 'price'): ?>
                                <tr>
                                    <th colspan="2"><h3>Preis pro Item festlegen</h3></th>
                                </tr>
                                <tr>
                                    <td colspan="2" style="padding:0;">
                                        <div style="max-height:800px; overflow-y:auto; border:1px solid #ddd; padding:10px;">
                                            <table class="widefat striped">
                                                <thead>
                                                    <tr>
                                                        <th style="width:50%">Item Name</th>
                                                        <th style="width:25%">Aktueller Preis</th>
                                                        <th style="width:25%">Neuer Preis (<?php echo esc_html($currency); ?>)</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php foreach($items_to_edit as $item): ?>
                                                        <tr>
                                                            <td>
                                                                <strong><?php echo esc_html($item->name); ?></strong><br>
                                                                <small style="color:#666"><?php echo esc_html($item->item_id); ?></small>
                                                            </td>
                                                            <td><?php echo esc_html($item->price); ?> <?php echo esc_html($currency); ?></td>
                                                            <td>
                                                                <input type="number" 
                                                                       name="item_prices[<?php echo $item->id; ?>]" 
                                                                       value="<?php echo esc_attr($item->price); ?>" 
                                                                       min="0" 
                                                                       style="width:100%; padding:5px;">
                                                            </td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </td>
                                </tr>
                                
                            <?php elseif ($action === 'offer'): ?>
                                <tr>
                                    <th colspan="2"><h3>Angebot pro Item festlegen</h3></th>
                                </tr>
                                <tr>
                                    <td colspan="2" style="padding:0;">
                                        <div style="max-height:800px; overflow-y:auto; border:1px solid #ddd; padding:10px;">
                                            <table class="widefat striped">
                                                <thead>
                                                    <tr>
                                                        <th style="width:40%">Item Name</th>
                                                        <th style="width:20%">Normalpreis</th>
                                                        <th style="width:20%">Als Angebot?</th>
                                                        <th style="width:20%">Angebotspreis</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php foreach($items_to_edit as $item): ?>
                                                        <tr>
                                                            <td>
                                                                <strong><?php echo esc_html($item->name); ?></strong><br>
                                                                <small style="color:#666"><?php echo esc_html($item->item_id); ?></small>
                                                            </td>
                                                            <td><?php echo esc_html($item->price); ?> <?php echo esc_html($currency); ?></td>
                                                            <td>
                                                                <input type="checkbox" 
                                                                       name="item_offers[<?php echo $item->id; ?>]" 
                                                                       value="1" 
                                                                       <?php checked($item->is_offer, 1); ?>
                                                                       onchange="toggleOfferPrice(this, <?php echo $item->id; ?>)">
                                                            </td>
                                                            <td>
                                                                <input type="number" 
                                                                       name="item_offer_prices[<?php echo $item->id; ?>]" 
                                                                       id="offer_price_<?php echo $item->id; ?>"
                                                                       value="<?php echo esc_attr($item->offer_price); ?>" 
                                                                       min="0" 
                                                                       style="width:100%; padding:5px;"
                                                                       <?php if(!$item->is_offer): ?>disabled<?php endif; ?>>
                                                            </td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                        <script>
                                        function toggleOfferPrice(checkbox, itemId) {
                                            const input = document.getElementById('offer_price_' + itemId);
                                            input.disabled = !checkbox.checked;
                                        }
                                        </script>
                                    </td>
                                </tr>
                                
                            <?php elseif ($action === 'status'): ?>
                                <tr>
                                    <th><label>Neuer Status für alle ausgewählten Items</label></th>
                                    <td>
                                        <select name="bulk_status">
                                            <option value="publish">Aktivieren (Publish)</option>
                                            <option value="draft">Deaktivieren (Draft)</option>
                                        </select>
                                    </td>
                                </tr>
                                
                            <?php elseif ($action === 'server'): ?>
                                <tr>
                                    <th colspan="2"><h3>Server pro Item zuweisen</h3></th>
                                </tr>
                                <tr>
                                    <td colspan="2" style="padding:0;">
                                        <?php if (empty($all_servers)): ?>
                                            <p>Keine Server vorhanden.</p>
                                        <?php else: ?>
                                            <div style="max-height:800px; overflow-y:auto; border:1px solid #ddd; padding:10px;">
                                                <table class="widefat striped">
                                                    <thead>
                                                        <tr>
                                                            <th style="width:40%">Item Name</th>
                                                            <th style="width:60%">Server (Mehrfachauswahl möglich)</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach($items_to_edit as $item): 
                                                            $current_servers = json_decode($item->servers, true) ?: [];
                                                        ?>
                                                            <tr>
                                                                <td>
                                                                    <strong><?php echo esc_html($item->name); ?></strong><br>
                                                                    <small style="color:#666"><?php echo esc_html($item->item_id); ?></small>
                                                                </td>
                                                                <td>
                                                                    <?php foreach ($all_servers as $s): 
                                                                        $checked = in_array($s->slug, $current_servers) ? 'checked' : '';
                                                                    ?>
                                                                        <label style="display:inline-block; margin-right:15px; margin-bottom:5px;">
                                                                            <input type="checkbox" 
                                                                                   name="item_servers[<?php echo $item->id; ?>][]" 
                                                                                   value="<?php echo esc_attr($s->slug); ?>" 
                                                                                   <?php echo $checked; ?>>
                                                                            <?php echo esc_html($s->name); ?>
                                                                        </label>
                                                                    <?php endforeach; ?>
                                                                </td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    </tbody>
                                                </table>
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                
                            <?php elseif ($action === 'category'): ?>
                                <tr>
                                    <th colspan="2"><h3>Kategorien pro Item zuweisen</h3></th>
                                </tr>
                                <tr>
                                    <td colspan="2" style="padding:0;">
                                        <div style="max-height:800px; overflow-y:auto; border:1px solid #ddd; padding:10px;">
                                            <?php if(empty($items_to_edit)): ?>
                                                <p>Fehler beim Laden der Items.</p>
                                            <?php else: ?>
                                                <table class="widefat striped">
                                                    <thead>
                                                        <tr>
                                                            <th style="width:35%">Item Name</th>
                                                            <th style="width:65%">Kategorie (Mehrfachauswahl mit STRG+Klick)</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach($items_to_edit as $item): 
                                                            $current_cats = json_decode($item->categories, true) ?: [];
                                                        ?>
                                                            <tr>
                                                                <td>
                                                                    <strong><?php echo esc_html($item->name); ?></strong><br>
                                                                    <small style="color:#666"><?php echo esc_html($item->item_id); ?></small>
                                                                </td>
                                                                <td>
                                                                    <select name="item_cat_assignments[<?php echo $item->id; ?>][]" multiple style="height:250px; width:100%; font-size:14px; padding:5px;">
                                                                        <?php foreach($all_categories as $cat): 
                                                                            $selected = in_array($cat->slug, $current_cats) ? 'selected' : '';
                                                                        ?>
                                                                            <option value="<?php echo esc_attr($cat->slug); ?>" <?php echo $selected; ?>>
                                                                                <?php echo esc_html($cat->name); ?>
                                                                            </option>
                                                                        <?php endforeach; ?>
                                                                    </select>
                                                                </td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    </tbody>
                                                </table>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                </tr>
                            <?php elseif ($action === 'sell'): ?>
                                <tr>
                                    <th colspan="2"><h3>Ankauf pro Item konfigurieren</h3></th>
                                </tr>
                                <tr>
                                    <td colspan="2" style="padding:0;">
                                        <div style="max-height:800px; overflow-y:auto; border:1px solid #ddd; padding:10px;">
                                            <table class="widefat striped">
                                                <thead>
                                                    <tr>
                                                        <th style="width:30%">Item Name</th>
                                                        <th style="width:12%">VK-Preis</th>
                                                        <th style="width:13%">Ankauf?</th>
                                                        <th style="width:18%">Modus</th>
                                                        <th style="width:12%">Wert</th>
                                                        <th style="width:15%">Ankaufspreis</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php foreach ($items_to_edit as $item):
                                                        $s_enabled = !empty($item->sell_enabled);
                                                        $s_mode    = $item->sell_price_mode  ?? 'percent';
                                                        $s_value   = $item->sell_price_value ?? 80;
                                                    ?>
                                                    <tr id="sell_row_<?php echo $item->id; ?>">
                                                        <td>
                                                            <strong><?php echo esc_html($item->name); ?></strong><br>
                                                            <small style="color:#666"><?php echo esc_html($item->item_id); ?></small>
                                                        </td>
                                                        <td><?php echo esc_html($item->price); ?> <?php echo esc_html($currency); ?></td>
                                                        <td>
                                                            <input type="checkbox"
                                                                   name="item_sell_enabled[<?php echo $item->id; ?>]"
                                                                   value="1"
                                                                   <?php checked($s_enabled); ?>
                                                                   onchange="wisUpdateSellRow(<?php echo $item->id; ?>, <?php echo intval($item->price); ?>)">
                                                        </td>
                                                        <td>
                                                            <select name="item_sell_mode[<?php echo $item->id; ?>]"
                                                                    id="sell_mode_<?php echo $item->id; ?>"
                                                                    onchange="wisUpdateSellRow(<?php echo $item->id; ?>, <?php echo intval($item->price); ?>)"
                                                                    style="width:100%">
                                                                <option value="percent" <?php selected($s_mode, 'percent'); ?>>% vom VK</option>
                                                                <option value="minus"   <?php selected($s_mode, 'minus');   ?>>VK minus</option>
                                                                <option value="fixed"   <?php selected($s_mode, 'fixed');   ?>>Fixpreis</option>
                                                            </select>
                                                        </td>
                                                        <td>
                                                            <input type="number"
                                                                   name="item_sell_value[<?php echo $item->id; ?>]"
                                                                   id="sell_value_<?php echo $item->id; ?>"
                                                                   value="<?php echo esc_attr($s_value); ?>"
                                                                   min="0" style="width:70px"
                                                                   oninput="wisUpdateSellRow(<?php echo $item->id; ?>, <?php echo intval($item->price); ?>)">
                                                        </td>
                                                        <td id="sell_preview_<?php echo $item->id; ?>" style="color:#0073aa;font-weight:bold;">
                                                            <?php
                                                            if ($s_enabled) {
                                                                if ($s_mode === 'percent') $sp = round($item->price * $s_value / 100, 2);
                                                                elseif ($s_mode === 'minus') $sp = max(0, $item->price - $s_value);
                                                                else $sp = max(0, $s_value);
                                                                echo number_format($sp, 2) . ' ' . esc_html($currency);
                                                            } else {
                                                                echo '<span style="color:#aaa">–</span>';
                                                            }
                                                            ?>
                                                        </td>
                                                    </tr>
                                                    <?php endforeach; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                        <script>
                                        function wisUpdateSellRow(id, vk) {
                                            var enabled = document.querySelector('input[name="item_sell_enabled[' + id + ']"]').checked;
                                            var mode    = document.getElementById('sell_mode_'    + id).value;
                                            var val     = parseFloat(document.getElementById('sell_value_'   + id).value) || 0;
                                            var preview = document.getElementById('sell_preview_' + id);
                                            if (!enabled) { preview.innerHTML = '<span style="color:#aaa">–</span>'; return; }
                                            var price = 0;
                                            if      (mode === 'percent') price = Math.max(0, Math.round(vk * val) / 100);
                                            else if (mode === 'minus')   price = Math.max(0, vk - val);
                                            else                          price = Math.max(0, val);
                                            preview.textContent = price.toFixed(2) + ' <?php echo esc_js($currency); ?>';
                                        }
                                        // Initiale Vorschau für alle Zeilen
                                        document.addEventListener('DOMContentLoaded', function() {
                                            <?php foreach ($items_to_edit as $item): ?>
                                            wisUpdateSellRow(<?php echo $item->id; ?>, <?php echo intval($item->price); ?>);
                                            <?php endforeach; ?>
                                        });
                                        </script>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </table>
                        
                        <p class="submit">
                            <a href="<?php echo admin_url('admin.php?page=wis_items'); ?>" class="button">Abbrechen</a>
                            <input type="submit" name="wis_bulk_save" class="button button-primary" value="Jetzt ändern">
                        </p>
                    </form>
                </div>
            </div>
            <?php
            return;
        }
        
        if (isset($_GET['action'], $_GET['id'], $_GET['_wpnonce'])) {
            if (!wp_verify_nonce($_GET['_wpnonce'], 'wis_item_action')) {
                wp_die('Security check failed');
            }
            
            $id = intval($_GET['id']);
            
            if ($_GET['action'] === 'delete') {
                WIS_DB::delete_item($id);
                echo '<div class="updated"><p>✅ Item gelöscht!</p></div>';
            } elseif ($_GET['action'] === 'toggle_status') {
                $item = WIS_DB::get_item($id);
                $new_status = ($item->status === 'publish') ? 'draft' : 'publish';
                WIS_DB::update_item($id, ['status' => $new_status]);
                echo '<div class="updated"><p>✅ Status geändert!</p></div>';
            }
        }
        
        // Fehlermeldung aus handle_save_item() anzeigen (falls Validierung fehlschlug)
        $save_error = get_transient('wis_save_error_' . get_current_user_id());
        if ($save_error) {
            delete_transient('wis_save_error_' . get_current_user_id());
            echo '<div class="error"><p>' . esc_html($save_error) . '</p></div>';
        }

        if (isset($_GET['created'])) {
            echo '<div class="updated"><p>✅ Item erfolgreich erstellt!</p></div>';
        }
        if (isset($_GET['saved'])) {
            echo '<div class="updated"><p>✅ Item gespeichert!</p></div>';
        }

        if (isset($_GET['edit']) || isset($_GET['add'])) {
            $item = isset($_GET['edit']) ? WIS_DB::get_item(intval($_GET['edit'])) : null;
            $servers = WIS_DB::get_servers();
            $categories = WIS_DB::get_categories();
            $currency = get_option('wis_currency_name', 'Coins');
            
            $item_servers = $item ? json_decode($item->servers, true) : [];
            $item_cats = $item ? json_decode($item->categories, true) : [];
            
            if (!$item && isset($_GET['add'])) {
                $item_cats = [];
            }
            
            ?>
            <div class="wrap">
                <h1><?php echo $item ? 'Item bearbeiten' : 'Neues Item'; ?></h1>
                <a href="<?php echo admin_url('admin.php?page=wis_items'); ?>" class="button">← Zurück zur Liste</a>
                
                <form method="post" style="max-width:800px; margin-top:20px;">
                    <?php wp_nonce_field('wis_item_form'); ?>
                    
                    <table class="form-table">
                        <tr>
                            <th><label for="name">Name *</label></th>
                            <td><input type="text" id="name" name="name" value="<?php echo $item ? esc_attr($item->name) : ''; ?>" class="regular-text" required></td>
                        </tr>
                        <tr>
                            <th><label for="item_id">Item ID / Typ *</label></th>
                            <td>
                                <?php
                                $fly_ids  = ['fly_5min','fly_15min','fly_30min','fly_1h','fly_2h','fly_3h'];
                                $is_fly   = $item && in_array($item->item_id, $fly_ids);
                                $is_rank  = $item && preg_match('/^rank_([^_]+(?:_[^_]+)*)_([a-zA-Z0-9_\-]+)_([a-zA-Z0-9_\-]+)_(\d+)$/', $item->item_id, $rm);
                                // Format: rank_{rank_id}_{lp_group}_{default_group}_{days}
                                // Fallback für altes Format rank_{rank_id}_{days}
                                $is_rank_old = !$is_rank && $item && preg_match('/^rank_(.+)_(\d+)$/', $item->item_id, $rm_old);
                                $is_fly_abo  = $item && ($item->item_id === 'fly_abo' || preg_match('/^fly_abo_\d+$/', $item->item_id));
                                $cur_abo_days = 0;
                                $is_plot_slots = $item && preg_match('/^plot_slots_(\d+)$/', $item->item_id, $ps_m);
                                $cur_plot_extra_slots = $is_plot_slots ? intval($ps_m[1]) : 1;
                                $is_plot_abo  = $item && preg_match('/^plot_abo_(\d+)$/', $item->item_id, $pa_m);
                                $cur_plot_abo_slots = $is_plot_abo ? intval($pa_m[1]) : 1;
                                $cur_rank_id       = $is_rank ? $rm[1] : ($is_rank_old ? $rm_old[1] : 'vip');
                                $cur_lp_group      = $is_rank ? $rm[2] : $cur_rank_id;
                                $cur_default_group = $is_rank ? $rm[3] : 'default';
                                $cur_rank_days     = $is_rank ? intval($rm[4]) : ($is_rank_old ? intval($rm_old[2]) : 30);
                                $cur_label         = $item ? esc_attr($item->name) : '';
                                $detected_type = $is_fly ? 'fly' : (($is_rank || $is_rank_old) ? 'rank' : ($is_fly_abo ? 'fly_abo' : ($is_plot_slots ? 'plot_slots' : ($is_plot_abo ? 'plot_abo' : 'minecraft'))));
                                ?>
                                <select id="item_type" name="item_type" onchange="wisToggleItemType(this.value)" style="margin-bottom:8px;">
                                    <option value="minecraft" <?php echo $detected_type === 'minecraft' ? 'selected' : ''; ?>>Minecraft Item</option>
                                    <option value="fly"       <?php echo $detected_type === 'fly'       ? 'selected' : ''; ?>>✈ Fly-Gutschein</option>
                                    <option value="rank"      <?php echo $detected_type === 'rank'      ? 'selected' : ''; ?>>👑 Rang (LuckPerms)</option>
                                    <option value="fly_abo"   <?php echo $detected_type === 'fly_abo'   ? 'selected' : ''; ?>>✈ Fly-Abo (tägl. Limit)</option>
                                    <option value="plot_slots" <?php echo $detected_type === 'plot_slots' ? 'selected' : ''; ?>>📦 Plot-Slots (einmalig)</option>
                                    <option value="plot_abo"   <?php echo $detected_type === 'plot_abo'   ? 'selected' : ''; ?>>📦 Plot-Abo (monatlich)</option>
                                </select>

                                <div id="wis_item_minecraft">
                                    <input type="text" id="item_id" name="item_id" value="<?php echo ($item && !$is_fly && !$is_rank && !$is_fly_abo && !$is_plot_slots && !$is_plot_abo) ? esc_attr($item->item_id) : ''; ?>" class="regular-text" placeholder="minecraft:diamond">
                                    <p class="description">Z.B.: minecraft:diamond (Kategorien werden automatisch zugewiesen)</p>
                                </div>

                                <div id="wis_item_fly" style="display:none;">
                                    <select name="fly_duration" id="fly_duration">
                                        <option value="fly_5min"  <?php echo ($item && $item->item_id === 'fly_5min')  ? 'selected' : ''; ?>>✈  5 Minuten Fly</option>
                                        <option value="fly_15min" <?php echo ($item && $item->item_id === 'fly_15min') ? 'selected' : ''; ?>>✈ 15 Minuten Fly</option>
                                        <option value="fly_30min" <?php echo ($item && $item->item_id === 'fly_30min') ? 'selected' : ''; ?>>✈ 30 Minuten Fly</option>
                                        <option value="fly_1h"    <?php echo ($item && $item->item_id === 'fly_1h')    ? 'selected' : ''; ?>>✈  1 Stunde Fly</option>
                                        <option value="fly_2h"    <?php echo ($item && $item->item_id === 'fly_2h')    ? 'selected' : ''; ?>>✈  2 Stunden Fly</option>
                                        <option value="fly_3h"    <?php echo ($item && $item->item_id === 'fly_3h')    ? 'selected' : ''; ?>>✈  3 Stunden Fly</option>
                                    </select>
                                    <p class="description">Der Spieler bekommt nach dem Kauf Fly für die gewählte Dauer.</p>
                                </div>

                                <div id="wis_item_rank" style="display:none;">
                                    <table style="border-collapse:collapse;">
                                        <tr>
                                            <td style="padding:4px 10px 4px 0;"><label for="rank_id"><strong>Rang-ID:</strong></label></td>
                                            <td><input type="text" id="rank_id" name="rank_id" value="<?php echo esc_attr($cur_rank_id); ?>" class="regular-text" placeholder="vip" style="width:160px;"></td>
                                            <td style="padding:4px 0 4px 12px; color:#666; font-size:12px;">Interner Name (frei wählbar, z.&nbsp;B. <code>vip</code>)</td>
                                        </tr>
                                        <tr>
                                            <td style="padding:4px 10px 4px 0;"><label for="lp_group"><strong>LuckPerms-Gruppe:</strong></label></td>
                                            <td><input type="text" id="lp_group" name="lp_group" value="<?php echo esc_attr($cur_lp_group); ?>" class="regular-text" placeholder="vip" style="width:160px;"></td>
                                            <td style="padding:4px 0 4px 12px; color:#666; font-size:12px;">Exakter Gruppenname in LuckPerms</td>
                                        </tr>
                                        <tr>
                                            <td style="padding:4px 10px 4px 0;"><label for="default_group"><strong>Standard-Gruppe (nach Ablauf):</strong></label></td>
                                            <td><input type="text" id="default_group" name="default_group" value="<?php echo esc_attr($cur_default_group); ?>" class="regular-text" placeholder="default" style="width:160px;"></td>
                                            <td style="padding:4px 0 4px 12px; color:#666; font-size:12px;">Gruppe nach Rang-Ablauf (z.&nbsp;B. <code>default</code>)</td>
                                        </tr>
                                        <tr>
                                            <td style="padding:4px 10px 4px 0;"><label for="rank_days"><strong>Laufzeit (Tage):</strong></label></td>
                                            <td>
                                                <input type="number" id="rank_days" name="rank_days" value="<?php echo esc_attr($cur_rank_days); ?>" min="0" style="width:80px;">
                                                <span class="description" style="margin-left:8px;">0 = dauerhaft</span>
                                            </td>
                                        </tr>
                                    </table>
                                    <p class="description" style="margin-top:6px;">
                                        Der Spielername im Shop (Feld <em>Name</em> oben) wird dem Spieler als Rang-Label angezeigt.<br>
                                        Gespeicherte Item-ID: <code>rank_{rang-id}_{lp-gruppe}_{standard-gruppe}_{tage}</code>
                                    </p>
                                </div>

                                <div id="wis_item_fly_abo" style="display:none;">
                                    <p class="description" style="margin-top:4px; padding:10px; background:#f0f6fc; border-left:3px solid #2271b1; border-radius:2px;">
                                        <strong>✈ Fly-Abo – monatliches Abonnement</strong><br><br>
                                        Spieler zahlen den <strong>Artikelpreis einmalig beim Kauf</strong>.<br>
                                        Danach wird am <strong>1. jedes Monats</strong> automatisch der gleiche Betrag per Vault abgebucht.<br>
                                        Kann der Spieler nicht zahlen, wird das Abo automatisch gekündigt.<br>
                                        Kündigung jederzeit ingame mit <code>/flyabocancel</code> – läuft bis Monatsende.<br><br>
                                        <strong>Artikelpreis</strong> = monatlicher Betrag &nbsp;|&nbsp;
                                        Tägl. Fly-Limit: konfigurierbar per <code>fly-abo.max-daily-hours</code> im Plugin.<br>
                                        Gespeicherte Item-ID: <code>fly_abo</code>
                                    </p>
                                </div>

                                <div id="wis_item_plot_slots" style="display:none;">
                                    <table style="border-collapse:collapse;">
                                        <tr>
                                            <td style="padding:4px 10px 4px 0;"><label for="plot_extra_slots"><strong>Zusätzliche Plot-Slots:</strong></label></td>
                                            <td>
                                                <input type="number" id="plot_extra_slots" name="plot_extra_slots"
                                                    value="<?php echo esc_attr($cur_plot_extra_slots); ?>"
                                                    min="1" style="width:80px;">
                                                <span style="margin-left:6px; color:#666;">Slots</span>
                                            </td>
                                        </tr>
                                    </table>
                                    <p class="description" style="margin-top:6px; padding:8px; background:#f6f7f7; border-left:3px solid #50b56a; border-radius:2px;">
                                        <strong>📦 Plot-Slots – einmaliger Kauf (permanent)</strong><br><br>
                                        Der Spieler erhält dauerhaft zusätzliche Plot-Slots on top seines Rang-Limits.<br>
                                        LuckPerms Meta <code>plotlimit</code> wird automatisch gesetzt.<br>
                                        Gespeicherte Item-ID: <code>plot_slots_{anzahl}</code>
                                    </p>
                                </div>

                                <div id="wis_item_plot_abo" style="display:none;">
                                    <table style="border-collapse:collapse;">
                                        <tr>
                                            <td style="padding:4px 10px 4px 0;"><label for="plot_abo_slots"><strong>Zusätzliche Plot-Slots (Abo):</strong></label></td>
                                            <td>
                                                <input type="number" id="plot_abo_slots" name="plot_abo_slots"
                                                    value="<?php echo esc_attr($cur_plot_abo_slots); ?>"
                                                    min="1" style="width:80px;">
                                                <span style="margin-left:6px; color:#666;">Slots</span>
                                            </td>
                                        </tr>
                                    </table>
                                    <p class="description" style="margin-top:6px; padding:8px; background:#f0f6fc; border-left:3px solid #2271b1; border-radius:2px;">
                                        <strong>📦 Plot-Abo – monatliches Abonnement</strong><br><br>
                                        Spieler zahlen den <strong>Artikelpreis einmalig beim Kauf</strong>.<br>
                                        Am <strong>1. jedes Monats</strong> wird der gleiche Betrag per Vault abgebucht.<br>
                                        Bei Zahlungsausfall: Abo-Slots verfallen, überzählige Plots werden eingefroren.<br>
                                        Kündigung ingame mit <code>/plotabocancel confirm</code> – läuft bis Monatsende.<br><br>
                                        <strong>Artikelpreis</strong> = monatlicher Beitrag<br>
                                        Gespeicherte Item-ID: <code>plot_abo_{anzahl}</code>
                                    </p>
                                </div>

                                <script>
                                function wisToggleItemType(val) {
                                    document.getElementById('wis_item_minecraft').style.display  = (val === 'minecraft')  ? '' : 'none';
                                    document.getElementById('wis_item_fly').style.display        = (val === 'fly')        ? '' : 'none';
                                    document.getElementById('wis_item_rank').style.display       = (val === 'rank')       ? '' : 'none';
                                    document.getElementById('wis_item_fly_abo').style.display    = (val === 'fly_abo')    ? '' : 'none';
                                    document.getElementById('wis_item_plot_slots').style.display = (val === 'plot_slots') ? '' : 'none';
                                    document.getElementById('wis_item_plot_abo').style.display   = (val === 'plot_abo')   ? '' : 'none';
                                    document.getElementById('item_id').required = (val === 'minecraft');
                                }
                                wisToggleItemType(document.getElementById('item_type').value);
                                </script>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="custom_image_url">Bild-URL (optional)</label></th>
                            <td>
                                <input type="url" id="custom_image_url" name="custom_image_url"
                                    value="<?php echo ($item && !empty($item->custom_image_url)) ? esc_attr($item->custom_image_url) : ''; ?>"
                                    class="large-text" placeholder="https://example.com/mein-fly-bild.png"
                                    oninput="wisPreviewImage(this.value)">
                                <p class="description">
                                    Eigene Bild-URL – überschreibt die automatische Minecraft-Item-ID-URL.<br>
                                    Ideal für Fly-Gutscheine, VIP-Pakete etc. Leer lassen = Standard.
                                </p>
                                <div id="wis_img_preview" style="margin-top:8px; <?php echo ($item && !empty($item->custom_image_url)) ? '' : 'display:none;'; ?>">
                                    <img id="wis_img_preview_img"
                                         src="<?php echo ($item && !empty($item->custom_image_url)) ? esc_url($item->custom_image_url) : ''; ?>"
                                         style="max-height:80px; border-radius:6px; border:1px solid #ddd; padding:4px; background:#2d2d2d;"
                                         alt="Vorschau"
                                         onerror="document.getElementById('wis_img_preview').style.display='none';">
                                    <span style="margin-left:8px; color:#666; font-size:12px;">Vorschau</span>
                                </div>
                                <script>
                                function wisPreviewImage(url) {
                                    var box = document.getElementById('wis_img_preview');
                                    var img = document.getElementById('wis_img_preview_img');
                                    if (url) {
                                        img.src = url;
                                        box.style.display = '';
                                    } else {
                                        box.style.display = 'none';
                                    }
                                }
                                </script>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="description">Beschreibung</label></th>
                            <td><textarea id="description" name="description" rows="3" class="large-text"><?php echo $item ? esc_textarea($item->description) : ''; ?></textarea></td>
                        </tr>
                        <tr>
                            <th><label for="price">Preis (<?php echo esc_html($currency); ?>) *</label></th>
                            <td><input type="number" id="price" name="price" value="<?php echo $item ? esc_attr($item->price) : '0'; ?>" min="0" required></td>
                        </tr>
                        <tr>
                            <th><label for="offer_price">Angebotspreis</label></th>
                            <td>
                                <input type="number" id="offer_price" name="offer_price" value="<?php echo $item ? esc_attr($item->offer_price) : '0'; ?>" min="0">
                                <p class="description">Optional: Wenn gesetzt, wird der normale Preis durchgestrichen</p>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="sell_enabled">Ankauf aktivieren</label></th>
                            <td>
                                <label>
                                    <input type="checkbox" id="sell_enabled" name="sell_enabled" value="1"
                                        <?php echo ($item && !empty($item->sell_enabled)) ? 'checked' : ''; ?>
                                        onchange="wisToggleSell(this.checked)">
                                    Spieler können dieses Item an den Shop verkaufen
                                </label>
                            </td>
                        </tr>
                        <tr id="wis_sell_row" <?php echo ($item && !empty($item->sell_enabled)) ? '' : 'style="display:none"'; ?>>
                            <th><label for="sell_price_mode">Ankaufspreis</label></th>
                            <td>
                                <select id="sell_price_mode" name="sell_price_mode" onchange="wisUpdateSellPreview()">
                                    <option value="percent" <?php echo ($item && $item->sell_price_mode === 'percent') ? 'selected' : ''; ?>>% vom Verkaufspreis</option>
                                    <option value="minus"   <?php echo ($item && $item->sell_price_mode === 'minus')   ? 'selected' : ''; ?>>Verkaufspreis minus fixer Betrag</option>
                                    <option value="fixed"   <?php echo ($item && $item->sell_price_mode === 'fixed')   ? 'selected' : ''; ?>>Fixer Preis</option>
                                </select>
                                &nbsp;
                                <input type="number" id="sell_price_value" name="sell_price_value" min="0"
                                    value="<?php echo $item ? esc_attr($item->sell_price_value ?? 80) : '80'; ?>"
                                    style="width:80px" oninput="wisUpdateSellPreview()">
                                <span id="wis_sell_preview" style="margin-left:8px;color:#0073aa;"></span>
                                <p class="description">
                                    Beispiele: 80&nbsp;%&nbsp;→&nbsp;80&nbsp;% des VK-Preises &nbsp;|&nbsp;
                                    Modus «minus 10»&nbsp;→&nbsp;VK-Preis&nbsp;−&nbsp;10&nbsp;|&nbsp;
                                    Fixer Preis 15&nbsp;→&nbsp;immer&nbsp;15&nbsp;<?php echo esc_html($currency); ?>
                                </p>
                                <script>
                                function wisToggleSell(on) {
                                    document.getElementById('wis_sell_row').style.display = on ? '' : 'none';
                                    if (on) wisUpdateSellPreview();
                                }
                                function wisUpdateSellPreview() {
                                    var vk    = parseFloat(document.getElementById('price').value) || 0;
                                    var mode  = document.getElementById('sell_price_mode').value;
                                    var val   = parseFloat(document.getElementById('sell_price_value').value) || 0;
                                    var price = 0;
                                    if (mode === 'percent') price = Math.max(0, Math.round(vk * val) / 100);
                                    else if (mode === 'minus') price = Math.max(0, vk - val);
                                    else price = Math.max(0, val);
                                    document.getElementById('wis_sell_preview').textContent =
                                        '→ Ankaufspreis: ' + price.toFixed(2) + ' <?php echo esc_js($currency); ?>';
                                }
                                document.getElementById('price').addEventListener('input', wisUpdateSellPreview);
                                wisUpdateSellPreview();
                                </script>
                            </td>
                        </tr>
                        <tr>
                            <th>Markierungen</th>
                            <td>
                                <label>
                                    <input type="checkbox" name="is_offer" value="1" <?php echo ($item && $item->is_offer) ? 'checked' : ''; ?>>
                                    Als Angebot markieren
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <th>Server</th>
                            <td>
                                <?php if (empty($servers)): ?>
                                    <p>Keine Server vorhanden. <a href="<?php echo admin_url('admin.php?page=wis_servers'); ?>">Server erstellen</a></p>
                                <?php else: ?>
                                    <?php foreach ($servers as $server): ?>
                                        <label style="display:block; margin:5px 0;">
                                            <input type="checkbox" name="servers[]" value="<?php echo esc_attr($server->slug); ?>" <?php echo in_array($server->slug, $item_servers ?: []) ? 'checked' : ''; ?>>
                                            <?php echo esc_html($server->name); ?>
                                        </label>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <tr>
                            <th>Kategorien</th>
                            <td>
                                <?php if (empty($categories)): ?>
                                    <p>Keine Kategorien vorhanden.</p>
                                <?php else: ?>
                                    <?php foreach ($categories as $cat): ?>
                                        <label style="display:block; margin:5px 0;">
                                            <input type="checkbox" name="categories[]" value="<?php echo esc_attr($cat->slug); ?>" <?php echo in_array($cat->slug, $item_cats ?: []) ? 'checked' : ''; ?>>
                                            <?php echo esc_html($cat->name); ?>
                                        </label>
                                    <?php endforeach; ?>
                                    <p class="description">Bei neuem Item werden Kategorien automatisch basierend auf der Item-ID gesetzt</p>
                                <?php endif; ?>
                            </td>
                        </tr>
                    </table>
                    
                    <p class="submit">
                        <input type="submit" name="wis_save_item" class="button button-primary" value="Speichern">
                    </p>
                </form>
            </div>
            <?php
            return;
        }
        
        // Liste mit Suche + Pagination
        $categories       = WIS_DB::get_categories();
        $current_category = isset($_GET['wis_category']) ? sanitize_text_field($_GET['wis_category']) : '';
        $search_query     = isset($_GET['wis_search'])   ? sanitize_text_field($_GET['wis_search'])   : '';
        $per_page         = 24;
        $current_page     = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;

        $fetch_args = []; // kein Status-Filter → alle Items zählen
        if (!empty($current_category)) $fetch_args['category_slug'] = $current_category;
        if (!empty($search_query))     $fetch_args['search']        = $search_query;

        $total_items = WIS_DB::count_items($fetch_args);
        $total_pages = max(1, (int) ceil($total_items / $per_page));
        $current_page = min($current_page, $total_pages);
        $offset = ($current_page - 1) * $per_page;

        global $wpdb;
        $table_items = $wpdb->prefix . 'wis_items';
        // Admin-Liste: alle Items anzeigen (publish + draft), Query ohne doppeltes prepare()
        $where_parts = [];
        $where_vals  = [];
        if (!empty($current_category)) {
            $where_parts[] = "categories LIKE %s";
            $where_vals[]  = '%"' . $wpdb->esc_like($current_category) . '"%';
        }
        if (!empty($search_query)) {
            $like = '%' . $wpdb->esc_like($search_query) . '%';
            $where_parts[] = "(name LIKE %s OR item_id LIKE %s)";
            $where_vals[]  = $like;
            $where_vals[]  = $like;
        }
        $where_sql   = !empty($where_parts) ? 'WHERE ' . implode(' AND ', $where_parts) : '';
        $where_vals[] = $per_page;
        $where_vals[] = $offset;
        $items = $wpdb->get_results(
            $wpdb->prepare(
                "SELECT * FROM $table_items $where_sql ORDER BY name ASC LIMIT %d OFFSET %d",
                $where_vals
            )
        );

        $currency = get_option('wis_currency_name', 'Coins');

        $base_url = admin_url('admin.php?page=wis_items');
        if (!empty($current_category)) $base_url .= '&wis_category=' . urlencode($current_category);
        if (!empty($search_query))     $base_url .= '&wis_search='   . urlencode($search_query);
        ?>
        <div class="wrap">
            <h1>Items
                <a href="<?php echo admin_url('admin.php?page=wis_items&add=1'); ?>" class="page-title-action">Neu erstellen</a>
            </h1>

            <div style="margin:10px 0 15px; display:flex; align-items:center; gap:10px; flex-wrap:wrap;">
                <form method="get" style="display:flex; gap:8px; align-items:center; flex-wrap:wrap;">
                    <input type="hidden" name="page" value="wis_items">
                    <?php if (!empty($current_category)): ?>
                        <input type="hidden" name="wis_category" value="<?php echo esc_attr($current_category); ?>">
                    <?php endif; ?>
                    <input
                        type="search"
                        name="wis_search"
                        value="<?php echo esc_attr($search_query); ?>"
                        placeholder="🔍 Item suchen (Name oder ID)…"
                        class="regular-text"
                        style="min-width:280px; padding:6px 10px;">
                    <input type="submit" class="button" value="Suchen">
                    <?php if (!empty($search_query)): ?>
                        <a href="<?php echo esc_url(admin_url('admin.php?page=wis_items' . (!empty($current_category) ? '&wis_category=' . urlencode($current_category) : ''))); ?>" class="button">✕ Zurücksetzen</a>
                    <?php endif; ?>
                </form>
                <span style="color:#666; font-size:13px;">
                    <?php echo $total_items; ?> Item(s) gefunden
                    <?php if (!empty($search_query)): ?>
                        – Suche: <strong><?php echo esc_html($search_query); ?></strong>
                    <?php endif; ?>
                </span>
            </div>
            
            <nav class="nav-tab-wrapper">
                <a href="<?php echo admin_url('admin.php?page=wis_items' . (!empty($search_query) ? '&wis_search=' . urlencode($search_query) : '')); ?>" class="nav-tab <?php echo $current_category === '' ? 'nav-tab-active' : ''; ?>">
                    Alle
                </a>
                <?php foreach ($categories as $cat): ?>
                    <a href="<?php echo admin_url('admin.php?page=wis_items&wis_category=' . $cat->slug . (!empty($search_query) ? '&wis_search=' . urlencode($search_query) : '')); ?>" class="nav-tab <?php echo $current_category === $cat->slug ? 'nav-tab-active' : ''; ?>">
                        <?php echo esc_html($cat->name); ?>
                    </a>
                <?php endforeach; ?>
            </nav>
            <br>
            
            <form method="post">
                <div class="tablenav top">
                    <div class="alignleft actions bulkactions">
                        <label for="bulk-action-selector-top" class="screen-reader-text">Massenaktionen wählen</label>
                        <select name="wis_bulk_action" id="bulk-action-selector-top">
                            <option value="">Massenaktionen</option>
                            <option value="price">Preis ändern</option>
                            <option value="offer">Angebot ändern</option>
                            <option value="server">Server zuweisen</option>
                            <option value="category">Kategorie zuweisen</option>
                            <option value="status">Aktivieren/Deaktivieren</option>
                            <option value="sell">Ankauf konfigurieren</option>
                        </select>
                        <input type="submit" name="wis_bulk_apply" class="button action" value="Anwenden">
                    </div>

                    <div class="tablenav-pages" style="float:right; margin-top:4px;">
                        <?php if ($total_pages > 1): ?>
                            <span class="displaying-num"><?php echo $total_items; ?> Einträge</span>
                            <span class="pagination-links">
                                <?php if ($current_page > 1): ?>
                                    <a class="button" href="<?php echo esc_url($base_url . '&paged=1'); ?>">«</a>
                                    <a class="button" href="<?php echo esc_url($base_url . '&paged=' . ($current_page - 1)); ?>">‹</a>
                                <?php endif; ?>
                                <span class="paging-input" style="margin:0 6px;">
                                    Seite <strong><?php echo $current_page; ?></strong> von <strong><?php echo $total_pages; ?></strong>
                                </span>
                                <?php if ($current_page < $total_pages): ?>
                                    <a class="button" href="<?php echo esc_url($base_url . '&paged=' . ($current_page + 1)); ?>">›</a>
                                    <a class="button" href="<?php echo esc_url($base_url . '&paged=' . $total_pages); ?>">»</a>
                                <?php endif; ?>
                            </span>
                        <?php else: ?>
                            <span class="displaying-num"><?php echo $total_items; ?> Einträge</span>
                        <?php endif; ?>
                    </div>
                    <br class="clear">
                </div>
                
                <table class="widefat fixed striped">
                    <thead>
                        <tr>
                            <th style="width:40px;" class="manage-column column-cb check-column">
                                <input type="checkbox" id="cb-select-all-1">
                            </th>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Item ID</th>
                            <th>Preis</th>
                            <th>Status</th>
                            <th>Aktionen</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($items)): ?>
                            <tr><td colspan="7" style="text-align:center; padding:40px;">
                                <?php echo !empty($search_query) ? 'Keine Items für diese Suche gefunden.' : 'Keine Items in dieser Kategorie gefunden.'; ?>
                            </td></tr>
                        <?php else: ?>
                            <?php foreach ($items as $item): ?>
                                <tr>
                                    <th class="check-column">
                                        <input type="checkbox" name="item_ids[]" id="cb-select-<?php echo $item->id; ?>" value="<?php echo $item->id; ?>">
                                    </th>
                                    <td><?php echo esc_html($item->id); ?></td>
                                    <td><strong><?php echo esc_html($item->name); ?></strong></td>
                                    <td>
                                        <?php
                                        if (preg_match('/^rank_([^_]+)_([a-zA-Z0-9_\-]+)_([a-zA-Z0-9_\-]+)_(\d+)$/', $item->item_id, $rm2)) {
                                            $rd = intval($rm2[4]);
                                            echo '<span title="' . esc_attr($item->item_id) . '">👑 LP: <code>' . esc_html($rm2[2]) . '</code> &mdash; ' . ($rd === 0 ? '<em>dauerhaft</em>' : esc_html($rd) . ' Tage') . '</span>';
                                        } elseif (preg_match('/^rank_(.+)_(\d+)$/', $item->item_id, $rm2)) {
                                            $rd = intval($rm2[2]);
                                            echo '<span title="' . esc_attr($item->item_id) . '">👑 <code>' . esc_html($rm2[1]) . '</code> &mdash; ' . ($rd === 0 ? '<em>dauerhaft</em>' : esc_html($rd) . ' Tage') . '</span>';
                                        } elseif ($item->item_id === 'fly_abo' || preg_match('/^fly_abo_/', $item->item_id)) {
                                            echo '<span title="' . esc_attr($item->item_id) . '">✈ Fly-Abo &mdash; monatlich</span>';
                                        } elseif (preg_match('/^plot_slots_(\d+)$/', $item->item_id, $plt_m)) {
                                            echo '<span title="' . esc_attr($item->item_id) . '">📦 Plot-Slots &mdash; +' . esc_html($plt_m[1]) . ' permanent</span>';
                                        } elseif (preg_match('/^plot_abo_(\d+)$/', $item->item_id, $plt_m)) {
                                            echo '<span title="' . esc_attr($item->item_id) . '">📦 Plot-Abo &mdash; +' . esc_html($plt_m[1]) . ' monatlich</span>';
                                        } else {
                                            echo '<code>' . esc_html($item->item_id) . '</code>';
                                        }
                                        ?>
                                    </td>
                                    <td><?php echo esc_html($item->price); ?> <?php echo esc_html($currency); ?></td>
                                    <td>
                                        <?php if ($item->status === 'publish'): ?>
                                            <span style="color:green;">✅ Aktiv</span>
                                        <?php else: ?>
                                            <span style="color:orange;">📝 Entwurf</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=wis_items&edit=' . $item->id), 'wis_item_action', '_wpnonce'); ?>" class="button button-small">Bearbeiten</a>
                                        <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=wis_items&action=delete&id=' . $item->id), 'wis_item_action', '_wpnonce'); ?>" class="button button-small" onclick="return confirm('Wirklich löschen?');" style="color:red;">Löschen</a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>

                <?php if ($total_pages > 1): ?>
                <div class="tablenav bottom">
                    <div class="tablenav-pages" style="float:right; margin-top:8px;">
                        <span class="displaying-num"><?php echo $total_items; ?> Einträge</span>
                        <span class="pagination-links">
                            <?php if ($current_page > 1): ?>
                                <a class="button" href="<?php echo esc_url($base_url . '&paged=1'); ?>">«</a>
                                <a class="button" href="<?php echo esc_url($base_url . '&paged=' . ($current_page - 1)); ?>">‹</a>
                            <?php endif; ?>
                            <span class="paging-input" style="margin:0 6px;">
                                Seite <strong><?php echo $current_page; ?></strong> von <strong><?php echo $total_pages; ?></strong>
                            </span>
                            <?php if ($current_page < $total_pages): ?>
                                <a class="button" href="<?php echo esc_url($base_url . '&paged=' . ($current_page + 1)); ?>">›</a>
                                <a class="button" href="<?php echo esc_url($base_url . '&paged=' . $total_pages); ?>">»</a>
                            <?php endif; ?>
                        </span>
                    </div>
                    <br class="clear">
                </div>
                <?php endif; ?>
            </form>
        </div>
        
        <script>
        (function() {
            const mainCb = document.getElementById('cb-select-all-1');
            if(mainCb) {
                mainCb.addEventListener('change', function(e) {
                    const cbs = document.querySelectorAll('input[name="item_ids[]"]');
                    cbs.forEach(cb => cb.checked = e.target.checked);
                });
            }
        })();
        </script>
        <?php
    }
    
    public static function page_servers() {
        if (isset($_POST['wis_add_server'])) {
            check_admin_referer('wis_server_form');
            WIS_DB::insert_server($_POST['slug'], $_POST['name']);
            echo '<div class="updated"><p>✅ Server erstellt!</p></div>';
        }
        
        if (isset($_GET['action'], $_GET['id']) && $_GET['action'] === 'delete') {
            check_admin_referer('wis_server_action', '_wpnonce');
            WIS_DB::delete_server(intval($_GET['id']));
            echo '<div class="updated"><p>✅ Server gelöscht!</p></div>';
        }
        
        $servers = WIS_DB::get_servers();
        ?>
        <div class="wrap">
            <h1>Server</h1>
            
            <div class="card" style="max-width:600px; padding:20px; margin-top:20px;">
                <h2>Neuen Server erstellen</h2>
                <form method="post">
                    <?php wp_nonce_field('wis_server_form'); ?>
                    <table class="form-table">
                        <tr>
                            <th><label for="name">Name *</label></th>
                            <td><input type="text" id="name" name="name" class="regular-text" required></td>
                        </tr>
                        <tr>
                            <th><label for="slug">Slug *</label></th>
                            <td>
                                <input type="text" id="slug" name="slug" class="regular-text" placeholder="survival" required>
                                <p class="description">Kleinbuchstaben ohne Leerzeichen</p>
                            </td>
                        </tr>
                    </table>
                    <p class="submit">
                        <input type="submit" name="wis_add_server" class="button button-primary" value="Server erstellen">
                    </p>
                </form>
            </div>
            
            <h2 style="margin-top:40px;">Vorhandene Server</h2>
            <table class="widefat fixed striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Slug</th>
                        <th>Aktionen</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($servers)): ?>
                        <tr><td colspan="4" style="text-align:center; padding:40px;">Noch keine Server vorhanden.</td></tr>
                    <?php else: ?>
                        <?php foreach ($servers as $server): ?>
                            <tr>
                                <td><?php echo esc_html($server->id); ?></td>
                                <td><strong><?php echo esc_html($server->name); ?></strong></td>
                                <td><code><?php echo esc_html($server->slug); ?></code></td>
                                <td>
                                    <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=wis_servers&action=delete&id=' . $server->id), 'wis_server_action', '_wpnonce'); ?>" class="button button-small" onclick="return confirm('Wirklich löschen?');" style="color:red;">Löschen</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php
    }
    
    public static function page_categories() {
        if (isset($_POST['wis_add_category'])) {
            check_admin_referer('wis_category_form');
            WIS_DB::insert_category($_POST['name']);
            echo '<div class="updated"><p>✅ Kategorie erstellt!</p></div>';
        }
        
        if (isset($_GET['action'], $_GET['id']) && $_GET['action'] === 'delete') {
            check_admin_referer('wis_category_action', '_wpnonce');
            WIS_DB::delete_category(intval($_GET['id']));
            echo '<div class="updated"><p>✅ Kategorie gelöscht!</p></div>';
        }
        
        $categories = WIS_DB::get_categories();
        ?>
        <div class="wrap">
            <h1>Kategorien</h1>
            
            <div class="card" style="max-width:600px; padding:20px; margin-top:20px;">
                <h2>Neue Kategorie erstellen</h2>
                <form method="post">
                    <?php wp_nonce_field('wis_category_form'); ?>
                    <table class="form-table">
                        <tr>
                            <th><label for="name">Name *</label></th>
                            <td><input type="text" id="name" name="name" class="regular-text" required></td>
                        </tr>
                    </table>
                    <p class="submit">
                        <input type="submit" name="wis_add_category" class="button button-primary" value="Kategorie erstellen">
                    </p>
                </form>
            </div>
            
            <h2 style="margin-top:40px;">Vorhandene Kategorien</h2>
            <table class="widefat fixed striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Slug</th>
                        <th>Aktionen</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($categories)): ?>
                        <tr><td colspan="4" style="text-align:center; padding:40px;">Noch keine Kategorien vorhanden.</td></tr>
                    <?php else: ?>
                        <?php foreach ($categories as $cat): ?>
                            <tr>
                                <td><?php echo esc_html($cat->id); ?></td>
                                <td><strong><?php echo esc_html($cat->name); ?></strong></td>
                                <td><code><?php echo esc_html($cat->slug); ?></code></td>
                                <td>
                                    <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=wis_categories&action=delete&id=' . $cat->id), 'wis_category_action', '_wpnonce'); ?>" class="button button-small" onclick="return confirm('Wirklich löschen?');" style="color:red;">Löschen</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php
    }
    
    public static function page_coupons() {
        if (isset($_POST['wis_save_coupon'])) {
            check_admin_referer('wis_coupon_form');
            
            $data = [
                'code' => sanitize_text_field($_POST['code']),
                'value' => intval($_POST['value']),
                'type' => sanitize_text_field($_POST['type']),
                'usage_limit' => intval($_POST['usage_limit']),
                'expiry' => !empty($_POST['expiry']) ? sanitize_text_field($_POST['expiry']) : null
            ];
            
            if (isset($_GET['edit'])) {
                unset($data['used_count']);
                WIS_DB::update_coupon(intval($_GET['edit']), $data);
                echo '<div class="updated"><p>✅ Gutschein gespeichert!</p></div>';
            } else {
                $data['used_count'] = 0;
                WIS_DB::insert_coupon($data);
                echo '<div class="updated"><p>✅ Gutschein erstellt!</p></div>';
            }
        }
        
        if (isset($_GET['action'], $_GET['id']) && $_GET['action'] === 'delete') {
            check_admin_referer('wis_coupon_action', '_wpnonce');
            WIS_DB::delete_coupon(intval($_GET['id']));
            echo '<div class="updated"><p>✅ Gutschein gelöscht!</p></div>';
        }
        
        if (isset($_GET['add']) || isset($_GET['edit'])) {
            global $wpdb;
            $coupon = isset($_GET['edit']) ? $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wis_coupons WHERE id = %d", intval($_GET['edit']))) : null;
            $currency = get_option('wis_currency_name', 'Coins');
            ?>
            <div class="wrap">
                <h1><?php echo $coupon ? 'Gutschein bearbeiten' : 'Neuer Gutschein'; ?></h1>
                <a href="<?php echo admin_url('admin.php?page=wis_coupons'); ?>" class="button">← Zurück zur Liste</a>
                
                <form method="post" style="max-width:600px; margin-top:20px;">
                    <?php wp_nonce_field('wis_coupon_form'); ?>
                    <table class="form-table">
                        <tr>
                            <th><label for="code">Code *</label></th>
                            <td>
                                <input type="text" id="code" name="code" value="<?php echo $coupon ? esc_attr($coupon->code) : ''; ?>" class="regular-text" style="text-transform:uppercase;" required>
                                <p class="description">Z.B.: SUMMER20</p>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="type">Typ *</label></th>
                            <td>
                                <select id="type" name="type" required>
                                    <option value="fixed" <?php echo ($coupon && $coupon->type === 'fixed') ? 'selected' : ''; ?>>Festbetrag (z.B. 100 <?php echo esc_html($currency); ?> Rabatt)</option>
                                    <option value="percent" <?php echo ($coupon && $coupon->type === 'percent') ? 'selected' : ''; ?>>Prozentual (z.B. 20% Rabatt)</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="value">Wert *</label></th>
                            <td>
                                <input type="number" id="value" name="value" value="<?php echo $coupon ? esc_attr($coupon->value) : ''; ?>" min="1" required>
                                <p class="description">Bei Festbetrag: Betrag in <?php echo esc_html($currency); ?>. Bei Prozent: Zahl ohne %</p>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="usage_limit">Nutzungslimit *</label></th>
                            <td>
                                <input type="number" id="usage_limit" name="usage_limit" value="<?php echo $coupon ? esc_attr($coupon->usage_limit) : '1'; ?>" min="1" required>
                            </td>
                        </tr>
                        <tr>
                            <th><label for="expiry">Ablaufdatum</label></th>
                            <td>
                                <input type="date" id="expiry" name="expiry" value="<?php echo $coupon && $coupon->expiry ? esc_attr($coupon->expiry) : ''; ?>">
                                <p class="description">Optional</p>
                            </td>
                        </tr>
                    </table>
                    <p class="submit">
                        <input type="submit" name="wis_save_coupon" class="button button-primary" value="Speichern">
                    </p>
                </form>
            </div>
            <?php
            return;
        }
        
        $coupons = WIS_DB::get_coupons();
        $currency = get_option('wis_currency_name', 'Coins');
        ?>
        <div class="wrap">
            <h1>Gutscheine <a href="<?php echo admin_url('admin.php?page=wis_coupons&add=1'); ?>" class="page-title-action">Neu erstellen</a></h1>
            
            <table class="widefat fixed striped">
                <thead>
                    <tr>
                        <th>Code</th>
                        <th>Rabatt</th>
                        <th>Genutzt</th>
                        <th>Gültig bis</th>
                        <th>Aktionen</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($coupons)): ?>
                        <tr><td colspan="5" style="text-align:center; padding:40px;">Noch keine Gutscheine vorhanden.</td></tr>
                    <?php else: ?>
                        <?php foreach ($coupons as $coupon): ?>
                            <tr>
                                <td><strong><?php echo esc_html($coupon->code); ?></strong></td>
                                <td>
                                    <?php if ($coupon->type === 'percent'): ?>
                                        <?php echo esc_html($coupon->value); ?>%
                                    <?php else: ?>
                                        <?php echo esc_html($coupon->value); ?> <?php echo esc_html($currency); ?>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo esc_html($coupon->used_count); ?> / <?php echo esc_html($coupon->usage_limit); ?></td>
                                <td><?php echo $coupon->expiry ? esc_html(date('d.m.Y', strtotime($coupon->expiry))) : '∞'; ?></td>
                                <td>
                                    <a href="<?php echo admin_url('admin.php?page=wis_coupons&edit=' . $coupon->id); ?>" class="button button-small">Bearbeiten</a>
                                    <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=wis_coupons&action=delete&id=' . $coupon->id), 'wis_coupon_action', '_wpnonce'); ?>" class="button button-small" onclick="return confirm('Wirklich löschen?');" style="color:red;">Löschen</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php
    }
    
    public static function page_orders() {
        if (isset($_GET['action'], $_GET['id']) && $_GET['action'] === 'delete') {
            check_admin_referer('wis_order_action', '_wpnonce');
            WIS_DB::delete_order(intval($_GET['id']));
            echo '<div class="updated"><p>✅ Bestellung gelöscht!</p></div>';
        }
        
        if (isset($_GET['action'], $_GET['id']) && $_GET['action'] === 'complete') {
            check_admin_referer('wis_order_action', '_wpnonce');
            WIS_DB::update_order_status(intval($_GET['id']), 'completed');
            echo '<div class="updated"><p>✅ Status geändert!</p></div>';
        }
        
        if (isset($_GET['view'])) {
            $order = WIS_DB::get_order(intval($_GET['view']));
            if (!$order) {
                echo '<div class="error"><p>Bestellung nicht gefunden.</p></div>';
                return;
            }
            
            $currency = get_option('wis_currency_name', 'Coins');
            $status_colors = [
                'pending' => '#ffc107',
                'processing' => '#0073aa',
                'completed' => 'green',
                'cancelled' => 'red',
                'failed' => 'red'
            ];
            $status_labels = [
                'pending' => 'Warte auf Ingame',
                'processing' => 'In Bearbeitung',
                'completed' => 'Erledigt',
                'cancelled' => 'Abgebrochen',
                'failed' => 'Fehler'
            ];
            
            ?>
            <div class="wrap">
                <h1>Bestellung #<?php echo $order->id; ?> - Details</h1>
                <a href="<?php echo admin_url('admin.php?page=wis_orders'); ?>" class="button">← Zurück</a>
                
                <table class="widefat" style="max-width:800px; margin-top:20px;">
                    <tr><th>ID</th><td><?php echo esc_html($order->id); ?></td></tr>
                    <tr><th>Datum</th><td><?php echo esc_html(date('d.m.Y H:i', strtotime($order->created_at))); ?></td></tr>
                    <tr><th>Spieler</th><td><strong><?php echo esc_html($order->player_name); ?></strong></td></tr>
                    <tr><th>Server</th><td><?php echo esc_html($order->server); ?></td></tr>
                    <tr><th>Zusammenfassung</th><td><?php echo esc_html($order->item_title); ?></td></tr>
                    <tr><th>Preis</th><td><?php echo esc_html($order->price); ?> <?php echo esc_html($currency); ?></td></tr>
                    <tr><th>Status</th><td style="color:<?php echo $status_colors[$order->status] ?? 'black'; ?>; font-weight:bold;"><?php echo $status_labels[$order->status] ?? $order->status; ?></td></tr>
                    <tr>
                        <th>Details (JSON)</th>
                        <td><code style="display:block; background:#eee; padding:10px; font-size:11px; overflow-x:auto;"><?php echo esc_html($order->response); ?></code></td>
                    </tr>
                </table>
            </div>
            <?php
            return;
        }
        
        $orders = WIS_DB::get_orders();
        $currency = get_option('wis_currency_name', 'Coins');
        ?>
        <div class="wrap">
            <h1>Bestellungen</h1>
            
            <table class="widefat fixed striped">
                <thead>
                    <tr>
                        <th style="width:140px;">Datum</th>
                        <th style="width:150px;">Spieler</th>
                        <th>Inhalt</th>
                        <th style="width:100px;">Preis</th>
                        <th style="width:120px;">Status</th>
                        <th style="width:200px;">Aktionen</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($orders)): ?>
                        <tr><td colspan="6" style="text-align:center; padding:40px;">Noch keine Bestellungen vorhanden.</td></tr>
                    <?php else: ?>
                        <?php foreach ($orders as $order): ?>
                            <?php
                            $status_map = [
                                'pending'    => 'Warte',
                                'claimed'    => '📡 Abgeholt',
                                'processing' => 'Geben...',
                                'completed'  => 'Fertig',
                                'cancelled'  => 'Abgebrochen',
                                'failed'     => 'Fehler'
                            ];
                            $status_colors = [
                                'pending'    => '#ffc107',
                                'claimed'    => '#17a2b8',
                                'processing' => '#0073aa',
                                'completed'  => 'green',
                                'cancelled'  => 'red',
                                'failed'     => 'red'
                            ];
                            ?>
                            <tr>
                                <td><?php echo esc_html(date('d.m.Y H:i', strtotime($order->created_at))); ?></td>
                                <td><strong><?php echo esc_html($order->player_name); ?></strong></td>
                                <td><?php echo esc_html(substr($order->item_title, 0, 50)) . (strlen($order->item_title) > 50 ? '...' : ''); ?></td>
                                <td><?php echo esc_html($order->price); ?> <?php echo esc_html($currency); ?></td>
                                <td style="color:<?php echo $status_colors[$order->status] ?? 'black'; ?>; font-weight:bold;"><?php echo $status_map[$order->status] ?? $order->status; ?></td>
                                <td>
                                    <a href="<?php echo admin_url('admin.php?page=wis_orders&view=' . $order->id); ?>" class="button button-small">👁️ Details</a>
                                    <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=wis_orders&action=delete&id=' . $order->id), 'wis_order_action', '_wpnonce'); ?>" class="button button-small" onclick="return confirm('Wirklich löschen?');" style="color:red;">🗑️ Löschen</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php
    }
    
    public static function page_json() {
        if (isset($_POST['wis_generate_json'])) {
            check_admin_referer('wis_json_export');
            
            $items = WIS_DB::get_items(['status' => 'publish']);
            $img_base = get_option('wis_image_base_url', '');
            
            $json_data = ['items' => []];
            
            foreach ($items as $item) {
                $json_data['items'][] = [
                    'id' => $item->item_id,
                    'name' => $item->name,
                    'description' => $item->description,
                    'price' => intval($item->price),
                    'image' => WIS_DB::get_item_image($item)
                ];
            }
            
            $json_output = json_encode($json_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            
            echo '<div class="wrap"><h1>📦 JSON Export</h1>';
            echo '<div class="updated"><p>✅ JSON erfolgreich generiert!</p></div>';
            echo '<textarea style="width:100%; height:400px; font-family:monospace; font-size:12px;">'.esc_textarea($json_output).'</textarea>';
            echo '<p><button onclick="downloadJSON()" class="button button-primary">💾 Als items.json herunterladen</button></p>';
            echo '<script>
            function downloadJSON() {
                const text = document.querySelector("textarea").value;
                const blob = new Blob([text], {type: "application/json"});
                const url = URL.createObjectURL(blob);
                const a = document.createElement("a");
                a.href = url;
                a.download = "items.json";
                a.click();
            }
            </script>';
            echo '<h3>📤 Nächste Schritte:</h3>';
            echo '<ol>';
            echo '<li>Lade die JSON-Datei herunter</li>';
            echo '<li>Gehe zu deinem Gitea Repository</li>';
            echo '<li>Lade die <code>items.json</code> hoch unter: <code>https://git.viper.ipv64.net/M_Viper/WP-Ingame-Shop-Pro</code></li>';
            echo '<li>Klicke dann auf den <strong>Quick-Import</strong> Button unten!</li>';
            echo '</ol>';
            echo '</div>';
            return;
        }
        
        $default_url = 'https://git.viper.ipv64.net/M_Viper/WP-Ingame-Shop-Pro/raw/branch/main/items.json';
        ?>
        <div class="wrap">
            <h1>📦 JSON Export/Import</h1>
            
            <div class="card" style="max-width:800px; padding:20px; margin-top:20px;">
                <h2>📤 JSON Export</h2>
                <p>Generiere eine JSON-Datei mit allen deinen Items für Gitea.</p>
                <form method="post">
                    <?php wp_nonce_field('wis_json_export'); ?>
                    <p class="submit">
                        <input type="submit" name="wis_generate_json" class="button button-primary button-large" value="📦 JSON Generieren">
                    </p>
                </form>
            </div>
            
            <div class="card" style="max-width:800px; padding:20px; margin-top:20px; background:#e8f5e9;">
                <h2>⚡ Quick-Import von Gitea</h2>
                <p><strong>Importiert direkt von deinem Gitea Repository!</strong></p>
                <p style="margin:10px 0; padding:10px; background:#fff; border-left:4px solid #28a745; font-family:monospace; font-size:12px; word-break:break-all;">
                    <?php echo esc_html($default_url); ?>
                </p>
                <button type="button" id="btn-quick-import" class="button button-primary button-large" style="background:#28a745; border-color:#28a745;">
                    ⚡ Quick-Import starten
                </button>
                <div id="quick-import-result" style="margin-top:15px;"></div>
            </div>
            
            <div class="card" style="max-width:800px; padding:20px; margin-top:20px;">
                <h2>📥 JSON Import (Manuelle URL)</h2>
                <p>Importiere Items aus einer beliebigen JSON-URL.</p>
                
                <div id="wis-import-form">
                    <input type="text" id="import-url" class="large-text code" value="<?php echo esc_attr($default_url); ?>" style="margin-bottom:15px;">
                    <br>
                    <button type="button" id="btn-import" class="button button-primary button-large">📥 Importieren</button>
                    <div id="import-result" style="margin-top:15px;"></div>
                </div>
            </div>
        </div>
        
        <script>
        document.getElementById('btn-quick-import').addEventListener('click', function() {
            const url = '<?php echo esc_js($default_url); ?>';
            const resultDiv = document.getElementById('quick-import-result');
            const btn = this;
            
            btn.disabled = true;
            btn.textContent = '⏳ Importiere von Gitea...';
            resultDiv.innerHTML = '<div style="padding:10px; background:#fff3cd; border-left:4px solid #ffc107; margin-top:10px;">⏳ Lade Items von Gitea...</div>';
            
            fetch('<?php echo rest_url('wis/v1/import_json'); ?>', {
                method: 'POST',
                headers: {
                    'Content-Type':'application/json',
                    'X-WP-Nonce': '<?php echo wp_create_nonce('wp_rest'); ?>'
                },
                body: JSON.stringify({url: url})
            })
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    resultDiv.innerHTML = '<div class="updated" style="margin-top:10px;"><p><strong>✅ Erfolgreich!</strong><br>' + data.imported + ' Items importiert! (' + data.skipped + ' übersprungen)</p></div>';
                    setTimeout(() => {
                        window.location.href = '<?php echo admin_url('admin.php?page=wis_items'); ?>';
                    }, 2000);
                } else {
                    resultDiv.innerHTML = '<div class="error" style="margin-top:10px;"><p><strong>❌ Fehler:</strong><br>' + data.message + '</p></div>';
                }
            })
            .catch(e => {
                resultDiv.innerHTML = '<div class="error" style="margin-top:10px;"><p><strong>❌ Netzwerkfehler:</strong><br>' + e.message + '</p></div>';
            })
            .finally(() => {
                btn.disabled = false;
                btn.textContent = '⚡ Quick-Import starten';
            });
        });
        
        document.getElementById('btn-import').addEventListener('click', function() {
            const url = document.getElementById('import-url').value.trim();
            const resultDiv = document.getElementById('import-result');
            
            if (!url) {
                resultDiv.innerHTML = '<div class="error"><p>Bitte URL eingeben!</p></div>';
                return;
            }
            
            this.disabled = true;
            this.textContent = '⏳ Importiere...';
            
            fetch('<?php echo rest_url('wis/v1/import_json'); ?>', {
                method: 'POST',
                headers: {
                    'Content-Type':'application/json',
                    'X-WP-Nonce': '<?php echo wp_create_nonce('wp_rest'); ?>'
                },
                body: JSON.stringify({url: url})
            })
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    resultDiv.innerHTML = '<div class="updated"><p>✅ ' + data.imported + ' Items importiert! (' + data.skipped + ' übersprungen)</p></div>';
                    setTimeout(() => {
                        window.location.href = '<?php echo admin_url('admin.php?page=wis_items'); ?>';
                    }, 2000);
                } else {
                    resultDiv.innerHTML = '<div class="error"><p>❌ Fehler: ' + data.message + '</p></div>';
                }
            })
            .catch(e => {
                resultDiv.innerHTML = '<div class="error"><p>❌ Netzwerkfehler: ' + e.message + '</p></div>';
            })
            .finally(() => {
                this.disabled = false;
                this.textContent = '📥 Importieren';
            });
        });
        </script>
        <?php
    }
    
    public static function page_reset() {
        if (isset($_POST['wis_confirm_reset'])) {
            check_admin_referer('wis_reset');
            WIS_Activator::reset_shop();
            echo '<div class="updated"><p>✅ Shop wurde komplett zurückgesetzt!</p></div>';
        }
        
        ?>
        <div class="wrap">
            <h1>🔄 Shop Reset</h1>
            
            <div class="card" style="max-width:800px; padding:20px; background:#fff3cd;">
                <h2 style="color:#856404;">⚠️ WARNUNG</h2>
                <p><strong>Diese Aktion löscht ALLE Daten:</strong></p>
                <ul>
                    <li>❌ Alle Items</li>
                    <li>❌ Alle Bestellungen</li>
                    <li>❌ Alle Gutscheine</li>
                    <li>❌ Alle Server</li>
                    <li>❌ Alle Kategorien</li>
                </ul>
                <p style="color:red; font-weight:bold;">Diese Aktion kann NICHT rückgängig gemacht werden!</p>
                
                <form method="post" onsubmit="return confirm('WIRKLICH ALLE DATEN LÖSCHEN? Dies kann nicht rückgängig gemacht werden!');">
                    <?php wp_nonce_field('wis_reset'); ?>
                    <p class="submit">
                        <input type="submit" name="wis_confirm_reset" class="button button-secondary button-large" value="🗑️ Shop jetzt zurücksetzen">
                    </p>
                </form>
            </div>
        </div>
        <?php
    }
    
    public static function page_top_spenders() {
        global $wpdb;
        $currency = get_option('wis_currency_name', 'Coins');
        
        $results = $wpdb->get_results("
            SELECT player_name, SUM(price) as total_spent, COUNT(*) as order_count
            FROM {$wpdb->prefix}wis_orders 
            WHERE status = 'completed'
            GROUP BY player_name 
            ORDER BY total_spent DESC 
            LIMIT 50
        ");
        
        ?>
        <div class="wrap">
            <h1>🏆 Top Spender</h1>
            <p>Spieler mit den höchsten Gesamtausgaben</p>
            
            <table class="widefat fixed striped">
                <thead>
                    <tr>
                        <th style="width:80px;">Rang</th>
                        <th>Spieler</th>
                        <th style="width:150px;">Ausgegeben</th>
                        <th style="width:150px;">Bestellungen</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($results)): ?>
                        <tr><td colspan="4" style="text-align:center; padding:40px;">Noch keine Statistiken vorhanden.</td></tr>
                    <?php else: ?>
                        <?php $rank = 1; foreach ($results as $row): ?>
                            <tr>
                                <td><strong>#<?php echo $rank++; ?></strong></td>
                                <td><?php echo esc_html($row->player_name); ?></td>
                                <td><?php echo esc_html(number_format($row->total_spent)); ?> <?php echo esc_html($currency); ?></td>
                                <td><?php echo esc_html($row->order_count); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php
    }
}

// ===========================================================
// REST API
// ===========================================================
class WIS_API {
    public static function register_routes() {
        register_rest_route('wis/v1', '/import_json', [
            'methods'             => 'POST',
            'callback'            => [self::class, 'import_json'],
            'permission_callback' => '__return_true',
        ]);
        register_rest_route('wis/v1', '/order', [
            'methods'             => 'POST',
            'callback'            => [self::class, 'create_order'],
            'permission_callback' => '__return_true',
        ]);
        register_rest_route('wis/v1', '/validate_coupon', [
            'methods'             => 'POST',
            'callback'            => [self::class, 'validate_coupon'],
            'permission_callback' => '__return_true',
        ]);
        register_rest_route('wis/v1', '/shop_items', [
            'methods'             => 'GET',
            'callback'            => [self::class, 'get_shop_items'],
            'permission_callback' => '__return_true',
        ]);
        register_rest_route('wis/v1', '/pending_orders', [
            'methods'             => 'GET',
            'callback'            => [self::class, 'get_pending_orders'],
            'permission_callback' => [WIS_Activator::class, 'spigot_permission'],
        ]);
        register_rest_route('wis/v1', '/execute_order', [
            'methods'             => 'POST',
            'callback'            => [self::class, 'execute_order'],
            'permission_callback' => [WIS_Activator::class, 'spigot_permission'],
        ]);
        register_rest_route('wis/v1', '/complete_order', [
            'methods'             => 'POST',
            'callback'            => [self::class, 'complete_order'],
            'permission_callback' => [WIS_Activator::class, 'spigot_permission'],
        ]);
        register_rest_route('wis/v1', '/cancel_order', [
            'methods'             => 'POST',
            'callback'            => [self::class, 'cancel_order'],
            'permission_callback' => [WIS_Activator::class, 'spigot_permission'],
        ]);
        register_rest_route('wis/v1', '/pending_offline', [
            'methods'             => 'GET',
            'callback'            => [self::class, 'get_pending_offline'],
            'permission_callback' => [WIS_Activator::class, 'spigot_permission'],
        ]);
        register_rest_route('wis/v1', '/orders_history', [
            'methods'             => 'GET',
            'callback'            => [self::class, 'get_orders_history'],
            'permission_callback' => [WIS_Activator::class, 'spigot_permission'],
        ]);

        // Ankauf-Endpunkte (ab v6.5)
        register_rest_route('wis/v1', '/sell_items', [
            'methods'             => 'GET',
            'callback'            => [self::class, 'get_sell_items'],
            'permission_callback' => [WIS_Activator::class, 'spigot_permission'],
        ]);
        register_rest_route('wis/v1', '/sell_item', [
            'methods'             => 'POST',
            'callback'            => [self::class, 'process_sell'],
            'permission_callback' => [WIS_Activator::class, 'spigot_permission'],
        ]);
    }


    // =========================================================
    // ANKAUF – Endpunkte (ab v6.5)
    // =========================================================

    /**
     * GET /wis/v1/sell_items?server=<slug>
     * Liefert alle Items die auf dem angegebenen Server ankaufbar sind.
     */
    public static function get_sell_items($request) {
        global $wpdb;
        $server   = sanitize_text_field($request->get_param('server') ?? '');
        $currency = get_option('wis_currency_name', 'Coins');
        $table    = $wpdb->prefix . 'wis_items';

        $items = $wpdb->get_results(
            "SELECT id, item_id, name, price, sell_price_mode, sell_price_value
               FROM $table
              WHERE status = 'publish' AND sell_enabled = 1"
        );

        $result = [];
        foreach ($items as $item) {
            // Ankaufspreis berechnen
            $sell_price = self::calc_sell_price(
                (int) $item->price,
                $item->sell_price_mode,
                (int) $item->sell_price_value
            );
            if ($sell_price <= 0) continue;

            // Server-Filter
            if (!empty($server)) {
                $servers = $wpdb->get_var($wpdb->prepare(
                    "SELECT servers FROM $table WHERE id = %d", $item->id
                ));
                $srv_list = json_decode($servers, true) ?: [];
                if (!empty($srv_list) && !in_array(strtolower($server), array_map('strtolower', $srv_list))) {
                    continue;
                }
            }

            $result[] = [
                'item_id'    => $item->item_id,
                'name'       => $item->name,
                'buy_price'  => (int) $item->price,
                'sell_price' => $sell_price,
            ];
        }

        return new WP_REST_Response([
            'items'    => $result,
            'currency' => $currency,
        ]);
    }

    /**
     * POST /wis/v1/sell_item
     * Body: { "player": "Steve", "server": "survival", "item_id": "minecraft:diamond", "quantity": 5 }
     * Antwortet mit dem Betrag der gutgeschrieben werden soll.
     */
    public static function process_sell($request) {
        global $wpdb;
        $player   = sanitize_text_field($request->get_param('player')   ?? '');
        $server   = sanitize_text_field($request->get_param('server')   ?? '');
        $item_id  = sanitize_text_field($request->get_param('item_id')  ?? '');
        $quantity = max(1, intval($request->get_param('quantity') ?? 1));

        if (!$player || !$item_id) {
            return new WP_REST_Response(['success' => false, 'message' => 'Fehlende Parameter'], 400);
        }

        $table = $wpdb->prefix . 'wis_items';

        // Item suchen (case-insensitive, mit/ohne minecraft: prefix)
        $clean_id = strtolower(str_replace('minecraft:', '', $item_id));
        $row = $wpdb->get_row($wpdb->prepare(
            "SELECT id, item_id, name, price, sell_enabled, sell_price_mode, sell_price_value, servers
               FROM $table
              WHERE sell_enabled = 1
                AND (LOWER(item_id) = %s OR LOWER(REPLACE(item_id,'minecraft:','')) = %s)
              LIMIT 1",
            strtolower($item_id), $clean_id
        ));

        if (!$row) {
            return new WP_REST_Response(['success' => false, 'message' => 'Item nicht ankaufbar'], 404);
        }

        $sell_price = self::calc_sell_price(
            (int) $row->price,
            $row->sell_price_mode,
            (int) $row->sell_price_value
        );

        if ($sell_price <= 0) {
            return new WP_REST_Response(['success' => false, 'message' => 'Ankaufspreis ist 0'], 422);
        }

        $total = round($sell_price * $quantity, 2);

        // Sell-Log schreiben
        $wpdb->insert($wpdb->prefix . 'wis_sell_log', [
            'player_name'   => $player,
            'server'        => $server,
            'item_id'       => $row->item_id,
            'item_name'     => $row->name,
            'quantity'      => $quantity,
            'price_per_item' => $sell_price,
            'total_paid'    => $total,
        ]);

        return new WP_REST_Response([
            'success'        => true,
            'item_id'        => $row->item_id,
            'item_name'      => $row->name,
            'quantity'       => $quantity,
            'price_per_item' => $sell_price,
            'total'          => $total,
        ]);
    }

    /**
     * Berechnet den Ankaufspreis aus Verkaufspreis + Modus.
     * mode = "percent"  → value ist ein Prozentwert des VK-Preises  (z.B. 80 = 80 %)
     * mode = "fixed"    → value ist ein absoluter Festpreis
     * mode = "minus"    → value ist ein absoluter Abzug vom VK-Preis
     */
    private static function calc_sell_price(int $buy_price, string $mode, int $value): float {
        switch ($mode) {
            case 'fixed':
                return max(0, $value);
            case 'minus':
                return max(0, $buy_price - $value);
            case 'percent':
            default:
                return max(0, round($buy_price * $value / 100, 2));
        }
    }

    public static function get_shop_items($request) {
        $page     = max(1, intval($request->get_param('page') ?? 1));
        $per_page = 24;
        $category = sanitize_text_field($request->get_param('category') ?? '');
        $search   = sanitize_text_field($request->get_param('search') ?? '');

        global $wpdb;
        $table = $wpdb->prefix . 'wis_items';
        $where_parts = ["status = 'publish'"];

        if (!empty($category)) {
            $search_pattern = '%"' . $category . '"%';
            $where_parts[] = $wpdb->prepare("categories LIKE %s", $search_pattern);
        }
        if (!empty($search)) {
            $search_like = '%' . $wpdb->esc_like($search) . '%';
            $where_parts[] = $wpdb->prepare("(name LIKE %s OR item_id LIKE %s)", $search_like, $search_like);
        }

        $where_sql   = implode(" AND ", $where_parts);
        $total       = (int) $wpdb->get_var("SELECT COUNT(*) FROM $table WHERE $where_sql");
        $offset      = ($page - 1) * $per_page;
        $items       = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM $table WHERE $where_sql ORDER BY name ASC LIMIT %d OFFSET %d",
            $per_page, $offset
        ));

        $img_base = get_option('wis_image_base_url', '');
        $currency = get_option('wis_currency_name', 'Coins');

        $result = [];
        foreach ($items as $item) {
            $result[] = [
                'id'          => $item->id,
                'item_id'     => $item->item_id,
                'name'        => $item->name,
                'description' => $item->description,
                'price'       => intval($item->price),
                'offer_price' => intval($item->offer_price),
                'is_offer'    => (bool) $item->is_offer,
                'is_daily_deal' => (bool) $item->is_daily_deal,
                'servers'     => json_decode($item->servers, true) ?: [],
                'categories'  => json_decode($item->categories, true) ?: [],
                'image'            => WIS_DB::get_item_image($item),
                'has_custom_image' => !empty($item->custom_image_url),
            ];
        }

        // Fly-Items nach Dauer sortieren: 5min→15min→30min→1h→2h→3h
        $fly_order = ['fly_5min'=>1,'fly_15min'=>2,'fly_30min'=>3,'fly_1h'=>4,'fly_2h'=>5,'fly_3h'=>6];
        usort($result, function($a, $b) use ($fly_order) {
            $ap = isset($fly_order[$a['item_id']]) ? $fly_order[$a['item_id']] : 999;
            $bp = isset($fly_order[$b['item_id']]) ? $fly_order[$b['item_id']] : 999;
            if ($ap !== 999 || $bp !== 999) return $ap - $bp;
            return strcmp($a['name'], $b['name']);
        });

        return new WP_REST_Response([
            'items'       => $result,
            'total'       => $total,
            'page'        => $page,
            'per_page'    => $per_page,
            'total_pages' => max(1, (int) ceil($total / $per_page)),
            'currency'    => $currency,
        ]);
    }
    
    public static function get_pending_orders($request) {
        global $wpdb;
        $player = sanitize_text_field($request->get_param('player'));

        if (!$player) {
            return new WP_REST_Response(['orders' => []]);
        }

        $table = $wpdb->prefix . 'wis_orders';

        $results = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM $table
             WHERE player_name = %s AND status = 'pending'
             ORDER BY created_at ASC LIMIT 5",
            $player
        ));

        if (!empty($results)) {
            $ids = implode(',', array_map(fn($o) => intval($o->id), $results));
            $wpdb->query("UPDATE $table SET status = 'claimed' WHERE id IN ($ids) AND status = 'pending'");
        }

        return new WP_REST_Response(['orders' => $results]);
    }

    public static function get_pending_offline($request) {
        if (get_option('wis_offline_queue_enabled', '0') !== '1') {
            return new WP_REST_Response(['orders' => [], 'message' => 'Offline-Queue deaktiviert']);
        }

        global $wpdb;
        $player = sanitize_text_field($request->get_param('player'));

        if (!$player) {
            return new WP_REST_Response(['orders' => []]);
        }

        $table   = $wpdb->prefix . 'wis_orders';
        $results = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM $table
             WHERE player_name = %s AND status = 'pending'
             ORDER BY created_at ASC LIMIT 10",
            $player
        ));

        if (!empty($results)) {
            $ids = implode(',', array_map(fn($o) => intval($o->id), $results));
            $wpdb->query("UPDATE $table SET status = 'claimed' WHERE id IN ($ids) AND status = 'pending'");
        }

        return new WP_REST_Response(['orders' => $results]);
    }

    public static function get_orders_history($request) {
        global $wpdb;
        $player = sanitize_text_field($request->get_param('player'));

        if (!$player) {
            return new WP_REST_Response(['orders' => []]);
        }

        $results = $wpdb->get_results($wpdb->prepare(
            "SELECT id, item_title, price, status, created_at
             FROM {$wpdb->prefix}wis_orders
             WHERE player_name = %s
             ORDER BY created_at DESC LIMIT 10",
            $player
        ));

        return new WP_REST_Response(['orders' => $results]);
    }

    public static function execute_order($request) {
        global $wpdb;
        $data = $request->get_json_params();
        $id   = intval($data['id'] ?? 0);

        if (!$id) return new WP_REST_Response(['success' => false], 400);

        $wpdb->query($wpdb->prepare(
            "UPDATE {$wpdb->prefix}wis_orders SET status = 'processing'
             WHERE id = %d AND status IN ('pending','claimed')",
            $id
        ));

        return new WP_REST_Response(['success' => true]);
    }

    public static function complete_order($request) {
        global $wpdb;
        $data = $request->get_json_params();
        $id = intval($data['id'] ?? 0);
        
        if (!$id) return new WP_REST_Response(['success' => false], 400);
        
        $wpdb->update(
            $wpdb->prefix . 'wis_orders', 
            ['status' => 'completed'], 
            ['id' => $id]
        );
        
        return new WP_REST_Response(['success' => true]);
    }

    public static function cancel_order($request) {
        global $wpdb;
        $data = $request->get_json_params();
        $id = intval($data['id'] ?? 0);
        
        if (!$id) return new WP_REST_Response(['success' => false], 400);
        
        $wpdb->update(
            $wpdb->prefix . 'wis_orders', 
            ['status' => 'cancelled'], 
            ['id' => $id]
        );
        
        return new WP_REST_Response(['success' => true]);
    }
    
    public static function import_json($request) {
        $data = $request->get_json_params();
        $url = esc_url_raw($data['url'] ?? '');
        
        if (!$url) {
            return new WP_REST_Response(['success' => false, 'message' => 'Keine URL angegeben'], 400);
        }
        
        $response = wp_remote_get($url, ['timeout' => 30]);
        
        if (is_wp_error($response)) {
            return new WP_REST_Response(['success' => false, 'message' => $response->get_error_message()], 400);
        }
        
        $body = wp_remote_retrieve_body($response);
        $json = json_decode($body, true);
        
        if (!isset($json['items']) || !is_array($json['items'])) {
            return new WP_REST_Response(['success' => false, 'message' => 'Ungültiges JSON Format'], 400);
        }
        
        $imported = 0;
        $skipped = 0;
        
        foreach ($json['items'] as $item) {
            $item_id = sanitize_text_field($item['id'] ?? '');
            $name = sanitize_text_field($item['name'] ?? 'Unbekannt');
            
            if (empty($item_id)) continue;
            
            $exists = WIS_DB::get_item_by_item_id($item_id);
            
            if ($exists) {
                $skipped++;
                continue;
            }
            
            WIS_DB::insert_item([
                'item_id' => $item_id,
                'name' => $name,
                'description' => sanitize_textarea_field($item['description'] ?? ''),
                'price' => intval($item['price'] ?? 0),
                'status' => intval($item['price'] ?? 0) > 0 ? 'publish' : 'draft',
                'servers' => '[]',
                'categories' => '[]'
            ]);
            
            $imported++;
        }
        
        return new WP_REST_Response(['success' => true, 'imported' => $imported, 'skipped' => $skipped]);
    }
    
    public static function create_order($request) {
        global $wpdb;
        $data = $request->get_json_params();
        
        $player = sanitize_text_field($data['player'] ?? '');
        $cart = $data['cart'] ?? [];
        $server = sanitize_text_field($data['server'] ?? '');
        $coupon_code = isset($data['coupon_code']) ? sanitize_text_field(strtoupper($data['coupon_code'])) : '';
        
        if (!$player || empty($cart) || !$server) {
            return new WP_REST_Response(['success' => false, 'message' => 'Fehlende Daten'], 400);
        }
        
        $exclude_offers = get_option('wis_coupon_exclude_offers', '0');
        $currency = get_option('wis_currency_name', 'Coins');
        
        $valid_cart = [];
        $total_normal = 0;
        $total_offer = 0;
        
        foreach ($cart as $item_data) {
            $item = WIS_DB::get_item(intval($item_data['id'] ?? 0));
            if (!$item || $item->status !== 'publish') continue;
            
            $qty = intval($item_data['quantity'] ?? 1);
            if ($qty <= 0) continue;
            
            $servers = json_decode($item->servers, true);
            if (!in_array($server, $servers ?: [])) continue;
            
            $price = $item->offer_price > 0 ? $item->offer_price : $item->price;
            
            $valid_cart[] = [
                'id' => $item->item_id,
                'title' => $item->name,
                'price' => $price,
                'qty' => $qty,
                'is_offer' => $item->is_offer
            ];
            
            $item_total = $price * $qty;
            
            if ($item->is_offer && $exclude_offers === '1') {
                $total_offer += $item_total;
            } else {
                $total_normal += $item_total;
            }
        }
        
        if (empty($valid_cart)) {
            return new WP_REST_Response(['success' => false, 'message' => 'Keine gültigen Items'], 400);
        }
        
        $coupon_discount = 0;
        $coupon_msg = '';
        $coupon_applied = false;
        
        if (!empty($coupon_code)) {
            $coupon = WIS_DB::get_coupon_by_code($coupon_code);
            
            if ($coupon) {
                if ($coupon->expiry && date('Y-m-d') > $coupon->expiry) {
                    $coupon_msg = 'Gutschein abgelaufen';
                } elseif ($coupon->used_count >= $coupon->usage_limit) {
                    $coupon_msg = 'Gutschein bereits aufgebraucht';
                } else {
                    if ($exclude_offers === '1' && $total_normal <= 0) {
                        $coupon_msg = 'Gutschein gilt nicht für Angebote';
                    } else {
                        if ($coupon->type === 'percent') {
                            $coupon_discount = floor($total_normal * ($coupon->value / 100));
                        } else {
                            $coupon_discount = $coupon->value;
                        }
                        
                        WIS_DB::update_coupon($coupon->id, ['used_count' => $coupon->used_count + 1]);
                        $coupon_applied = true;
                        $coupon_msg = "Gutschein eingelöst: -{$coupon_discount} {$currency}";
                    }
                }
            } else {
                $coupon_msg = 'Ungültiger Code';
            }
        }
        
        $final_price = max(0, $total_normal - $coupon_discount) + $total_offer;

        // Fly-Dauern (item_id → Sekunden)
        // Fly-Dauern und lesbares Label (wird dem Spieler auf dem Code angezeigt)
        $fly_durations = [
            'fly_5min'  =>  5 * 60,
            'fly_15min' => 15 * 60,
            'fly_30min' => 30 * 60,
            'fly_1h'    =>  1 * 3600,
            'fly_2h'    =>  2 * 3600,
            'fly_3h'    =>  3 * 3600,
        ];
        $fly_labels = [
            'fly_5min'  => '5 Minuten Fly',
            'fly_15min' => '15 Minuten Fly',
            'fly_30min' => '30 Minuten Fly',
            'fly_1h'    => '1 Stunde Fly',
            'fly_2h'    => '2 Stunden Fly',
            'fly_3h'    => '3 Stunden Fly',
        ];

        $items_payload    = [];
        $commands_payload = [];
        $title_parts      = [];

        foreach ($valid_cart as $item) {
            $item_id = $item['id']; // Das ist der item_id-String aus wis_items

            if (isset($fly_durations[$item_id])) {
                // Fly-Gutschein: pro Stueck einen eigenen Code-Eintrag (qty > 1 = mehrere Codes)
                $base_sec   = $fly_durations[$item_id];
                $base_label = $fly_labels[$item_id];
                for ($q = 0; $q < intval($item['qty']); $q++) {
                    $commands_payload[] = [
                        'type'         => 'fly',
                        'duration_sec' => $base_sec,
                        'label'        => $base_label,
                    ];
                }
                $title_parts[] = $item['qty'] . 'x ' . $item['title'];
            } elseif (preg_match('/^rank_([^_]+)_([a-zA-Z0-9_\-]+)_([a-zA-Z0-9_\-]+)_(\d+)$/', $item_id, $rm)) {
                // Neues Format: rank_{rank_id}_{lp_group}_{default_group}_{days}
                $rank_id       = $rm[1];
                $lp_group      = $rm[2];
                $default_group = $rm[3];
                $rank_days     = intval($rm[4]);
                for ($q = 0; $q < intval($item['qty']); $q++) {
                    $commands_payload[] = [
                        'type'          => 'rank',
                        'rank_id'       => $rank_id,
                        'lp_group'      => $lp_group,
                        'default_group' => $default_group,
                        'label'         => $item['title'],
                        'days'          => $rank_days,
                    ];
                }
                $title_parts[] = $item['qty'] . 'x ' . $item['title'];
            } elseif (preg_match('/^rank_(.+)_(\d+)$/', $item_id, $rm)) {
                // Altes Format (Fallback): rank_{rank_id}_{days}
                $rank_id   = $rm[1];
                $rank_days = intval($rm[2]);
                for ($q = 0; $q < intval($item['qty']); $q++) {
                    $commands_payload[] = [
                        'type'    => 'rank',
                        'rank_id' => $rank_id,
                        'lp_group'=> $rank_id,
                        'default_group' => 'default',
                        'label'   => $item['title'],
                        'days'    => $rank_days,
                    ];
                }
                $title_parts[] = $item['qty'] . 'x ' . $item['title'];
            } elseif (preg_match('/^fly_abo$/', $item_id) || preg_match('/^fly_abo_\d*$/', $item_id)) {
                // Fly-Abo: monatlich abonniert, Preis = Monatsbeitrag
                $commands_payload[] = [
                    'type'  => 'fly_abo',
                    'label' => $item['title'],
                ];
                $title_parts[] = $item['title'];
            } elseif (preg_match('/^plot_slots_(\d+)$/', $item_id, $ps_m)) {
                // Plot-Slots: einmaliger permanenter Kauf
                $extra_slots = intval($ps_m[1]) * intval($item['qty']);
                $commands_payload[] = [
                    'type'  => 'plot_slots',
                    'slots' => $extra_slots,
                    'label' => $item['title'],
                ];
                $title_parts[] = $item['qty'] . 'x ' . $item['title'];
            } elseif (preg_match('/^plot_abo_(\d+)$/', $item_id, $pa_m)) {
                // Plot-Abo: monatliche Abbuchung, Preis = Monatsbeitrag
                $abo_slots = intval($pa_m[1]);
                $commands_payload[] = [
                    'type'  => 'plot_abo',
                    'slots' => $abo_slots,
                    'label' => $item['title'],
                ];
                $title_parts[] = $item['title'];
            } else {
                // Normales Item
                $items_payload[] = [
                    'id'     => $item_id,
                    'amount' => $item['qty'],
                ];
                $title_parts[] = $item['qty'] . 'x ' . $item['title'];
            }
        }

        $title = "Warenkorb: " . implode(', ', $title_parts);
        if (strlen($title) > 240) $title = substr($title, 0, 237) . '...';

        $payload = [
            'items'    => $items_payload,
            'commands' => $commands_payload,
            'coupon'   => $coupon_applied ? ['code' => $coupon_code, 'discount' => $coupon_discount] : [],
        ];
        
        WIS_DB::insert_order([
            'player_name' => $player,
            'server' => $server,
            'item_id' => 'cart',
            'item_title' => $title,
            'price' => $final_price,
            'quantity' => count($valid_cart),
            'status' => 'pending',
            'response' => json_encode($payload)
        ]);

        // Fly-Abo Abonnements registrieren / verlängern
        foreach ($commands_payload as $cmd) {
            if (($cmd['type'] ?? '') !== 'fly_abo') continue;
            $abo_days  = intval($cmd['days']  ?? 30);
            $abo_label = sanitize_text_field($cmd['label'] ?? 'Fly-Abo');
            $abo_price = 0;
            // Preis aus dem Warenkorb ermitteln (erstes passendes fly_abo Item)
            foreach ($valid_cart as $ci) {
                if (preg_match('/^fly_abo_\d+$/', $ci['id'])) {
                    $abo_price = intval($ci['price'] ?? 0);
                    break;
                }
            }
            global $wpdb;
            WIS_Activator::create_abo_subs_table();
            $subs_table = $wpdb->prefix . 'wis_fly_abo_subs';
            $existing = $wpdb->get_row($wpdb->prepare(
                "SELECT * FROM {$subs_table} WHERE player_name = %s AND server = %s",
                $player, $server
            ));
            if ($existing && $existing->status === 'active') {
                // Bestehendes aktives Abo verlängern (kumulativ)
                $wpdb->query($wpdb->prepare(
                    "UPDATE {$subs_table}
                     SET expires_at    = DATE_ADD(IF(expires_at > NOW(), expires_at, NOW()), INTERVAL %d DAY),
                         cancelled     = 0,
                         cancelled_at  = NULL,
                         label         = %s,
                         price         = %d
                     WHERE id = %d",
                    $abo_days, $abo_label, $abo_price, $existing->id
                ));
            } else {
                // Neues Abo anlegen
                $wpdb->replace($subs_table, [
                    'player_name' => $player,
                    'server'      => $server,
                    'label'       => $abo_label,
                    'price'       => $abo_price,
                    'status'      => 'active',
                    'cancelled'   => 0,
                    'expires_at'  => date('Y-m-d H:i:s', strtotime("+{$abo_days} days")),
                ]);
            }
        }

        $msg = '✅ Bestellung erfolgreich!';
        if ($coupon_msg) $msg .= ' (' . $coupon_msg . ')';
        
        return new WP_REST_Response(['success' => true, 'message' => $msg]);
    }
    
    public static function validate_coupon($request) {
        $data = $request->get_json_params();
        $code = sanitize_text_field(strtoupper($data['code'] ?? ''));
        $cart = $data['cart'] ?? [];
        
        if (!$code) {
            return new WP_REST_Response(['success' => false, 'message' => 'Kein Code']);
        }
        
        $coupon = WIS_DB::get_coupon_by_code($code);
        
        if (!$coupon) {
            return new WP_REST_Response(['success' => false, 'message' => 'Gutschein nicht gefunden']);
        }
        
        if ($coupon->expiry && date('Y-m-d') > $coupon->expiry) {
            return new WP_REST_Response(['success' => false, 'message' => 'Gutschein abgelaufen']);
        }
        
        if ($coupon->used_count >= $coupon->usage_limit) {
            return new WP_REST_Response(['success' => false, 'message' => 'Bereits aufgebraucht']);
        }
        
        $exclude_offers = get_option('wis_coupon_exclude_offers', '0');
        
        if ($exclude_offers === '1' && !empty($cart)) {
            $has_normal = false;
            foreach ($cart as $item_data) {
                $item = WIS_DB::get_item(intval($item_data['id'] ?? 0));
                if ($item && !$item->is_offer) {
                    $has_normal = true;
                    break;
                }
            }
            
            if (!$has_normal) {
                return new WP_REST_Response(['success' => false, 'message' => 'Gutschein gilt nicht für Angebote']);
            }
        }
        
        $currency = get_option('wis_currency_name', 'Coins');
        $msg = $coupon->type === 'percent' 
            ? "Gutschein gültig (-{$coupon->value}%)" 
            : "Gutschein gültig (-{$coupon->value} {$currency})";
        
        return new WP_REST_Response([
            'success' => true,
            'type' => $coupon->type,
            'value' => $coupon->value,
            'message' => $msg
        ]);
    }
}

// ===========================================================
// SHORTCODE - FRONTEND SHOP
// ===========================================================
class WIS_Shortcode {
    public static function register() {
        add_shortcode('ingame_shop_form', [self::class, 'render']);
    }
    
    public static function render() {
        $servers        = WIS_DB::get_servers();
        $categories     = WIS_DB::get_categories();
        $currency       = get_option('wis_currency_name', 'Coins');
        $header_text    = get_option('wis_header_text', '');
        $exclude_offers = get_option('wis_coupon_exclude_offers', '0');
        $first_category = !empty($categories) ? $categories[0]->slug : '';

        ob_start();
        ?>
        <style>
        .wis-shop { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; max-width: 1400px; margin: 40px auto; background: #f4f6f8; padding: 20px; border-radius: 10px; position: relative; }
        .wis-header { text-align: center; margin-bottom: 30px; }
        .wis-header h2 { color: #333; margin-bottom: 10px; }
        .wis-status { background: #d4edda; color: #155724; padding: 15px; border-radius: 8px; border-left: 4px solid #c3e6cb; margin-bottom: 20px; font-weight: 500; }
        .wis-control-bar { display: flex; flex-wrap: wrap; gap: 15px; margin-bottom: 30px; align-items: center; justify-content: space-between; background: #fff; padding: 15px; border-radius: 8px; border: 1px solid #ddd; }
        .wis-search-input { padding: 10px 15px; border: 1px solid #ddd; border-radius: 6px; font-size: 16px; width: 100%; max-width: 300px; }
        .wis-filter-select { padding: 12px 20px; font-size: 16px; border: 2px solid #ddd; border-radius: 8px; background: white; cursor: pointer; min-width: 200px; }
        .wis-cart-btn { padding: 12px 30px; background: linear-gradient(135deg, #28a745 0%, #20c997 100%); color: white; border: none; border-radius: 8px; font-weight: bold; font-size: 1rem; cursor: pointer; transition: all 0.3s; position: relative; display: flex; align-items: center; gap: 10px; }
        .wis-cart-btn:hover { transform: scale(1.05); box-shadow: 0 5px 15px rgba(40, 167, 69, 0.4); }
        .wis-cart-badge { position: absolute; top: -8px; right: -8px; background: #dc3545; color: white; border-radius: 50%; width: 24px; height: 24px; display: flex; align-items: center; justify-content: center; font-size: 0.8rem; font-weight: bold; }
        .wis-cat-tabs { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 20px; justify-content: center; }
        .wis-cat-btn { padding: 8px 16px; border: 1px solid #ddd; background: #fff; border-radius: 20px; cursor: pointer; transition: all 0.2s; }
        .wis-cat-btn:hover, .wis-cat-btn.active { background: #667eea; color: white; border-color: #667eea; }
        .wis-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 320px)); gap: 15px; justify-content: center; margin: 0 auto; min-height: 200px; }
        .wis-card { background: white; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 6px rgba(0,0,0,0.05); transition: transform 0.2s, box-shadow 0.2s; display: flex; flex-direction: column; border: 1px solid #eee; position: relative; width: 100%; }
        .wis-card.offer { border: 2px solid #ffc107; }
        .wis-card:hover { transform: translateY(-5px); box-shadow: 0 10px 20px rgba(0,0,0,0.1); }
        .wis-card-img { width: 100%; height: 180px; background: #2d2d2d; display: flex; align-items: center; justify-content: center; position: relative; padding: 20px; }
        .wis-card-img img { width: 160px !important; height: 160px !important; object-fit: contain; filter: drop-shadow(0 6px 8px rgba(0,0,0,0.7)); transition: transform 0.3s; image-rendering: pixelated; image-rendering: -moz-crisp-edges; image-rendering: crisp-edges; }
        .wis-card:hover .wis-card-img img { transform: scale(1.15) rotate(5deg); }
        .wis-card-img--custom { padding: 0 !important; overflow: hidden; }
        .wis-card-img--custom img { width: 100% !important; height: 100% !important; object-fit: cover !important; image-rendering: auto !important; filter: none !important; border-radius: 0; }
        .wis-card:hover .wis-card-img--custom img { transform: scale(1.05) !important; }
        .wis-offer-badge, .wis-daily-badge { position: absolute; top: 10px; left: 10px; background: linear-gradient(135deg, #ff416c, #ff4b2b); color: white; padding: 6px 12px; border-radius: 20px; font-size: 0.75rem; font-weight: bold; box-shadow: 0 2px 5px rgba(0,0,0,0.3); z-index: 2; }
        .wis-daily-badge { background: linear-gradient(135deg, #6f42c1, #8e44ad); border: 1px solid #fff; }
        .wis-card-body { padding: 15px; flex-grow: 1; display: flex; flex-direction: column; }
        .wis-card-title { font-size: 1.1rem; font-weight: 700; margin: 0 0 10px 0; color: #333; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
        .wis-card-desc { font-size: 0.85rem; color: #666; margin-bottom: 10px; line-height: 1.4; height: 40px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
        .wis-card-price-container { display: flex; align-items: baseline; gap: 10px; margin-bottom: 10px; }
        .wis-card-price { font-size: 1.2rem; color: #e67e22; font-weight: 800; }
        .wis-card-price-old { font-size: 0.9rem; color: #999; text-decoration: line-through; }
        .wis-card-servers { font-size: 0.85rem; color: #666; margin-bottom: 15px; }
        .wis-quantity-control { display: flex; align-items: center; gap: 10px; margin-bottom: 15px; }
        .wis-quantity-btn { width: 35px; height: 35px; border: 2px solid #667eea; background: white; color: #667eea; border-radius: 6px; font-weight: bold; cursor: pointer; transition: all 0.2s; }
        .wis-quantity-btn:hover { background: #667eea; color: white; }
        .wis-quantity-input { width: 60px; text-align: center; border: 2px solid #ddd; border-radius: 6px; padding: 8px; font-weight: bold; }
        .wis-btn-add { margin-top: auto; width: 100%; padding: 12px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border: none; border-radius: 6px; font-weight: bold; cursor: pointer; transition: all 0.3s; }
        .wis-btn-add:hover { transform: scale(1.05); box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4); }
        .wis-pagination { display: flex; justify-content: center; align-items: center; gap: 8px; margin: 30px 0 10px; flex-wrap: wrap; }
        .wis-page-btn { padding: 8px 14px; border: 2px solid #ddd; background: #fff; border-radius: 6px; cursor: pointer; font-size: 14px; font-weight: 600; transition: all 0.2s; color: #333; }
        .wis-page-btn:hover { border-color: #667eea; color: #667eea; }
        .wis-page-btn.active { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-color: #667eea; color: #fff; }
        .wis-page-btn:disabled { opacity: 0.4; cursor: not-allowed; }
        .wis-page-info { font-size: 14px; color: #666; margin: 0 8px; }
        .wis-loading { text-align: center; padding: 60px 20px; color: #888; font-size: 1.1rem; }
        .wis-loading-spinner { display: inline-block; width: 36px; height: 36px; border: 4px solid #ddd; border-top-color: #667eea; border-radius: 50%; animation: wis-spin 0.8s linear infinite; margin-bottom: 12px; }
        @keyframes wis-spin { to { transform: rotate(360deg); } }
        .wis-modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.6); z-index: 2147483647; display: none; align-items: center; justify-content: center; transform: translateZ(0); }
        .wis-modal { background: white; width: 90%; max-width: 600px; max-height: 80vh; padding: 30px; border-radius: 15px; box-shadow: 0 15px 30px rgba(0,0,0,0.3); overflow-y: auto; position: relative; z-index: 2147483648; }
        .wis-cart-item { display: flex; justify-content: space-between; align-items: center; padding: 15px; border-bottom: 1px solid #eee; }
        .wis-cart-item-title { font-weight: bold; color: #333; }
        .wis-cart-item-price { color: #e67e22; font-weight: 600; }
        .wis-cart-item-remove { background: #dc3545; color: white; border: none; padding: 8px 15px; border-radius: 6px; cursor: pointer; }
        .wis-cart-total { border-top: 2px solid #333; padding: 20px 0; margin-top: 15px; font-size: 1.3rem; font-weight: bold; display: flex; justify-content: space-between; }
        .wis-modal-input { width: 100%; padding: 15px; margin: 20px 0; border: 2px solid #ddd; border-radius: 8px; font-size: 1.1rem; box-sizing: border-box; }
        .wis-modal-actions { display: flex; gap: 10px; }
        .wis-modal-btn { flex: 1; padding: 12px; border: none; border-radius: 8px; font-weight: bold; cursor: pointer; font-size: 1rem; }
        .wis-btn-confirm { background: linear-gradient(135deg, #28a745 0%, #20c997 100%); color: white; }
        .wis-btn-cancel { background: #6c757d; color: white; }
        .wis-coupon-input-group { display: flex; gap: 10px; margin-bottom: 20px; }
        .wis-coupon-input { flex-grow: 1; padding: 10px; border: 1px solid #ddd; border-radius: 6px; }
        .wis-coupon-btn { padding: 10px 15px; background: #6f42c1; color: white; border: none; border-radius: 6px; font-weight: bold; cursor: pointer; }
        .wis-coupon-msg { font-size: 0.85rem; color: #e67e22; margin-top: 5px; min-height: 20px; }
        .wis-alert { margin-top: 15px; padding: 12px; border-radius: 5px; display: none; }
        .wis-alert.success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .wis-alert.error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
        </style>
        
        <div class="wis-shop">
            <div class="wis-header">
                <h2>🛒 Ingame Shop</h2>
                <?php if (!empty(trim($header_text))): ?>
                    <div class="wis-status"><?php echo wp_kses_post($header_text); ?></div>
                <?php endif; ?>
            </div>
            
            <div class="wis-control-bar">
                <input type="text" id="wis-search" class="wis-search-input" placeholder="🔍 Suche Item...">
                
                <select id="server-filter" class="wis-filter-select">
                    <option value="">Alle Server</option>
                    <?php foreach ($servers as $s): ?>
                        <option value="<?php echo esc_attr($s->slug); ?>"><?php echo esc_html($s->name); ?></option>
                    <?php endforeach; ?>
                </select>
                
                <button class="wis-cart-btn" id="wis-open-cart-btn">
                    🛒 Warenkorb
                    <span class="wis-cart-badge" id="cart-count">0</span>
                </button>
            </div>
            
            <?php if (!empty($categories)): ?>
            <div class="wis-cat-tabs">
                <?php $is_first = true; ?>
                <?php foreach ($categories as $cat): ?>
                    <button class="wis-cat-btn <?php echo $is_first ? 'active' : ''; ?>"
                            data-cat="<?php echo esc_attr($cat->slug); ?>">
                        <?php echo esc_html($cat->name); ?>
                    </button>
                    <?php $is_first = false; ?>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
            
            <div class="wis-grid" id="wis-grid">
                <div class="wis-loading" style="grid-column:1/-1;">
                    <div class="wis-loading-spinner"></div><br>Lade Items…
                </div>
            </div>

            <div class="wis-pagination" id="wis-pagination"></div>
        </div>
        
        <!-- Warenkorb Modal -->
        <div class="wis-modal-overlay" id="cart-modal">
            <div class="wis-modal">
                <h2>🛒 Dein Warenkorb</h2>
                <div id="cart-content">
                    <div style="text-align:center; padding:40px; color:#999;">Dein Warenkorb ist leer</div>
                </div>
                <div id="cart-checkout" style="display:none;">
                    <div style="margin-bottom:20px; background:#f8f9fa; padding:15px; border-radius:8px;">
                        <label style="display:block; font-weight:bold; margin-bottom:5px;">🎫 Gutscheincode</label>
                        <div class="wis-coupon-input-group">
                            <input type="text" id="coupon-code" class="wis-coupon-input" placeholder="CODE">
                            <button class="wis-coupon-btn" id="wis-coupon-btn">Einlösen</button>
                        </div>
                        <div id="coupon-msg" class="wis-coupon-msg"></div>
                    </div>
                    
                    <div class="wis-cart-total">
                        <span>Gesamt:</span>
                        <span id="cart-total">0 <?php echo esc_html($currency); ?></span>
                    </div>
                    
                    <select id="checkout-server" class="wis-modal-input">
                        <option value="">-- Server wählen --</option>
                        <?php foreach ($servers as $s): ?>
                            <option value="<?php echo esc_attr($s->slug); ?>"><?php echo esc_html($s->name); ?></option>
                        <?php endforeach; ?>
                    </select>
                    
                    <input type="text" id="checkout-player" class="wis-modal-input" placeholder="Dein Spielername">
                    
                    <div class="wis-modal-actions">
                        <button class="wis-modal-btn wis-btn-confirm" id="wis-checkout-btn">💰 Kauf abschließen</button>
                        <button class="wis-modal-btn wis-btn-cancel" id="wis-close-cart-btn">Abbrechen</button>
                    </div>
                    
                    <div id="cart-alert" class="wis-alert"></div>
                </div>
            </div>
        </div>
        
        <script>
        (function() {
            const shopCurrency      = <?php echo json_encode($currency); ?>;
            const shopExcludeOffers = <?php echo $exclude_offers === '1' ? 'true' : 'false'; ?>;
            const apiBase           = <?php echo json_encode(rest_url('wis/v1')); ?>;
            const serverList        = <?php echo json_encode(array_map(function($s){ return ['slug'=>$s->slug,'name'=>$s->name]; }, $servers)); ?>;

            let cart          = [];
            let couponData    = {};
            let currentCat    = <?php echo json_encode($first_category); ?>;
            let currentSearch = '';
            let currentPage   = 1;
            let totalPages    = 1;
            let searchTimer   = null;
            let allItems      = [];
            let itemMap       = {}; // id (int) -> item object

            // -------------------------------------------------------
            // INIT
            // -------------------------------------------------------
            function init() {
                // Modal an <body> hängen damit z-index korrekt greift
                const modal = document.getElementById('cart-modal');
                if (modal && document.body) document.body.appendChild(modal);

                // Erste Items laden
                loadItems(1);

                // ---- Event Delegation: Warenkorb-Buttons im Grid ----
                document.getElementById('wis-grid').addEventListener('click', function(e) {

                    // "In den Warenkorb" Button
                    const addBtn = e.target.closest('.wis-btn-add');
                    if (addBtn) {
                        const card = addBtn.closest('.wis-card');
                        const id   = parseInt(addBtn.getAttribute('data-item-id'), 10);
                        const item = itemMap[id];
                        if (!item || !card) return;
                        const qtyInput = card.querySelector('.wis-quantity-input');
                        const qty = parseInt(qtyInput ? qtyInput.value : '1', 10) || 1;
                        addToCartItem(item, qty, addBtn, card);
                        return;
                    }

                    // Mengen-Buttons
                    const qtyBtn = e.target.closest('.wis-quantity-btn');
                    if (qtyBtn) {
                        const delta = parseInt(qtyBtn.getAttribute('data-delta'), 10);
                        const ctrl  = qtyBtn.closest('.wis-quantity-control');
                        if (!ctrl) return;
                        const input = ctrl.querySelector('.wis-quantity-input');
                        if (input) input.value = Math.max(1, Math.min(999, (parseInt(input.value, 10) || 1) + delta));
                    }
                });

                // ---- Kategorie-Tabs ----
                document.querySelectorAll('.wis-cat-btn').forEach(function(btn) {
                    btn.addEventListener('click', function() {
                        document.querySelectorAll('.wis-cat-btn').forEach(function(b){ b.classList.remove('active'); });
                        btn.classList.add('active');
                        currentCat    = btn.getAttribute('data-cat');
                        currentPage   = 1;
                        // Suche beim Kategoriewechsel leeren
                        currentSearch = '';
                        var searchInput = document.getElementById('wis-search');
                        if (searchInput) searchInput.value = '';
                        loadItems(1);
                    });
                });

                // ---- Suche ----
                var searchInput = document.getElementById('wis-search');
                if (searchInput) {
                    searchInput.addEventListener('input', function() {
                        clearTimeout(searchTimer);
                        searchTimer = setTimeout(function() {
                            currentSearch = searchInput.value.trim();
                            currentPage   = 1;
                            // Bei aktiver Suche: Kategorie-Filter deaktivieren (kategorienübergreifend suchen)
                            if (currentSearch) {
                                document.querySelectorAll('.wis-cat-btn').forEach(function(b){ b.classList.remove('active'); });
                                currentCat = '';
                            } else {
                                // Suche geleert: erste Kategorie wieder aktivieren
                                var firstBtn = document.querySelector('.wis-cat-btn');
                                if (firstBtn) {
                                    firstBtn.classList.add('active');
                                    currentCat = firstBtn.getAttribute('data-cat');
                                }
                            }
                            loadItems(1);
                        }, 350);
                    });
                }

                // ---- Server-Filter ----
                var serverSelect = document.getElementById('server-filter');
                if (serverSelect) {
                    serverSelect.addEventListener('change', function() {
                        renderGrid(allItems);
                    });
                }

                // ---- Warenkorb öffnen ----
                var openBtn = document.getElementById('wis-open-cart-btn');
                if (openBtn) openBtn.addEventListener('click', openCart);

                // ---- Warenkorb schließen ----
                var closeBtn = document.getElementById('wis-close-cart-btn');
                if (closeBtn) closeBtn.addEventListener('click', closeCart);

                // ---- Modal-Overlay klick ----
                var cartModal = document.getElementById('cart-modal');
                if (cartModal) {
                    cartModal.addEventListener('click', function(e) {
                        if (e.target === cartModal) closeCart();
                    });
                }

                // ---- Gutschein einlösen ----
                var couponBtn = document.getElementById('wis-coupon-btn');
                if (couponBtn) couponBtn.addEventListener('click', validateCoupon);

                // ---- Kauf abschließen ----
                var checkoutBtn = document.getElementById('wis-checkout-btn');
                if (checkoutBtn) checkoutBtn.addEventListener('click', checkout);
            }

            if (document.readyState === 'loading') {
                document.addEventListener('DOMContentLoaded', init);
            } else {
                init();
            }

            // -------------------------------------------------------
            // ITEMS LADEN
            // -------------------------------------------------------
            function loadItems(page) {
                currentPage = page;
                var grid = document.getElementById('wis-grid');
                grid.innerHTML = '<div class="wis-loading" style="grid-column:1/-1;"><div class="wis-loading-spinner"></div><br>Lade Items…</div>';
                document.getElementById('wis-pagination').innerHTML = '';

                var url = apiBase + '/shop_items?page=' + page;
                if (currentCat)    url += '&category=' + encodeURIComponent(currentCat);
                if (currentSearch) url += '&search='   + encodeURIComponent(currentSearch);

                fetch(url)
                    .then(function(r){ return r.json(); })
                    .then(function(data) {
                        allItems   = data.items || [];
                        totalPages = data.total_pages || 1;
                        itemMap    = {};
                        allItems.forEach(function(i){ itemMap[i.id] = i; });
                        renderGrid(allItems);
                        renderPagination(data.page, data.total_pages, data.total);
                    })
                    .catch(function() {
                        grid.innerHTML = '<div class="wis-loading" style="grid-column:1/-1; color:#dc3545;">Fehler beim Laden der Items.</div>';
                    });
            }

            // -------------------------------------------------------
            // GRID RENDERN  — KEIN INLINE-ONCLICK, nur data-item-id
            // -------------------------------------------------------
            function renderGrid(items) {
                var grid       = document.getElementById('wis-grid');
                var serverSlug = document.getElementById('server-filter').value;

                var filtered = serverSlug
                    ? items.filter(function(i){ return i.servers.indexOf(serverSlug) !== -1; })
                    : items;

                if (filtered.length === 0) {
                    grid.innerHTML = '<div style="grid-column:1/-1; text-align:center; padding:40px; background:#fff; border-radius:10px; color:#888;">Keine Items gefunden.</div>';
                    return;
                }

                grid.innerHTML = filtered.map(function(item) {
                    var price    = item.offer_price > 0 ? item.offer_price : item.price;
                    var showOld  = item.offer_price > 0 && item.offer_price !== item.price;
                    var serverNames = item.servers
                        .map(function(slug) {
                            var s = serverList.find(function(x){ return x.slug === slug; });
                            return s ? s.name : slug;
                        })
                        .join(', ') || 'Kein Server';

                    var badge = item.is_daily_deal
                        ? '<div class="wis-daily-badge">🎁 Angebot des Tages</div>'
                        : (item.is_offer ? '<div class="wis-offer-badge">🔥 Angebot</div>' : '');

                    var oldPrice = showOld
                        ? '<div class="wis-card-price-old">' + item.price + ' ' + shopCurrency + '</div>'
                        : '';

                    var desc = item.description
                        ? '<div class="wis-card-desc">' + escHtml(item.description) + '</div>'
                        : '';

                    // Wichtig: data-item-id statt onclick mit JSON
                    return '<div class="wis-card' + (item.is_offer ? ' offer' : '') + '">'
                        + badge
                        + '<div class="wis-card-img' + (item.has_custom_image ? ' wis-card-img--custom' : '') + '">'
                        +   '<img src="' + escHtml(item.image) + '" alt="' + escHtml(item.name) + '" loading="lazy"'
                        +        ' onerror="this.onerror=null;this.src=\'https://via.placeholder.com/100/333/fff?text=?\';">'
                        + '</div>'
                        + '<div class="wis-card-body">'
                        +   '<h3 class="wis-card-title" title="' + escHtml(item.name) + '">' + escHtml(item.name) + '</h3>'
                        +   desc
                        +   '<div class="wis-card-price-container">'
                        +     oldPrice
                        +     '<div class="wis-card-price">' + price + ' ' + shopCurrency + '</div>'
                        +   '</div>'
                        +   '<div class="wis-card-servers">📡 ' + escHtml(serverNames) + '</div>'
                        +   '<div class="wis-quantity-control">'
                        +     '<button class="wis-quantity-btn" data-delta="-1" type="button">-</button>'
                        +     '<input type="number" class="wis-quantity-input" value="1" min="1" max="999">'
                        +     '<button class="wis-quantity-btn" data-delta="1" type="button">+</button>'
                        +   '</div>'
                        +   '<button class="wis-btn-add" data-item-id="' + item.id + '" type="button">➕ In den Warenkorb</button>'
                        + '</div>'
                        + '</div>';
                }).join('');
            }

            // -------------------------------------------------------
            // PAGINATION
            // -------------------------------------------------------
            function renderPagination(page, pages, total) {
                var el = document.getElementById('wis-pagination');
                if (pages <= 1) { el.innerHTML = ''; return; }

                var html = '';
                html += '<button class="wis-page-btn" ' + (page <= 1 ? 'disabled' : '') + ' data-page="' + (page - 1) + '">‹ Zurück</button>';

                var start = Math.max(1, page - 2);
                var end   = Math.min(pages, page + 2);
                if (start > 1) {
                    html += '<button class="wis-page-btn" data-page="1">1</button>';
                    if (start > 2) html += '<span style="color:#999">…</span>';
                }
                for (var p = start; p <= end; p++) {
                    html += '<button class="wis-page-btn' + (p === page ? ' active' : '') + '" data-page="' + p + '">' + p + '</button>';
                }
                if (end < pages) {
                    if (end < pages - 1) html += '<span style="color:#999">…</span>';
                    html += '<button class="wis-page-btn" data-page="' + pages + '">' + pages + '</button>';
                }
                html += '<button class="wis-page-btn" ' + (page >= pages ? 'disabled' : '') + ' data-page="' + (page + 1) + '">Weiter ›</button>';
                html += '<span class="wis-page-info">' + total + ' Items</span>';
                el.innerHTML = html;

                // Pagination-Buttons via Event Delegation
                el.querySelectorAll('.wis-page-btn:not([disabled])').forEach(function(btn) {
                    btn.addEventListener('click', function() {
                        var p = parseInt(btn.getAttribute('data-page'), 10);
                        if (p) {
                            loadItems(p);
                            document.querySelector('.wis-shop').scrollIntoView({behavior: 'smooth', block: 'start'});
                        }
                    });
                });
            }

            // -------------------------------------------------------
            // WARENKORB LOGIK
            // -------------------------------------------------------
            function addToCartItem(item, qty, btn, card) {
                var price = item.offer_price > 0 ? item.offer_price : item.price;
                var existing = cart.find(function(i){ return i.id === item.id; });
                if (existing) {
                    existing.quantity += qty;
                } else {
                    cart.push({
                        id: item.id,
                        name: item.name,
                        price: price,
                        quantity: qty,
                        servers: item.servers,
                        is_offer: !!item.is_offer
                    });
                }
                updateCartBadge();
                btn.textContent = '✅ Hinzugefügt';
                btn.style.background = '#28a745';
                setTimeout(function() {
                    btn.textContent = '➕ In den Warenkorb';
                    btn.style.background = '';
                }, 1500);
                var qtyInput = card.querySelector('.wis-quantity-input');
                if (qtyInput) qtyInput.value = 1;
            }

            function updateCartBadge() {
                var total = cart.reduce(function(s, i){ return s + i.quantity; }, 0);
                document.getElementById('cart-count').textContent = total;
            }

            function openCart() {
                renderCart();
                document.getElementById('cart-modal').style.display = 'flex';
            }

            function closeCart() {
                document.getElementById('cart-modal').style.display = 'none';
            }

            function calculateTotal() {
                var normal = 0, offer = 0;
                cart.forEach(function(item) {
                    var t = item.price * item.quantity;
                    if (shopExcludeOffers && item.is_offer) offer += t; else normal += t;
                });
                var discount = 0;
                if (couponData && typeof couponData.value !== 'undefined') {
                    discount = couponData.type === 'percent'
                        ? Math.floor(normal * couponData.value / 100)
                        : couponData.value;
                }
                return Math.max(0, normal - discount) + offer;
            }

            function renderCart() {
                var content  = document.getElementById('cart-content');
                var checkout = document.getElementById('cart-checkout');
                if (cart.length === 0) {
                    content.innerHTML = '<div style="text-align:center;padding:40px;color:#999;">Dein Warenkorb ist leer</div>';
                    checkout.style.display = 'none';
                    return;
                }
                content.innerHTML = cart.map(function(item, idx) {
                    return '<div class="wis-cart-item">'
                        + '<div>'
                        +   '<div class="wis-cart-item-title">' + escHtml(item.name) + '</div>'
                        +   '<div class="wis-cart-item-price">' + item.quantity + 'x × ' + item.price + ' = ' + (item.price * item.quantity) + ' ' + shopCurrency + '</div>'
                        + '</div>'
                        + '<button class="wis-cart-item-remove" data-cart-idx="' + idx + '" type="button">🗑️</button>'
                        + '</div>';
                }).join('');

                // Remove-Buttons
                content.querySelectorAll('.wis-cart-item-remove').forEach(function(btn) {
                    btn.addEventListener('click', function() {
                        var idx = parseInt(btn.getAttribute('data-cart-idx'), 10);
                        cart.splice(idx, 1);
                        updateCartBadge();
                        renderCart();
                    });
                });

                document.getElementById('cart-total').textContent = calculateTotal() + ' ' + shopCurrency;
                checkout.style.display = 'block';
            }

            function validateCoupon() {
                var code    = document.getElementById('coupon-code').value.trim().toUpperCase();
                var msgEl   = document.getElementById('coupon-msg');
                if (!code) { couponData = {}; msgEl.textContent = ''; renderCart(); return; }
                msgEl.textContent = 'Prüfe…';
                msgEl.style.color = '#0073aa';
                fetch(apiBase + '/validate_coupon', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({code: code, cart: cart})
                })
                .then(function(r){ return r.json(); })
                .then(function(data) {
                    if (data.success) {
                        couponData = {type: data.type, value: data.value};
                        msgEl.textContent = '✅ ' + data.message;
                        msgEl.style.color = 'green';
                    } else {
                        couponData = {};
                        msgEl.textContent = '❌ ' + data.message;
                        msgEl.style.color = 'red';
                    }
                    renderCart();
                });
            }

            function checkout() {
                var player     = document.getElementById('checkout-player').value.trim();
                var server     = document.getElementById('checkout-server').value;
                var couponCode = document.getElementById('coupon-code').value.trim();
                var alertEl    = document.getElementById('cart-alert');
                var btn        = document.getElementById('wis-checkout-btn');

                if (!player) { alertEl.textContent = 'Bitte Spielername eingeben!'; alertEl.className = 'wis-alert error'; alertEl.style.display = 'block'; return; }
                if (!server) { alertEl.textContent = 'Bitte Server auswählen!';      alertEl.className = 'wis-alert error'; alertEl.style.display = 'block'; return; }

                var invalid = cart.filter(function(i){ return i.servers.indexOf(server) === -1; });
                if (invalid.length) { alertEl.textContent = 'Einige Items sind nicht für diesen Server!'; alertEl.className = 'wis-alert error'; alertEl.style.display = 'block'; return; }

                btn.disabled = true;
                btn.textContent = '⏳ Speichere…';

                fetch(apiBase + '/order', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({player: player, cart: cart, server: server, coupon_code: couponCode})
                })
                .then(function(r){ return r.json(); })
                .then(function(data) {
                    if (data.success) {
                        alertEl.textContent   = data.message;
                        alertEl.className     = 'wis-alert success';
                        alertEl.style.display = 'block';
                        cart = []; couponData = {};
                        updateCartBadge();
                        setTimeout(function(){ closeCart(); location.reload(); }, 2000);
                    } else {
                        alertEl.textContent   = data.message;
                        alertEl.className     = 'wis-alert error';
                        alertEl.style.display = 'block';
                    }
                })
                .finally(function() {
                    btn.disabled = false;
                    btn.textContent = '💰 Kauf abschließen';
                });
            }

            // -------------------------------------------------------
            // HILFSFUNKTIONEN
            // -------------------------------------------------------
            function escHtml(str) {
                return String(str)
                    .replace(/&/g, '&amp;')
                    .replace(/</g, '&lt;')
                    .replace(/>/g, '&gt;')
                    .replace(/"/g, '&quot;')
                    .replace(/'/g, '&#039;');
            }

        })();
        </script>
        <?php
        return ob_get_clean();
    }
}

// ===========================================================
// SIDEBAR WIDGET
// ===========================================================
class WIS_Sidebar_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'wis_sidebar_offer',
            'WIS Shop Angebot',
            ['description' => 'Zeigt Daily Deal oder Angebot an']
        );
    }
    
    public function widget($args, $instance) {
        echo $args['before_widget'];
        
        if (!empty($instance['title'])) {
            echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
        }
        
        global $wpdb;
        $currency = get_option('wis_currency_name', 'Coins');
        $img_base = get_option('wis_image_base_url', '');
        
        $item = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}wis_items WHERE is_daily_deal = 1 AND status = 'publish' LIMIT 1");
        
        if (!$item) {
            $item = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}wis_items WHERE is_offer = 1 AND status = 'publish' ORDER BY updated_at DESC LIMIT 1");
        }
        
        if ($item) {
            $price = $item->offer_price > 0 ? $item->offer_price : $item->price;
            $show_old = $item->offer_price > 0 && $item->offer_price != $item->price;
            $img_url = WIS_DB::get_item_image($item);
            $target_url = !empty($instance['shop_url']) ? $instance['shop_url'] : home_url();
            
            ?>
            <div style="background:#fff; border-radius:10px; padding:0; overflow:hidden; box-shadow:0 4px 10px rgba(0,0,0,0.05); text-align:center;">
                <div style="position:relative; background:#2d2d2d; padding:15px; height:160px; display:flex; align-items:center; justify-content:center;">
                    <?php if ($item->is_daily_deal): ?>
                        <div style="position:absolute; top:10px; left:10px; background:linear-gradient(135deg, #6f42c1, #8e44ad); color:#fff; padding:4px 10px; font-size:10px; border-radius:20px; font-weight:bold; z-index:2;">🎁 DAILY DEAL</div>
                    <?php elseif ($item->is_offer): ?>
                        <div style="position:absolute; top:10px; left:10px; background:linear-gradient(135deg, #ff416c, #ff4b2b); color:#fff; padding:4px 10px; font-size:10px; border-radius:20px; font-weight:bold; z-index:2;">🔥 ANGEBOT</div>
                    <?php endif; ?>
                    <img src="<?php echo esc_url($img_url); ?>" alt="<?php echo esc_attr($item->name); ?>" style="max-width:90%; max-height:90%; object-fit:contain; filter:drop-shadow(0 4px 8px rgba(0,0,0,0.6));">
                </div>
                <div style="padding:15px;">
                    <h4 style="margin:0 0 8px 0; font-size:15px; color:#333; font-weight:700;"><?php echo esc_html($item->name); ?></h4>
                    <div style="margin-bottom:12px;">
                        <?php if ($show_old): ?>
                            <span style="text-decoration:line-through; color:#999; font-size:11px;"><?php echo esc_html($item->price); ?> <?php echo esc_html($currency); ?></span>
                        <?php endif; ?>
                        <span style="font-size:20px; font-weight:800; color:#28a745;"><?php echo esc_html($price); ?> <span style="font-size:12px; font-weight:400; color:#666;"><?php echo esc_html($currency); ?></span></span>
                    </div>
                    <a href="<?php echo esc_url($target_url); ?>" style="display:block; padding:10px 0; background:linear-gradient(135deg, #667eea 0%, #764ba2 100%); color:#fff; text-decoration:none; border-radius:6px; font-weight:bold; font-size:13px;">
                        <?php echo esc_html($instance['btn_text'] ?? 'Zum Shop'); ?> 🛒
                    </a>
                </div>
            </div>
            <?php
        } else {
            echo '<div style="background:#f8f9fa; border:1px dashed #ddd; border-radius:8px; padding:20px; text-align:center;"><p style="margin:0; color:#888; font-size:13px;">Kein Angebot verfügbar.</p></div>';
        }
        
        echo $args['after_widget'];
    }
    
    public function form($instance) {
        $title    = !empty($instance['title'])    ? $instance['title']    : '🔥 Angebot des Tages';
        $btn_text = !empty($instance['btn_text']) ? $instance['btn_text'] : 'Zum Shop';
        $shop_url = !empty($instance['shop_url']) ? $instance['shop_url'] : '';
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">Titel:</label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('btn_text'); ?>">Button Text:</label>
            <input class="widefat" id="<?php echo $this->get_field_id('btn_text'); ?>" name="<?php echo $this->get_field_name('btn_text'); ?>" type="text" value="<?php echo esc_attr($btn_text); ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('shop_url'); ?>">Shop URL:</label>
            <input class="widefat" id="<?php echo $this->get_field_id('shop_url'); ?>" name="<?php echo $this->get_field_name('shop_url'); ?>" type="text" value="<?php echo esc_attr($shop_url); ?>">
        </p>
        <?php
    }
    
    public function update($new_instance, $old_instance) {
        $instance             = [];
        $instance['title']    = !empty($new_instance['title'])    ? sanitize_text_field($new_instance['title'])    : '';
        $instance['btn_text'] = !empty($new_instance['btn_text']) ? sanitize_text_field($new_instance['btn_text']) : 'Zum Shop';
        $instance['shop_url'] = !empty($new_instance['shop_url']) ? esc_url_raw($new_instance['shop_url'])         : '';
        return $instance;
    }
}

// ===========================================================
// INITIALIZATION
// ===========================================================
register_activation_hook(__FILE__, [WIS_Activator::class, 'activate']);
register_deactivation_hook(__FILE__, [WIS_Activator::class, 'deactivate']);

add_action('admin_menu',   [WIS_Admin::class,     'register_menu']);
add_action('admin_init',   [WIS_Admin::class,     'handle_save_item']);
add_action('rest_api_init',[WIS_API::class,        'register_routes']);
add_action('init',         [WIS_Shortcode::class,  'register']);
add_action('widgets_init', function() {
    register_widget('WIS_Sidebar_Widget');
});
add_action('wis_daily_deal_event', [WIS_Activator::class, 'run_daily_deal']);
add_action('wis_abo_renewal_event', [WIS_Activator::class, 'run_abo_renewal']);