{% load django_htmx %}
{% if supervisor.trainee.all %}
    <ul class="list-group list-group-flush">
        {% for trainee in supervisor.trainee.all %}
            {% with trainee_user=trainee.user %}
                <li class="list-group-item px-0 py-3 trainee-row">
                    <div class="d-flex flex-wrap justify-content-between align-items-center gap-2">
                        <div>
                            <div class="fw-semibold">
                                {% if trainee_user %}
                                    {{ trainee_user.get_full_name|default:trainee_user.username }}
                                {% else %}
                                    {{ trainee }}
                                {% endif %}
                            </div>
                            <div class="text-muted small">
                                {% if trainee_user and trainee_user.email %}
                                    {{ trainee_user.email }}
                                {% else %}
                                    No email on file
                                {% endif %}
                            </div>
                        </div>

                        <div class="d-flex gap-1">
                            {% if trainee_user %}
                                <a href="{% url 'account_profile' trainee_user.username %}"
                                   class="btn btn-sm btn-outline-secondary">
                                    Profile
                                </a>
                            {% endif %}
                            {% if request.user == supervisor.user or request.user.is_superuser or request.user.groups.filter|length %}
                                <form method="post" action="{% url 'generic:supervisor_trainee_remove' supervisor.pk trainee_user.pk %}" style="display:inline;">
                                    {% csrf_token %}
                                    <button type="submit"
                                            class="btn btn-sm btn-outline-danger"
                                            hx-post="{% url 'generic:supervisor_trainee_remove' supervisor.pk trainee_user.pk %}"
                                            hx-target="#trainees-list-content"
                                            hx-swap="outerHTML"
                                            hx-confirm="Remove {{ trainee_user.get_full_name|default:trainee_user.username }}?">
                                        Remove
                                    </button>
                                </form>
                            {% endif %}
                        </div>
                    </div>
                </li>
            {% endwith %}
        {% endfor %}
    </ul>
{% else %}
    <div class="alert alert-secondary mb-0">
        No trainees are currently linked to this supervisor.
    </div>
{% endif %}
