{% extends "management/base.html" %}
{% load i18n lucide static %}

{% block heading %}{% trans "Add multiple members" %}{% endblock heading %}
{% block subheading %}{{ group.name }}{% endblock subheading %}

{% block panel %}
    <form method="post">
        {% csrf_token %}
        {{ formset.management_form }}

        <div class="card bg-base-100 shadow">
            <div class="card-body">
                {% for error in formset.non_form_errors %}
                    <div class="alert alert-error my-2">
                        <span>{{ error }}</span>
                    </div>
                {% endfor %}

                {% comment %}
                    Deliberately not wrapped in the usual overflow-x-auto: that makes a
                    scroll container, which clips the member picker's absolutely
                    positioned dropdown for every row but the first couple.
                {% endcomment %}
                <table class="table w-full">
                    <thead>
                        <tr>
                            <th>{% trans "Member" %}</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody id="bulk-add-rows" data-prefix="{{ formset.prefix }}">
                        {% for form in formset %}
                            {% include "management/_group_bulk_add_row.html" with form=form %}
                        {% endfor %}
                    </tbody>
                </table>

                <template id="bulk-add-row-template">
                    {% include "management/_group_bulk_add_row.html" with form=formset.empty_form %}
                </template>

                <div class="flex flex-wrap items-center gap-2 mt-2">
                    <button class="btn btn-outline btn-sm gap-2" type="button" id="add-row">{% lucide "plus" size=14 %} {% trans "Add another row" %}</button>
                    <button class="btn btn-primary btn-sm gap-2" type="submit">{% lucide "users" size=14 %} {% trans "Add to group" %}</button>
                </div>
            </div>
        </div>

        <div class="mt-4">
            <a class="btn btn-outline gap-2" href="{% url 'management:group_detail' group.pk %}">{% lucide "arrow-left" size=16 %} {% trans "Back" %}</a>
        </div>
    </form>
{% endblock panel %}

{% block extra_body %}
    <script src="{% static 'js/searchable-select.js' %}"></script>
    <script src="{% static 'js/bulk-add-rows.js' %}"></script>
{% endblock extra_body %}
