<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registrierung - Veranstaltungen</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .container {
            width: 100%;
            max-width: 400px;
        }
        .card {
            background: white;
            border-radius: 10px;
            padding: 40px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 28px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            color: #555;
            font-weight: 600;
            margin-bottom: 8px;
        }
        input, select {
            width: 100%;
            padding: 12px;
            border: 2px solid #e0e0e0;
            border-radius: 5px;
            font-size: 14px;
            transition: border-color 0.3s;
            font-family: inherit;
        }
        input:focus, select:focus {
            outline: none;
            border-color: #667eea;
        }
        .btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s;
            margin-top: 10px;
        }
        .btn:hover {
            transform: translateY(-2px);
        }
        .error {
            color: #dc3545;
            font-size: 13px;
            margin-top: 5px;
        }
        .success {
            color: #28a745;
            padding: 12px;
            background: #d4edda;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .link {
            text-align: center;
            margin-top: 20px;
            color: #666;
        }
        .link a {
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
        }
        .link a:hover {
            text-decoration: underline;
        }
        .role-info {
            background: #f0f4ff;
            padding: 10px;
            border-radius: 5px;
            font-size: 13px;
            color: #555;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <h1>Registrierung</h1>
            <form id="registerForm">
                <div class="form-group">
                    <label for="name">Name</label>
                    <input type="text" id="name" name="name" required>
                    <div class="error" id="error-name"></div>
                </div>

                <div class="form-group">
                    <label for="email">Email</label>
                    <input type="email" id="email" name="email" required>
                    <div class="error" id="error-email"></div>
                </div>

                <div class="form-group">
                    <label for="password">Passwort</label>
                    <input type="password" id="password" name="password" required minlength="8">
                    <div class="error" id="error-password"></div>
                </div>

                <div class="form-group">
                    <label for="password_confirmation">Passwort wiederholen</label>
                    <input type="password" id="password_confirmation" name="password_confirmation" required>
                </div>

                <div class="form-group">
                    <label for="role">Ich möchte als... registrieren</label>
                    <select id="role" name="role" required>
                        <option value="user">Normaler User (Veranstaltungen anschauen)</option>
                        <option value="organizer">Organizer (Veranstaltungen erstellen)</option>
                    </select>
                    <div class="role-info" id="roleInfo">
                        Normale User können Veranstaltungen ansehen und zu Favoriten hinzufügen.
                    </div>
                </div>

                <button type="submit" class="btn">Registrieren</button>
                <div class="error" id="error-general"></div>
                <div class="success" id="success-message" style="display: none;"></div>
            </form>

            <div class="link">
                Hast du bereits ein Konto? <a href="/login">Hier einloggen</a>
            </div>
        </div>
    </div>

    <script>
        const roleSelect = document.getElementById('role');
        const roleInfo = document.getElementById('roleInfo');

        roleSelect.addEventListener('change', (e) => {
            if (e.target.value === 'organizer') {
                roleInfo.textContent = 'Organizer können neue Veranstaltungen erstellen und verwalten.';
            } else {
                roleInfo.textContent = 'Normale User können Veranstaltungen ansehen und zu Favoriten hinzufügen.';
            }
        });

        document.getElementById('registerForm').addEventListener('submit', async (e) => {
            e.preventDefault();

            const formData = {
                name: document.getElementById('name').value,
                email: document.getElementById('email').value,
                password: document.getElementById('password').value,
                password_confirmation: document.getElementById('password_confirmation').value,
                role: document.getElementById('role').value,
            };

            try {
                const response = await fetch('/api/auth/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(formData),
                });

                const data = await response.json();

                if (!response.ok) {
                    // Fehler anzeigen
                    document.getElementById('error-general').textContent = data.message || 'Registrierung fehlgeschlagen';
                    if (data.errors) {
                        Object.keys(data.errors).forEach(key => {
                            const errorEl = document.getElementById(`error-${key}`);
                            if (errorEl) {
                                errorEl.textContent = data.errors[key][0];
                            }
                        });
                    }
                    return;
                }

                // Erfolgreich - Token speichern und weiterleiten
                localStorage.setItem('auth_token', data.token);
                localStorage.setItem('user', JSON.stringify(data.user));
                document.getElementById('success-message').textContent = 'Registrierung erfolgreich! Weitergeleitet...';
                document.getElementById('success-message').style.display = 'block';

                setTimeout(() => {
                    window.location.href = '/';
                }, 1500);
            } catch (error) {
                document.getElementById('error-general').textContent = 'Ein Fehler ist aufgetreten: ' + error.message;
            }
        });
    </script>
</body>
</html>
