{% 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-shield-alt me-2"></i>{{ title }}
                </h1>
                <a href="{% url 'stiftung:user_detail' user_obj.pk %}" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-1"></i>Zurück zum Benutzer
                </a>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card shadow">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">
                        <i class="fas fa-key me-2"></i>Berechtigungen verwalten
                    </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 %}

                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i>
                        <strong>Hinweis:</strong> Wählen Sie die Berechtigungen aus, die diesem Benutzer gewährt werden sollen. Änderungen werden sofort wirksam.
                    </div>

                    <form method="post" id="permissionsForm">
                        {% csrf_token %}
                        
                        <!-- Permission Groups -->
                        {% for group_key, group_data in permission_groups.items %}
                            {% if group_data.permissions %}
                            <div class="card mb-4">
                                <div class="card-header">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <h6 class="mb-0">
                                            <i class="{{ group_data.icon }} me-2"></i>{{ group_data.name }}
                                        </h6>
                                        <div>
                                            <button type="button" class="btn btn-outline-primary btn-sm" onclick="toggleGroupPermissions('{{ group_key }}', true)">
                                                <i class="fas fa-check-square me-1"></i>Alle auswählen
                                            </button>
                                            <button type="button" class="btn btn-outline-secondary btn-sm" onclick="toggleGroupPermissions('{{ group_key }}', false)">
                                                <i class="fas fa-square me-1"></i>Alle abwählen
                                            </button>
                                        </div>
                                    </div>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        {% for permission_data in group_data.permissions %}
                                            {% if permission_data|length == 3 %}
                                                {% with field_name=permission_data.0 field=permission_data.1 permission=permission_data.2 %}
                                                <div class="col-md-6 mb-3">
                                                    <div class="form-check">
                                                        {{ field }}
                                                        <label class="form-check-label" for="{{ field.id_for_label }}">
                                                            {% if permission %}
                                                                {{ permission.name }}
                                                            {% else %}
                                                                {{ field.label }}
                                                            {% endif %}
                                                        </label>
                                                    </div>
                                                </div>
                                                {% endwith %}
                                            {% else %}
                                                {% with field_name=permission_data.0 field=permission_data.1 %}
                                                <div class="col-md-6 mb-3">
                                                    <div class="form-check">
                                                        {{ field }}
                                                        <label class="form-check-label" for="{{ field.id_for_label }}">
                                                            {{ field.label }}
                                                        </label>
                                                    </div>
                                                </div>
                                                {% endwith %}
                                            {% endif %}
                                        {% endfor %}
                                    </div>
                                </div>
                            </div>
                            {% endif %}
                        {% endfor %}

                        <hr>
                        <div class="d-flex justify-content-between">
                            <a href="{% url 'stiftung:user_detail' user_obj.pk %}" class="btn btn-outline-secondary">
                                <i class="fas fa-times me-1"></i>Abbrechen
                            </a>
                            <div>
                                <button type="button" class="btn btn-outline-primary me-2" onclick="selectAllPermissions()">
                                    <i class="fas fa-check-square me-1"></i>Alle auswählen
                                </button>
                                <button type="button" class="btn btn-outline-secondary me-2" onclick="clearAllPermissions()">
                                    <i class="fas fa-square me-1"></i>Alle abwählen
                                </button>
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-save me-1"></i>Berechtigungen speichern
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>


<script>
function toggleGroupPermissions(groupKey, select) {
    const checkboxes = document.querySelectorAll(`input[type="checkbox"][id*="perm_"]`);
    const groupCard = document.querySelector(`[onclick*="${groupKey}"]`).closest('.card');
    const groupCheckboxes = groupCard.querySelectorAll('input[type="checkbox"]');
    
    groupCheckboxes.forEach(checkbox => {
        checkbox.checked = select;
    });
}

function selectAllPermissions() {
    const checkboxes = document.querySelectorAll('input[type="checkbox"][id*="perm_"]');
    checkboxes.forEach(checkbox => {
        checkbox.checked = true;
    });
}

function clearAllPermissions() {
    const checkboxes = document.querySelectorAll('input[type="checkbox"][id*="perm_"]');
    checkboxes.forEach(checkbox => {
        checkbox.checked = false;
    });
}

// Show confirmation for changes
document.getElementById('permissionsForm').addEventListener('submit', function(e) {
    const checkedCount = document.querySelectorAll('input[type="checkbox"][id*="perm_"]:checked').length;
    const totalCount = document.querySelectorAll('input[type="checkbox"][id*="perm_"]').length;
    
    if (checkedCount === 0) {
        if (!confirm('Sie haben keine Berechtigungen ausgewählt. Der Benutzer hat dann nur sehr eingeschränkte Zugriffsrechte. Fortfahren?')) {
            e.preventDefault();
        }
    } else if (checkedCount === totalCount) {
        if (!confirm('Sie haben alle Berechtigungen ausgewählt. Der Benutzer hat dann vollständigen Zugriff auf alle Funktionen. Fortfahren?')) {
            e.preventDefault();
        }
    }
});
</script>
{% endblock %}
