<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LiveComponent File Upload 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: 800px;
            margin: 0 auto;
        }

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

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

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

        .event-log {
            max-height: 300px;
            overflow-y: auto;
            background: #f9f9f9;
            padding: 1rem;
            border-radius: 4px;
            font-family: 'Monaco', 'Courier New', monospace;
            font-size: 0.875rem;
        }

        .event-item {
            padding: 0.5rem;
            margin-bottom: 0.5rem;
            background: white;
            border-left: 3px solid #4CAF50;
            border-radius: 2px;
        }

        .event-name {
            font-weight: bold;
            color: #333;
        }

        .event-payload {
            color: #666;
            margin-top: 0.25rem;
        }

        .event-time {
            color: #999;
            font-size: 0.75rem;
        }

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

        .feature-list li {
            margin-bottom: 0.5rem;
        }
    </style>
</head>
<body data-module="livecomponent">
    <div class="container">
        <h1>📤 LiveComponent File Upload Demo</h1>

        <div class="card">
            <h2>Image Uploader Component</h2>
            {{uploader}}
        </div>

        <div class="card">
            <h2>Upload Event Log</h2>
            <div id="event-log" class="event-log">
                <p style="color: #999;">Waiting for upload events...</p>
            </div>
        </div>

        <div class="card">
            <h2>Features</h2>
            <ul class="feature-list">
                <li>✅ Drag & Drop Support</li>
                <li>✅ File Input with Click</li>
                <li>✅ Image Validation (MIME type, size)</li>
                <li>✅ Upload Progress Indicator</li>
                <li>✅ Image Preview after Upload</li>
                <li>✅ Clear/Delete Functionality</li>
                <li>✅ Upload Statistics</li>
                <li>✅ Event Dispatching (upload:success, upload:error)</li>
            </ul>
        </div>

        <div class="card">
            <h2>Technical Details</h2>
            <ul class="feature-list">
                <li><strong>Max File Size:</strong> 5MB</li>
                <li><strong>Allowed Formats:</strong> JPG, PNG, GIF, WebP</li>
                <li><strong>Upload Endpoint:</strong> /live-component/{id}/upload</li>
                <li><strong>Validation:</strong> Server-side (MIME type, size, image format)</li>
                <li><strong>Storage:</strong> public/uploads/ directory</li>
            </ul>
        </div>
    </div>

    <script>
        // Upload event logging (UI-specific, not LiveComponent core)
        document.addEventListener('livecomponent:upload:success', (e) => {
            logEvent('Upload Success', e.detail);
        });

        document.addEventListener('livecomponent:upload:error', (e) => {
            logEvent('Upload Error', e.detail);
        });

        document.addEventListener('livecomponent:image:cleared', (e) => {
            logEvent('Image Cleared', e.detail);
        });

        function logEvent(name, payload) {
            const log = document.getElementById('event-log');

            // Remove "waiting" message
            const waiting = log.querySelector('p');
            if (waiting) waiting.remove();

            const item = document.createElement('div');
            item.className = 'event-item';

            const time = new Date().toLocaleTimeString();

            item.innerHTML = `
                <div class="event-name">${name}</div>
                <div class="event-payload">${JSON.stringify(payload)}</div>
                <div class="event-time">${time}</div>
            `;

            log.insertBefore(item, log.firstChild);

            // Keep only last 20 events
            while (log.children.length > 20) {
                log.removeChild(log.lastChild);
            }
        }
    </script>
</body>
</html>
