{% extends 'base.html' %}

{% block title %}{{ title }} - Benutzerverwaltung - van Hees-Theyssen-Vogel'sche Stiftung{% endblock %}

{% block content %}

    <div class="row">
        <div class="col-12">
            <div class="d-sm-flex align-items-center justify-content-between mb-4">
                <h1 class="h3 mb-0 text-gray-800">
                    <i class="fas fa-user-edit me-2"></i>{{ title }}
                </h1>
                <a href="{% url 'stiftung:user_management' %}" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-1"></i>Zurück zur Übersicht
                </a>
            </div>
        </div>
    </div>

    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-user me-2"></i>Benutzerdaten
                    </h6>
                </div>
                <div class="card-body">
                    {% if form.errors %}
                    <div class="alert alert-danger">
                        <h6><i class="fas fa-exclamation-triangle me-2"></i>Fehler beim Speichern:</h6>
                        {% for field, errors in form.errors.items %}
                            {% for error in errors %}
                                <div>{{ error }}</div>
                            {% endfor %}
                        {% endfor %}
                    </div>
                    {% endif %}

                    <form method="post">
                        {% csrf_token %}
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.username.id_for_label }}" class="form-label">
                                        {{ form.username.label }}
                                        <span class="text-danger">*</span>
                                    </label>
                                    {{ form.username }}
                                    {% if form.username.help_text %}
                                    <div class="form-text">{{ form.username.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.email.id_for_label }}" class="form-label">
                                        {{ form.email.label }}
                                        <span class="text-danger">*</span>
                                    </label>
                                    {{ form.email }}
                                    {% if form.email.help_text %}
                                    <div class="form-text">{{ form.email.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.first_name.id_for_label }}" class="form-label">{{ form.first_name.label }}</label>
                                    {{ form.first_name }}
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.last_name.id_for_label }}" class="form-label">{{ form.last_name.label }}</label>
                                    {{ form.last_name }}
                                </div>
                            </div>
                        </div>

                        {% if form.password1 %}
                        <hr>
                        <h6 class="text-primary mb-3">
                            <i class="fas fa-lock me-2"></i>Passwort
                        </h6>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.password1.id_for_label }}" class="form-label">
                                        {{ form.password1.label }}
                                        <span class="text-danger">*</span>
                                    </label>
                                    {{ form.password1 }}
                                    {% if form.password1.help_text %}
                                    <div class="form-text">{{ form.password1.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group mb-3">
                                    <label for="{{ form.password2.id_for_label }}" class="form-label">
                                        {{ form.password2.label }}
                                        <span class="text-danger">*</span>
                                    </label>
                                    {{ form.password2 }}
                                    {% if form.password2.help_text %}
                                    <div class="form-text">{{ form.password2.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                        {% endif %}

                        <hr>
                        <h6 class="text-primary mb-3">
                            <i class="fas fa-cog me-2"></i>Benutzereinstellungen
                        </h6>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-check mb-3">
                                    {{ form.is_active }}
                                    <label class="form-check-label" for="{{ form.is_active.id_for_label }}">
                                        {{ form.is_active.label }}
                                    </label>
                                    {% if form.is_active.help_text %}
                                    <div class="form-text">{{ form.is_active.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-check mb-3">
                                    {{ form.is_staff }}
                                    <label class="form-check-label" for="{{ form.is_staff.id_for_label }}">
                                        {{ form.is_staff.label }}
                                    </label>
                                    {% if form.is_staff.help_text %}
                                    <div class="form-text">{{ form.is_staff.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        {% if form.send_welcome_email %}
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-check mb-3">
                                    {{ form.send_welcome_email }}
                                    <label class="form-check-label" for="{{ form.send_welcome_email.id_for_label }}">
                                        {{ form.send_welcome_email.label }}
                                    </label>
                                    {% if form.send_welcome_email.help_text %}
                                    <div class="form-text">{{ form.send_welcome_email.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                        {% endif %}

                        <hr>
                        <div class="d-flex justify-content-between">
                            <a href="{% url 'stiftung:user_management' %}" class="btn btn-outline-secondary">
                                <i class="fas fa-times me-1"></i>Abbrechen
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save me-1"></i>Speichern
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            {% if user_obj %}
            <!-- Additional Actions for Existing Users -->
            <div class="card shadow mt-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-tools me-2"></i>Weitere Aktionen
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <a href="{% url 'stiftung:user_change_password' user_obj.pk %}" class="btn btn-outline-warning w-100">
                                <i class="fas fa-key d-block mb-2 fa-2x"></i>
                                <span>Passwort ändern</span>
                            </a>
                        </div>
                        <div class="col-md-4 mb-3">
                            <a href="{% url 'stiftung:user_permissions' user_obj.pk %}" class="btn btn-outline-info w-100">
                                <i class="fas fa-shield-alt d-block mb-2 fa-2x"></i>
                                <span>Berechtigungen verwalten</span>
                            </a>
                        </div>
                        <div class="col-md-4 mb-3">
                            <a href="{% url 'stiftung:user_detail' user_obj.pk %}" class="btn btn-outline-primary w-100">
                                <i class="fas fa-user d-block mb-2 fa-2x"></i>
                                <span>Benutzerdetails</span>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}
        </div>
    </div>

{% endblock %}
