{% extends "allianceauth/base-bs5.html" %}
{% load static %}
{% load i18n %}
{% load evelinks %}
{% load navactive %}

{% block page_title %}{% translate "Groups Management" %}{% endblock page_title %}
{% block header_nav_brand %}{% translate "Groups Management" %}{% endblock header_nav_brand %}

{% block extra_css %}
{% endblock extra_css %}

{% block header_nav_collapse_left %}
<li class="active">
    <a class="nav-link active" id="add-tab" data-bs-toggle="tab" data-bs-target="#add" type="button" role="tab" aria-controls="addd" aria-selected="true">
        {% translate "Join Requests" %}

        {% if acceptrequests %}
            <span class="badge bg-secondary">{{ acceptrequests|length }}</span>
        {% endif %}
    </a>
</li>

{%  if not auto_leave %}
    <li>
        <a class="nav-link" id="leave-tab" data-bs-toggle="tab" data-bs-target="#leave" type="button" role="tab" aria-controls="leave" aria-selected="false">
            {% translate "Leave Requests" %}

            {% if leaverequests %}
                <span class="badge bg-secondary">{{ leaverequests|length }}</span>
            {% endif %}
        </a>
    </li>
{% endif %}
<li class="nav-item ">
    <a class="nav-link {% navactive request 'groupmanagement:membership groupmanagement:audit_log' %}" href="{% url 'groupmanagement:membership' %}">{% translate "Group Membership" %}</a>
</li>

{% endblock %}


{% block content %}
<div class="tab-content">
    <div id="add" class="tab-pane active">
        {% if acceptrequests %}
            <div class="table-responsive">
                <table class="table table-aa">
                    <thead>
                        <tr>
                            <th>{% translate "Character" %}</th>
                            <th>{% translate "Organization" %}</th>
                            <th>{% translate "Group" %}</th>
                            <th></th>
                        </tr>
                    </thead>

                    <tbody class="align-middle">
                        {% for acceptrequest in acceptrequests %}
                            <tr>
                                <td>
                                    <img src="{{ acceptrequest.main_char|character_portrait_url:32 }}" class="rounded-circle" style="margin-right: 1rem;" alt="{{ acceptrequest.main_char.character_name }}">
                                    {% if acceptrequest.main_char %}
                                        <a href="{{ acceptrequest.main_char|evewho_character_url }}" target="_blank">
                                            {{ acceptrequest.main_char.character_name }}
                                        </a>
                                    {% else %}
                                        {{ acceptrequest.user.username }}
                                    {% endif %}
                                </td>
                                <td>
                                    {% if acceptrequest.main_char %}
                                        <a href="{{ acceptrequest.main_char|dotlan_corporation_url }}" target="_blank">
                                            {{ acceptrequest.main_char.corporation_name }}
                                        </a><br>
                                        {{ acceptrequest.main_char.alliance_name|default_if_none:"" }}
                                    {% else %}
                                        {% translate "(unknown)" %}
                                    {% endif %}
                                </td>
                                <td>{{ acceptrequest.group.name }}</td>
                                <td class="text-end">
                                    <a href="{% url 'groupmanagement:accept_request' acceptrequest.id %}" class="btn btn-success">
                                        {% translate "Accept" %}
                                    </a>

                                    <a href="{% url 'groupmanagement:reject_request' acceptrequest.id %}" class="btn btn-danger">
                                        {% translate "Reject" %}
                                    </a>
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        {% else %}
            <div class="alert alert-warning text-center">{% translate "No group add requests." %}</div>
        {% endif %}
    </div>

    {%  if not auto_leave %}
        <div id="leave" class="tab-pane">
            {% if leaverequests %}
                <div class="table-responsive">
                    <table class="table table-aa">
                        <thead>
                            <tr>
                                <th>{% translate "Character" %}</th>
                                <th>{% translate "Organization" %}</th>
                                <th>{% translate "Group" %}</th>
                                <th></th>
                            </tr>
                        </thead>

                        <tbody class="align-middle">
                            {% for leaverequest in leaverequests %}
                                <tr>
                                    <td>
                                        <img src="{{ leaverequest.main_char|character_portrait_url:32 }}" class="rounded-circle" style="margin-right: 1rem;" alt="{{ leaverequest.main_char.character_name }}">
                                        {% if leaverequest.main_char %}
                                            <a href="{{ leaverequest.main_char|evewho_character_url }}" target="_blank">
                                                {{ leaverequest.main_char.character_name }}
                                            </a>
                                        {% else %}
                                            {{ leaverequest.user.username }}
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if leaverequest.main_char %}
                                            <a href="{{ leaverequest.main_char|dotlan_corporation_url }}" target="_blank">
                                                {{ leaverequest.main_char.corporation_name }}
                                            </a><br>
                                            {{ leaverequest.main_char.alliance_name|default_if_none:"" }}
                                        {% else %}
                                            {% translate "(unknown)" %}
                                        {% endif %}
                                    </td>
                                    <td>{{ leaverequest.group.name }}</td>
                                    <td class="text-end">
                                        <a href="{% url 'groupmanagement:leave_accept_request' leaverequest.id %}" class="btn btn-success">
                                            {% translate "Accept" %}
                                        </a>

                                        <a href="{% url 'groupmanagement:leave_reject_request' leaverequest.id %}" class="btn btn-danger">
                                            {% translate "Reject" %}
                                        </a>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% else %}
                <div class="alert alert-warning text-center">{% translate "No group leave requests." %}</div>
            {% endif %}
        </div>
    {% endif %}
</div>
{% endblock content %}
