<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LiveComponent Shopping Cart Demo</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            padding: 2rem;
            background: #f5f5f5;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
        }

        h1 {
            margin-bottom: 1rem;
            color: #333;
        }

        .intro {
            background: white;
            padding: 1.5rem;
            border-radius: 8px;
            margin-bottom: 2rem;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .intro h2 {
            font-size: 1.25rem;
            margin-bottom: 1rem;
            color: #444;
        }

        .feature-list {
            line-height: 1.8;
            color: #666;
        }

        .feature-list li {
            margin-bottom: 0.5rem;
        }

        .demo-section {
            background: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 2rem;
        }

        .demo-section h3 {
            margin-bottom: 1.5rem;
            color: #333;
            font-size: 1.125rem;
        }

        .demo-controls {
            background: #f9f9f9;
            padding: 1.5rem;
            border-radius: 6px;
            margin-bottom: 2rem;
        }

        .demo-controls h4 {
            margin-bottom: 1rem;
            color: #333;
        }

        .control-row {
            display: flex;
            gap: 1rem;
            margin-bottom: 1rem;
            flex-wrap: wrap;
        }

        .btn-demo {
            padding: 0.75rem 1.5rem;
            background: #2196F3;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-weight: 500;
            transition: background 0.2s;
        }

        .btn-demo:hover {
            background: #1976D2;
        }

        .btn-demo.btn-success {
            background: #4CAF50;
        }

        .btn-demo.btn-success:hover {
            background: #388E3C;
        }

        .btn-demo.btn-danger {
            background: #f44336;
        }

        .btn-demo.btn-danger:hover {
            background: #d32f2f;
        }

        .info-box {
            background: #e3f2fd;
            padding: 1rem;
            border-radius: 4px;
            margin-top: 1rem;
            border-left: 4px solid #2196F3;
        }

        .info-box strong {
            color: #1976D2;
        }

        .product-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 1rem;
            margin-bottom: 2rem;
        }

        .product-card {
            background: white;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 1rem;
            text-align: center;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .product-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        }

        .product-image {
            width: 100%;
            height: 150px;
            object-fit: cover;
            border-radius: 6px;
            margin-bottom: 0.75rem;
        }

        .product-name {
            font-weight: 600;
            margin-bottom: 0.5rem;
            color: #333;
        }

        .product-price {
            font-size: 1.25rem;
            color: #2196F3;
            font-weight: 600;
            margin-bottom: 0.75rem;
        }

        .product-add-btn {
            width: 100%;
            padding: 0.5rem;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-weight: 500;
            transition: background 0.2s;
        }

        .product-add-btn:hover {
            background: #388E3C;
        }
    </style>
</head>
<body data-module="livecomponent">
    <div class="container">
        <h1>🛒 LiveComponent Shopping Cart Demo</h1>

        <div class="intro">
            <h2>E-Commerce Warenkorb mit Live-Updates</h2>
            <ul class="feature-list">
                <li>✅ <strong>Add/Remove Items:</strong> Produkte zum Warenkorb hinzufügen und entfernen</li>
                <li>✅ <strong>Quantity Management:</strong> Mengen anpassen mit Min/Max Validierung</li>
                <li>✅ <strong>Price Calculation:</strong> Automatische Preis-Berechnung mit Zwischensumme</li>
                <li>✅ <strong>Discount Codes:</strong> Rabattcodes anwenden (WELCOME10, SAVE20, SUMMER15, VIP25)</li>
                <li>✅ <strong>Shipping Options:</strong> Standard, Express, Overnight Versand</li>
                <li>✅ <strong>Free Shipping:</strong> Progress Bar für kostenlosen Versand ab 50€</li>
                <li>✅ <strong>Tax Calculation:</strong> Automatische MwSt-Berechnung (19%)</li>
                <li>✅ <strong>Total Savings:</strong> Anzeige der Gesamtersparnis</li>
            </ul>
        </div>

        <!-- Demo Products -->
        <div class="demo-section">
            <h3>Verfügbare Produkte</h3>
            <div class="product-grid">
                <div class="product-card">
                    <img src="https://via.placeholder.com/200x150/4CAF50/fff?text=Laptop" alt="Laptop" class="product-image">
                    <div class="product-name">Premium Laptop</div>
                    <div class="product-price">899,00 €</div>
                    <button class="product-add-btn" onclick="addProduct('laptop', 'Premium Laptop', 899.00, 'https://via.placeholder.com/100x100/4CAF50/fff?text=Laptop')">
                        In den Warenkorb
                    </button>
                </div>

                <div class="product-card">
                    <img src="https://via.placeholder.com/200x150/2196F3/fff?text=Phone" alt="Smartphone" class="product-image">
                    <div class="product-name">Smartphone Pro</div>
                    <div class="product-price">699,00 €</div>
                    <button class="product-add-btn" onclick="addProduct('phone', 'Smartphone Pro', 699.00, 'https://via.placeholder.com/100x100/2196F3/fff?text=Phone')">
                        In den Warenkorb
                    </button>
                </div>

                <div class="product-card">
                    <img src="https://via.placeholder.com/200x150/FF9800/fff?text=Tablet" alt="Tablet" class="product-image">
                    <div class="product-name">Tablet Plus</div>
                    <div class="product-price">449,00 €</div>
                    <button class="product-add-btn" onclick="addProduct('tablet', 'Tablet Plus', 449.00, 'https://via.placeholder.com/100x100/FF9800/fff?text=Tablet')">
                        In den Warenkorb
                    </button>
                </div>

                <div class="product-card">
                    <img src="https://via.placeholder.com/200x150/9C27B0/fff?text=Watch" alt="Smartwatch" class="product-image">
                    <div class="product-name">Smartwatch Elite</div>
                    <div class="product-price">299,00 €</div>
                    <button class="product-add-btn" onclick="addProduct('watch', 'Smartwatch Elite', 299.00, 'https://via.placeholder.com/100x100/9C27B0/fff?text=Watch')">
                        In den Warenkorb
                    </button>
                </div>

                <div class="product-card">
                    <img src="https://via.placeholder.com/200x150/F44336/fff?text=Headphones" alt="Kopfhörer" class="product-image">
                    <div class="product-name">Noise Cancelling Headphones</div>
                    <div class="product-price">249,00 €</div>
                    <button class="product-add-btn" onclick="addProduct('headphones', 'Noise Cancelling Headphones', 249.00, 'https://via.placeholder.com/100x100/F44336/fff?text=Headphones')">
                        In den Warenkorb
                    </button>
                </div>

                <div class="product-card">
                    <img src="https://via.placeholder.com/200x150/00BCD4/fff?text=Speaker" alt="Lautsprecher" class="product-image">
                    <div class="product-name">Bluetooth Speaker</div>
                    <div class="product-price">89,00 €</div>
                    <button class="product-add-btn" onclick="addProduct('speaker', 'Bluetooth Speaker', 89.00, 'https://via.placeholder.com/100x100/00BCD4/fff?text=Speaker')">
                        In den Warenkorb
                    </button>
                </div>
            </div>
        </div>

        <!-- Shopping Cart Component -->
        <div class="demo-section">
            <h3>Warenkorb</h3>
            {{shopping_cart}}
        </div>

        <div class="demo-controls">
            <h4>Schnellaktionen</h4>

            <div class="control-row">
                <button class="btn-demo btn-success" onclick="fillCart()">
                    🛒 Warenkorb füllen (3 Produkte)
                </button>
                <button class="btn-demo" onclick="applyDiscount('WELCOME10')">
                    🎟️ Rabattcode anwenden (10%)
                </button>
                <button class="btn-demo btn-danger" onclick="clearCart()">
                    🗑️ Warenkorb leeren
                </button>
            </div>

            <div class="info-box">
                <strong>Tipp:</strong> Klicke auf die Produkte um sie dem Warenkorb hinzuzufügen.
                Teste die verschiedenen Rabattcodes (WELCOME10, SAVE20, SUMMER15, VIP25) und beobachte
                wie sich die Preise und die Ersparnis ändern. Ab 50€ Warenwert gibt es kostenlosen Versand!
            </div>
        </div>
    </div>

    <script>
        // Helper function to trigger cart actions
        function triggerCartAction(action, params = {}) {
            const component = document.querySelector('[data-component-id^="shopping-cart:"]');
            if (!component) {
                console.error('Shopping cart component not found');
                return;
            }

            const button = document.createElement('button');
            button.setAttribute('data-live-action', action);

            Object.entries(params).forEach(([key, value]) => {
                button.setAttribute(`data-param-${key}`, typeof value === 'object' ? JSON.stringify(value) : value);
            });

            button.style.display = 'none';
            component.appendChild(button);
            button.click();
            component.removeChild(button);
        }

        // Add Product to Cart
        function addProduct(productId, productName, price, imageUrl) {
            triggerCartAction('addItem', {
                'product-id': productId,
                'product-name': productName,
                'price': price,
                'quantity': 1,
                'image-url': imageUrl,
                'attributes': JSON.stringify({})
            });
        }

        // Fill Cart with Demo Products
        function fillCart() {
            addProduct('laptop', 'Premium Laptop', 899.00, 'https://via.placeholder.com/100x100/4CAF50/fff?text=Laptop');

            setTimeout(() => {
                addProduct('phone', 'Smartphone Pro', 699.00, 'https://via.placeholder.com/100x100/2196F3/fff?text=Phone');
            }, 300);

            setTimeout(() => {
                addProduct('headphones', 'Noise Cancelling Headphones', 249.00, 'https://via.placeholder.com/100x100/F44336/fff?text=Headphones');
            }, 600);
        }

        // Apply Discount Code
        function applyDiscount(code) {
            triggerCartAction('applyDiscountCode', {
                'code': code
            });
        }

        // Clear Cart
        function clearCart() {
            if (confirm('Möchten Sie den Warenkorb wirklich leeren?')) {
                triggerCartAction('clearCart', {});
            }
        }

        // Initialize with one product
        window.addEventListener('DOMContentLoaded', function() {
            // Check if cart is empty
            const emptyState = document.querySelector('.cart-empty');

            if (emptyState) {
                // Add first product after a short delay
                setTimeout(() => {
                    addProduct('speaker', 'Bluetooth Speaker', 89.00, 'https://via.placeholder.com/100x100/00BCD4/fff?text=Speaker');
                }, 500);
            }
        });
    </script>
</body>
</html>
