{% extends "base.html" %}

{% block title %}Benutzer hinzufügen - Ausbilder-Bereich - Mercedes-Benz{% endblock %}

{% block extra_css %}
<style>
    /* Spezielle Styles für Admin-Benutzer-Formular */
    .admin-form-container {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
        border: 1px solid rgba(0, 0, 0, 0.05);
        box-shadow: 
            0 20px 25px -5px rgba(0, 0, 0, 0.1),
            0 10px 10px -5px rgba(0, 0, 0, 0.04),
            0 0 0 1px rgba(255, 255, 255, 0.05) inset;
    }
    
    .dark .admin-form-container {
        background: rgba(15, 23, 42, 0.95);
        border: 1px solid rgba(255, 255, 255, 0.1);
        box-shadow: 
            0 20px 25px -5px rgba(0, 0, 0, 0.25),
            0 10px 10px -5px rgba(0, 0, 0, 0.1),
            0 0 0 1px rgba(255, 255, 255, 0.05) inset;
    }
    
    .form-field-premium {
        position: relative;
        overflow: hidden;
    }
    
    .form-field-premium::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 1px;
        background: linear-gradient(90deg, transparent, rgba(59, 130, 246, 0.5), transparent);
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    
    .form-field-premium:focus-within::before {
        opacity: 1;
    }
    
    .input-premium {
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        background: rgba(255, 255, 255, 0.8);
        backdrop-filter: blur(8px);
        border: 1px solid rgba(0, 0, 0, 0.1);
    }
    
    .dark .input-premium {
        background: rgba(15, 23, 42, 0.8);
        border: 1px solid rgba(255, 255, 255, 0.1);
        color: #ffffff;
    }
    
    .input-premium:focus {
        background: rgba(255, 255, 255, 0.95);
        border-color: rgba(59, 130, 246, 0.5);
        box-shadow: 
            0 0 0 3px rgba(59, 130, 246, 0.1),
            0 10px 25px -5px rgba(59, 130, 246, 0.1);
        transform: translateY(-1px);
    }
    
    .dark .input-premium:focus {
        background: rgba(15, 23, 42, 0.95);
        border-color: rgba(59, 130, 246, 0.5);
        box-shadow: 
            0 0 0 3px rgba(59, 130, 246, 0.1),
            0 10px 25px -5px rgba(59, 130, 246, 0.1);
    }
    
    .btn-mercedes-primary {
        background: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
        box-shadow: 
            0 4px 15px rgba(59, 130, 246, 0.3),
            0 0 0 1px rgba(255, 255, 255, 0.1) inset;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    .btn-mercedes-primary:hover {
        background: linear-gradient(135deg, #1d4ed8 0%, #2563eb 100%);
        box-shadow: 
            0 8px 25px rgba(59, 130, 246, 0.4),
            0 0 0 1px rgba(255, 255, 255, 0.2) inset;
        transform: translateY(-2px);
    }
    
    .btn-mercedes-secondary {
        background: linear-gradient(135deg, #6b7280 0%, #9ca3af 100%);
        box-shadow: 
            0 4px 15px rgba(107, 114, 128, 0.3),
            0 0 0 1px rgba(255, 255, 255, 0.1) inset;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    .btn-mercedes-secondary:hover {
        background: linear-gradient(135deg, #4b5563 0%, #6b7280 100%);
        box-shadow: 
            0 8px 25px rgba(107, 114, 128, 0.4),
            0 0 0 1px rgba(255, 255, 255, 0.2) inset;
        transform: translateY(-2px);
    }
    
    .info-box-premium {
        background: linear-gradient(135deg, rgba(59, 130, 246, 0.1) 0%, rgba(139, 92, 246, 0.1) 100%);
        border: 1px solid rgba(59, 130, 246, 0.2);
        backdrop-filter: blur(10px);
    }
    
    .dark .info-box-premium {
        background: linear-gradient(135deg, rgba(59, 130, 246, 0.1) 0%, rgba(139, 92, 246, 0.1) 100%);
        border: 1px solid rgba(59, 130, 246, 0.3);
    }
    
    .validation-error {
        border-color: #ef4444 !important;
        box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1) !important;
    }
    
    .validation-success {
        border-color: #10b981 !important;
        box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.1) !important;
    }
</style>
{% endblock %}

{% block content %}
<div class="max-w-4xl mx-auto space-y-8">
    <!-- Header Section mit Mercedes-Benz Design -->
    <div class="admin-form-container rounded-2xl p-8 transition-all duration-300">
        <div class="flex items-center justify-between mb-6">
            <div class="flex items-center space-x-4">
                <!-- Mercedes Icon -->
                <div class="w-12 h-12 rounded-full bg-gradient-to-br from-blue-500 to-blue-600 flex items-center justify-center shadow-lg">
                    <svg class="w-7 h-7 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197m3 4.197a4 4 0 11-8 0 4 4 0 018 0z"/>
                    </svg>
                </div>
                <div>
                    <h1 class="text-3xl font-bold text-slate-900 dark:text-white transition-colors duration-300">
                        Neuen Benutzer hinzufügen
                    </h1>
                    <p class="text-slate-600 dark:text-slate-400 mt-1 transition-colors duration-300">
                        Erstellen Sie einen neuen Benutzer für das Mercedes-Benz MYP System
                    </p>
                </div>
            </div>
            
            <!-- Zurück Button -->
            <a href="{{ url_for('admin_page', tab='users') }}" 
               class="btn-mercedes-secondary text-white px-6 py-3 rounded-xl font-medium transition-all duration-300 flex items-center space-x-2 hover:scale-105">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
                </svg>
                <span>Zurück zur Übersicht</span>
            </a>
        </div>
    </div>

    <!-- Hauptformular -->
    <div class="admin-form-container rounded-2xl p-8 transition-all duration-300">
        <form id="userForm" action="{{ url_for('admin_create_user_form') }}" method="POST" class="space-y-8">
            <!-- CSRF Token -->
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
            
            <!-- Formular-Header -->
            <div class="border-b border-slate-200 dark:border-slate-700 pb-6">
                <h2 class="text-xl font-semibold text-slate-900 dark:text-white mb-2 transition-colors duration-300">
                    Benutzerdaten eingeben
                </h2>
                <p class="text-slate-600 dark:text-slate-400 transition-colors duration-300">
                    Bitte füllen Sie alle erforderlichen Felder aus, um einen neuen Benutzer zu erstellen.
                </p>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
                <!-- Linke Spalte: Grunddaten -->
                <div class="space-y-6">
                    <!-- E-Mail -->
                    <div class="form-field-premium">
                        <label for="email" class="block text-sm font-semibold text-slate-900 dark:text-white mb-3 transition-colors duration-300">
                            <svg class="w-4 h-4 inline mr-2 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 12a4 4 0 10-8 0 4 4 0 008 0zm0 0v1.5a2.5 2.5 0 005 0V12a9 9 0 10-9 9m4.5-1.206a8.959 8.959 0 01-4.5 1.207"/>
                            </svg>
                            E-Mail-Adresse *
                        </label>
                        <input type="email" 
                               id="email" 
                               name="email" 
                               required 
                               class="input-premium w-full px-4 py-3 rounded-xl focus:outline-none transition-all duration-300 text-slate-900 dark:text-white placeholder-slate-500 dark:placeholder-slate-400"
                               placeholder="beispiel@mercedes-benz.com"
                               data-validation="email">
                        <div class="validation-message hidden mt-2 text-sm text-red-500"></div>
                    </div>

                    <!-- Vollständiger Name -->
                    <div class="form-field-premium">
                        <label for="name" class="block text-sm font-semibold text-slate-900 dark:text-white mb-3 transition-colors duration-300">
                            <svg class="w-4 h-4 inline mr-2 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
                            </svg>
                            Vollständiger Name
                        </label>
                        <input type="text" 
                               id="name" 
                               name="name" 
                               class="input-premium w-full px-4 py-3 rounded-xl focus:outline-none transition-all duration-300 text-slate-900 dark:text-white placeholder-slate-500 dark:placeholder-slate-400"
                               placeholder="Max Mustermann"
                               data-validation="name">
                        <div class="validation-message hidden mt-2 text-sm text-gray-500">
                            Optional: Wird für die Anzeige im System verwendet
                        </div>
                    </div>
                </div>

                <!-- Rechte Spalte: Sicherheit & Rolle -->
                <div class="space-y-6">
                    <!-- Passwort -->
                    <div class="form-field-premium">
                        <label for="password" class="block text-sm font-semibold text-slate-900 dark:text-white mb-3 transition-colors duration-300">
                            <svg class="w-4 h-4 inline mr-2 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"/>
                            </svg>
                            Passwort *
                        </label>
                        <div class="relative">
                            <input type="password" 
                                   id="password" 
                                   name="password" 
                                   required 
                                   minlength="6"
                                   class="input-premium w-full px-4 py-3 pr-12 rounded-xl focus:outline-none transition-all duration-300 text-slate-900 dark:text-white placeholder-slate-500 dark:placeholder-slate-400"
                                   placeholder="Mindestens 6 Zeichen"
                                   data-validation="password">
                            <button type="button" 
                                    id="togglePassword" 
                                    class="absolute right-3 top-1/2 transform -translate-y-1/2 text-slate-500 hover:text-slate-700 dark:text-slate-400 dark:hover:text-slate-200 transition-colors duration-300">
                                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
                                </svg>
                            </button>
                        </div>
                        <div class="validation-message hidden mt-2 text-sm text-red-500"></div>
                        <!-- Passwort-Stärke-Anzeige -->
                        <div id="passwordStrength" class="hidden mt-3">
                            <div class="flex space-x-1 mb-2">
                                <div class="strength-bar h-1 flex-1 bg-gray-200 dark:bg-gray-700 rounded"></div>
                                <div class="strength-bar h-1 flex-1 bg-gray-200 dark:bg-gray-700 rounded"></div>
                                <div class="strength-bar h-1 flex-1 bg-gray-200 dark:bg-gray-700 rounded"></div>
                                <div class="strength-bar h-1 flex-1 bg-gray-200 dark:bg-gray-700 rounded"></div>
                            </div>
                            <div class="text-xs text-slate-600 dark:text-slate-400">
                                <span id="strengthText">Passwort-Stärke</span>
                            </div>
                        </div>
                    </div>

                    <!-- Benutzerrolle -->
                    <div class="form-field-premium">
                        <label for="role" class="block text-sm font-semibold text-slate-900 dark:text-white mb-3 transition-colors duration-300">
                            <svg class="w-4 h-4 inline mr-2 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"/>
                            </svg>
                            Benutzerrolle
                        </label>
                        <select id="role" 
                                name="role" 
                                class="input-premium w-full px-4 py-3 rounded-xl focus:outline-none transition-all duration-300 text-slate-900 dark:text-white">
                            <option value="user">👤 Standard-Benutzer</option>
                            <option value="admin">⚙️ Administrator (Ausbilder)</option>
                        </select>
                        <div class="mt-2 text-sm text-slate-600 dark:text-slate-400">
                            <span id="roleDescription">Standard-Zugriff auf das MYP System</span>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Informations-Box -->
            <div class="info-box-premium rounded-xl p-6 transition-all duration-300">
                <div class="flex items-start space-x-3">
                    <div class="flex-shrink-0">
                        <svg class="w-6 h-6 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                    </div>
                    <div class="flex-1">
                        <h3 class="text-sm font-semibold text-slate-900 dark:text-white mb-2 transition-colors duration-300">
                            Wichtige Hinweise zum Erstellen von Benutzern
                        </h3>
                        <ul class="text-sm text-slate-700 dark:text-slate-300 space-y-1 transition-colors duration-300">
                            <li class="flex items-center space-x-2">
                                <svg class="w-4 h-4 text-green-500 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
                                </svg>
                                <span>Felder mit * sind Pflichtfelder und müssen ausgefüllt werden</span>
                            </li>
                            <li class="flex items-center space-x-2">
                                <svg class="w-4 h-4 text-green-500 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
                                </svg>
                                <span>Das Passwort muss mindestens 6 Zeichen lang sein</span>
                            </li>
                            <li class="flex items-center space-x-2">
                                <svg class="w-4 h-4 text-green-500 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
                                </svg>
                                <span>Der Benutzername wird automatisch aus der E-Mail-Adresse generiert</span>
                            </li>
                            <li class="flex items-center space-x-2">
                                <svg class="w-4 h-4 text-amber-500 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd"/>
                                </svg>
                                <span>Administratoren haben Vollzugriff auf das Mercedes-Benz MYP System</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Aktions-Buttons -->
            <div class="flex flex-col sm:flex-row space-y-3 sm:space-y-0 sm:space-x-4 pt-6 border-t border-slate-200 dark:border-slate-700">
                <button type="submit" 
                        id="submitBtn"
                        class="btn-mercedes-primary text-white px-8 py-4 rounded-xl font-semibold transition-all duration-300 flex items-center justify-center space-x-3 hover:scale-105 disabled:opacity-50 disabled:cursor-not-allowed">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                    </svg>
                    <span>Benutzer erstellen</span>
                    <div class="loading-spinner hidden ml-2">
                        <svg class="animate-spin w-4 h-4 text-white" fill="none" viewBox="0 0 24 24">
                            <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                            <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                        </svg>
                    </div>
                </button>
                
                <a href="{{ url_for('admin_page', tab='users') }}" 
                   class="btn-mercedes-secondary text-white px-8 py-4 rounded-xl font-semibold text-center transition-all duration-300 flex items-center justify-center space-x-3 hover:scale-105">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                    </svg>
                    <span>Abbrechen</span>
                </a>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('userForm');
    const emailInput = document.getElementById('email');
    const passwordInput = document.getElementById('password');
    const nameInput = document.getElementById('name');
    const roleSelect = document.getElementById('role');
    const submitBtn = document.getElementById('submitBtn');
    const togglePasswordBtn = document.getElementById('togglePassword');
    
    // Passwort-Sichtbarkeit umschalten
    togglePasswordBtn.addEventListener('click', function() {
        const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
        passwordInput.setAttribute('type', type);
        
        // Icon ändern
        const icon = this.querySelector('svg path:last-child');
        if (type === 'text') {
            // Auge durchgestrichen
            icon.setAttribute('d', 'M3 3l18 18M10.584 10.587a2 2 0 002.828 2.829M9.363 5.365A9.466 9.466 0 0112 5c4.478 0 8.268 2.943 9.542 7a9.564 9.564 0 01-1.226 1.686m-2.854 2.852A9.465 9.465 0 0112 19c-4.478 0-8.268-2.943-9.542-7a9.564 9.564 0 011.226-1.686');
        } else {
            // Normales Auge
            icon.setAttribute('d', 'M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z');
        }
    });
    
    // Rolle-Beschreibung aktualisieren
    const roleDescriptions = {
        'user': 'Standard-Zugriff auf das MYP System - kann Reservierungen erstellen und verwalten',
        'admin': 'Vollzugriff auf das System - kann Benutzer verwalten, Systemeinstellungen ändern und alle Bereiche einsehen'
    };
    
    roleSelect.addEventListener('change', function() {
        const roleDescription = document.getElementById('roleDescription');
        roleDescription.textContent = roleDescriptions[this.value] || roleDescriptions['user'];
    });
    
    // E-Mail-Validierung
    function validateEmail(email) {
        const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
        return emailRegex.test(email);
    }
    
    // Passwort-Stärke bewerten
    function getPasswordStrength(password) {
        let score = 0;
        if (password.length >= 6) score++;
        if (password.length >= 8) score++;
        if (/[A-Z]/.test(password)) score++;
        if (/[a-z]/.test(password)) score++;
        if (/[0-9]/.test(password)) score++;
        if (/[^A-Za-z0-9]/.test(password)) score++;
        
        return Math.min(score, 4);
    }
    
    // Validierung anzeigen
    function showValidation(input, isValid, message = '') {
        const validationMessage = input.parentNode.querySelector('.validation-message');
        
        input.classList.remove('validation-error', 'validation-success');
        if (validationMessage) {
            validationMessage.classList.add('hidden');
        }
        
        if (input.value) {
            if (isValid) {
                input.classList.add('validation-success');
            } else {
                input.classList.add('validation-error');
                if (validationMessage && message) {
                    validationMessage.textContent = message;
                    validationMessage.classList.remove('hidden');
                }
            }
        }
    }
    
    // Passwort-Stärke anzeigen
    function updatePasswordStrength(password) {
        const strengthContainer = document.getElementById('passwordStrength');
        const strengthBars = strengthContainer.querySelectorAll('.strength-bar');
        const strengthText = document.getElementById('strengthText');
        
        if (password.length === 0) {
            strengthContainer.classList.add('hidden');
            return;
        }
        
        strengthContainer.classList.remove('hidden');
        const strength = getPasswordStrength(password);
        
        const colors = ['bg-red-500', 'bg-yellow-500', 'bg-blue-500', 'bg-green-500'];
        const texts = ['Sehr schwach', 'Schwach', 'Mittel', 'Stark'];
        
        strengthBars.forEach((bar, index) => {
            bar.className = 'strength-bar h-1 flex-1 rounded transition-colors duration-300';
            if (index < strength) {
                bar.classList.add(colors[strength - 1]);
            } else {
                bar.classList.add('bg-gray-200', 'dark:bg-gray-700');
            }
        });
        
        strengthText.textContent = strength > 0 ? `Passwort-Stärke: ${texts[strength - 1]}` : 'Passwort-Stärke';
    }
    
    // Event Listeners für Validierung
    emailInput.addEventListener('blur', function() {
        const isValid = validateEmail(this.value);
        showValidation(this, isValid, 'Bitte geben Sie eine gültige E-Mail-Adresse ein');
    });
    
    emailInput.addEventListener('input', function() {
        if (this.value) {
            const isValid = validateEmail(this.value);
            showValidation(this, isValid);
        }
    });
    
    passwordInput.addEventListener('input', function() {
        const isValid = this.value.length >= 6;
        showValidation(this, isValid, 'Das Passwort muss mindestens 6 Zeichen lang sein');
        updatePasswordStrength(this.value);
    });
    
    // Form-Submit-Validierung
    form.addEventListener('submit', function(e) {
        e.preventDefault();
        
        const email = emailInput.value.trim();
        const password = passwordInput.value.trim();
        
        let isValid = true;
        let firstErrorField = null;
        
        // E-Mail validieren
        if (!email) {
            showValidation(emailInput, false, 'E-Mail-Adresse ist erforderlich');
            isValid = false;
            if (!firstErrorField) firstErrorField = emailInput;
        } else if (!validateEmail(email)) {
            showValidation(emailInput, false, 'Bitte geben Sie eine gültige E-Mail-Adresse ein');
            isValid = false;
            if (!firstErrorField) firstErrorField = emailInput;
        }
        
        // Passwort validieren
        if (!password) {
            showValidation(passwordInput, false, 'Passwort ist erforderlich');
            isValid = false;
            if (!firstErrorField) firstErrorField = passwordInput;
        } else if (password.length < 6) {
            showValidation(passwordInput, false, 'Das Passwort muss mindestens 6 Zeichen lang sein');
            isValid = false;
            if (!firstErrorField) firstErrorField = passwordInput;
        }
        
        if (!isValid) {
            if (firstErrorField) {
                firstErrorField.focus();
                firstErrorField.scrollIntoView({ behavior: 'smooth', block: 'center' });
            }
            return;
        }
        
        // Loading-State anzeigen
        submitBtn.disabled = true;
        submitBtn.querySelector('.loading-spinner').classList.remove('hidden');
        submitBtn.querySelector('span').textContent = 'Wird erstellt...';
        
        // Formular absenden
        setTimeout(() => {
            form.submit();
        }, 500);
    });
    
    // Initiale Rolle-Beschreibung setzen
    const roleDescription = document.getElementById('roleDescription');
    roleDescription.textContent = roleDescriptions[roleSelect.value];
    
    console.log('✅ Admin Benutzer-Formular erfolgreich initialisiert');
});
</script>
{% endblock %} 