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

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

{% block content %}
    <div class="clearfix">
        <h1 class="page-header text-center">{% translate "Available Groups" %}</h1>

        <div class="panel panel-default">
            <div class="panel-heading">
                <div class="panel-title">{% translate "Groups" %}</div>
            </div>
            <div class="panel-body">
                {% if groups %}
                    <table class="table table-striped" id="available-groups">
                        <thead>
                            <tr>
                                <th>{% translate "Name" %}</th>
                                <th>{% translate "Description" %}</th>
                                <th></th>
                            </tr>
                        </thead>

                        <tbody>
                            {% for g in groups %}
                                <tr>
                                    <td>{{ g.group.name }}</td>
                                    <td>{{ g.group.authgroup.description|linebreaks|urlize }}</td>
                                    <td class="text-right">
                                        {% if g.group in user.groups.all %}
                                            {% if not g.request %}
                                                <a href="{% url 'groupmanagement:request_leave' g.group.id %}" class="btn btn-danger">
                                                    {% translate "Leave" %}
                                                </a>
                                            {% else %}
                                                <button type="button" class="btn btn-primary" disabled>
                                                    {{ g.request.status }}
                                                </button>
                                            {% endif %}
                                        {% elif not g.request %}
                                            {% if g.group.authgroup.open %}
                                                <a href="{% url 'groupmanagement:request_add' g.group.id %}" class="btn btn-success">
                                                    {% translate "Join" %}
                                                </a>
                                            {% else %}
                                                <a href="{% url 'groupmanagement:request_add' g.group.id %}" class="btn btn-primary">
                                                    {% translate "Request" %}
                                                </a>
                                            {% endif %}
                                        {% else %}
                                            <button type="button" class="btn btn-primary" disabled>
                                                {{ g.request.status }}
                                            </button>
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                {% else %}
                    <div class="alert alert-warning text-center">
                        {% translate "No groups available." %}
                    </div>
                {% endif %}
            </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 type="application/javascript">
        $(document).ready(function() {
            {% if groups %}
                $('#available-groups').DataTable({
                    paging: false,
                    info: false,
                    order: [
                        [0, 'asc']
                    ],
                    columnDefs: [
                        {
                            width: '250px',
                            targets: [0]
                        },
                        {
                            width: '150px',
                            targets: [2]
                        },
                        {
                            orderable: false,
                            targets: [1,2]
                        }
                    ],
                });
            {% endif %}
        });
    </script>
{% endblock extra_javascript %}
