{% extends "allianceauth/base.html" %}
{% load i18n %}
{% load evelinks %}

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

{% block content %}
    <div class="clearfix">
        <br>
        {% include 'groupmanagement/menu.html' %}

        <ul class="nav nav-tabs">
            <li class="active">
                <a data-toggle="tab" href="#add">
                    {% translate "Join Requests" %}

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

            <li>
                <a data-toggle="tab" href="#leave">
                    {% translate "Leave Requests" %}

                    {% if leaverequests %}
                        <span class="badge">{{ leaverequests|length }}</span>
                    {% endif %}
                </a>
            </li>
        </ul>

        <div class="tab-content">
            <div id="add" class="tab-pane fade in active panel panel-default">
                <div class="panel-body">
                    {% if acceptrequests %}
                        <table class="table table-striped table-hover" id="group-accept-requests" style="width: 100%;">
                            <thead>
                                <tr>
                                    <th>{% translate "Character" %}</th>
                                    <th>{% translate "Organization" %}</th>
                                    <th>{% translate "Group" %}</th>
                                    <th></th>
                                </tr>
                            </thead>

                            <tbody>
                                {% for acceptrequest in acceptrequests %}
                                    <tr>
                                        <td class="valign-middle">
                                            {% if acceptrequest.main_char %}
                                                <img src="{{ acceptrequest.main_char|character_portrait_url:32 }}" class="img-rounded" style="margin-right: 0.25rem;" alt="{{ acceptrequest.main_char.character_name }}">

                                                <a href="{{ acceptrequest.main_char|evewho_character_url }}" target="_blank">
                                                    {{ acceptrequest.main_char.character_name }}
                                                </a>
                                            {% else %}
                                                {{ acceptrequest.user.username }}
                                            {% endif %}
                                        </td>
                                        <td class="valign-middle">
                                            {% 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 class="valign-middle">{{ acceptrequest.group.name }}</td>
                                        <td class="text-right valign-middle">
                                            <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>
                    {% else %}
                        <div class="alert alert-warning text-center">{% translate "No group add requests." %}</div>
                    {% endif %}
                </div>
            </div>

            <div id="leave" class="tab-pane fade panel panel-default">
                <div class="panel-body">
                    {% if leaverequests %}
                        <table class="table table-striped table-hover" id="group-leave-requests" style="width: 100%;">
                            <thead>
                                <tr>
                                    <th>{% translate "Character" %}</th>
                                    <th>{% translate "Organization" %}</th>
                                    <th>{% translate "Group" %}</th>
                                    <th></th>
                                </tr>
                            </thead>

                            <tbody>
                                {% for leaverequest in leaverequests %}
                                    <tr>
                                        <td class="valign-middle">
                                            {% if leaverequest.main_char %}
                                                <img src="{{ leaverequest.main_char|character_portrait_url:32 }}" class="img-rounded" style="margin-right: 0.25rem;" alt="{{ leaverequest.main_char.character_name }}">

                                                <a href="{{ leaverequest.main_char|evewho_character_url }}" target="_blank">
                                                    {{ leaverequest.main_char.character_name }}
                                                </a>
                                            {% else %}
                                                {{ leaverequest.user.username }}
                                            {% endif %}
                                        </td>
                                        <td class="valign-middle">
                                            {% 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 class="valign-middle">{{ leaverequest.group.name }}</td>
                                        <td class="text-right valign-middle">
                                            <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>
                    {% else %}
                        <div class="alert alert-warning text-center">{% translate "No group leave requests." %}</div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endblock content %}

{% block extra_css %}
    {% include 'bundles/datatables-css.html' %}
{% endblock extra_css %}

{% block extra_javascript %}
    {% include 'bundles/datatables-js.html' %}

    <script>
        $(document).ready(function() {
            {% if acceptrequests %}
                $('#group-accept-requests').DataTable({
                    paging: false,
                    info: false,
                    order: [
                        [0, 'asc']
                    ],
                    columnDefs: [
                        {
                            width: '250px',
                            targets: [0]
                        },
                        {
                            width: '200px',
                            targets: [3]
                        },
                        {
                            orderable: false,
                            targets: [3]
                        }
                    ],
                });
            {% endif %}

            {% if leaverequests %}
                $('#group-leave-requests').DataTable({
                    paging: false,
                    info: false,
                    order: [
                        [0, 'asc']
                    ],
                    columnDefs: [
                        {
                            width: '250px',
                            targets: [0]
                        },
                        {
                            width: '200px',
                            targets: [3]
                        },
                        {
                            orderable: false,
                            targets: [3]
                        }
                    ],
                });
            {% endif %}

            $('a[data-toggle="tab"]').on('shown.bs.tab', function() {
                $.fn.dataTable.tables({
                    visible: true,
                    api: true
                }).columns.adjust().draw();
            })
        });
    </script>
{% endblock extra_javascript %}
