{% extends 'control/base.html' %}
{% load i18n %}

{% block heading %}{{ user.username }}{% endblock %}

{% block subcontent %}
    {% if request.user.is_staff %}
        <a href="{% url 'admin:auth_user_change' user.id %}" class="float-right">{% trans 'show in django admin panel' %}</a>
    {% endif %}
    <h4>{% trans 'General Permissions' %}</h4>
    {% if user_permissions_form %}
        <form method="POST" class="user-permissions-form">
            {% csrf_token %}
            {% for field in user_permissions_form %}
                {% if field.name == 'review_group_reports' %}
                    {{ field.label_tag }} {{ field }}
                {% else %}
                    <label>{{ field }} {{ field.label }}</label><br>
                {% endif %}
            {% endfor %}
            <button type="submit" name="submit_user_permissions" value="1">{% trans 'Save' %}</button>
        </form>
    {% else %}
        <p>
            {% for permission_title in user_permissions %}
                {{ permission_title }},<br>
            {% empty %}
                <em>{% trans 'none' %}</em>
            {% endfor %}
        </p>
    {% endif %}

    <a name="access"></a>
    <h4>{% trans 'Access Permissions' %}</h4>
    {% if access_restriction %}
        <p>
            {% with restriction_title=access_restriction.title %}
                {% blocktrans %}for {{ restriction_title }}{% endblocktrans %}
            {% endwith %} –
            <a href="?">« {% trans 'back' %}</a>
        </p>
        {% if access_permissions %}
            <form method="post">
            {% csrf_token %}
            <table>
                <tr>
                    <th>{% trans 'author' %}</th>
                    <th>{% trans 'expires' %}</th>
                    <th>{% trans 'can grant' %}</th>
                    <th>{% trans 'key' %}</th>
                    <th></th>
                </tr>
                {% for permission in access_permissions %}
                    <tr>
                        <td>
                            {% if permission.author %}
                                <a href="{% url 'control.users.detail' user=permission.author_id %}">{{ permission.author.username }}</a>
                            {% endif %}
                        </td>
                        <td>
                            <span class="{% if permission.expired %}red{% else %}green{% endif %}">
                                {% if permission.expire_date %}
                                    {{ permission.expire_date }}
                                {% else %}
                                    {% trans 'never' %}
                                {% endif %}
                            </span>
                        </td>
                        <td>
                            {% if permission.can_grant %}
                                <strong class="green">{% trans 'Yes' %}</strong>
                            {% else %}
                                {% trans 'No' %}
                            {% endif %}
                        </td>
                        <td>{% if permission.unique_key %}{{ permission.unique_key }}{% endif %}</td>
                        {% if request.user_permissions.grant_all_access or request.user == permission.author %}
                            <td class="button-cell"><button type="submit" name="delete_access_permission" value="{{ permission.pk }}">{% trans 'Delete' %}</button></td>
                        {% endif %}
                    </tr>
                {% endfor %}
            </table>
            </form>
        {% else %}
            <p><em>{% trans 'none' %}</em></p>
        {% endif %}
    {% else %}
        {% trans 'Add' as button_label %}
        {% include 'control/fragment_access_permissions_form.html' with button_label=button_label %}
        {% if access_permissions %}
            <form method="post">
            {% csrf_token %}
            <table>
                <tr>
                    <th>{% trans 'Access Restriction' %}</th>
                    <th>{% trans 'expires' %}</th>
                    <th>{% trans 'can grant' %}</th>
                    <th></th>
                </tr>
                {% for permission in access_permissions %}
                    <tr>
                        <td>{{ permission.title }}</td>
                        <td>
                            <span class="{% if permission.expired %}red{% else %}green{% endif %}">
                                {% if permission.expire_date %}
                                    {{ permission.expire_date }}
                                {% else %}
                                    {% trans 'never' %}
                                {% endif %}
                            </span>
                        </td>
                        <td>
                            {% if permission.can_grant %}
                                <strong class="green">{% trans 'Yes' %}</strong>
                            {% else %}
                                {% trans 'No' %}
                            {% endif %}
                        </td>
                        <td class="button-cell"><a class="button" href="?restriction={{ permission.pk }}">{% trans 'Details' %}</a></td>
                    </tr>
                {% endfor %}
            </table>
            </form>
        {% else %}
            <p><em>{% trans 'none' %}</em></p>
        {% endif %}
    {% endif %}

    {% if space_accesses or space_accesses_form %}
        <p></p>
        <h4>{% trans 'Space Access' %}</h4>
        {{ space_accesses_form.non_field_errors }}
        {{ space_accesses_form.access_permissions.errors }}
        {{ space_accesses_form.expires.errors }}
        <form method="post">
        <table>
            <tr>
                <th>{% trans 'Space' %}</th>
                <th>{% trans 'can edit' %}</th>
                <th></th>
            </tr>
            {% for space_access in space_accesses %}
                <tr>
                    <td>{{ space_access.space.title }}</td>
                    <td>
                        {% if space_access.can_edit %}
                            <strong class="green">{% trans 'Yes' %}</strong>
                        {% else %}
                            {% trans 'No' %}
                        {% endif %}
                    </td>
                    <td class="button-cell">
                        {% csrf_token %}<button type="submit" name="delete_space_access" value="{{ space_access.pk }}">{% trans 'Delete' %}</button>
                    </td>
                </tr>
            {% endfor %}
        </table>
        </form>
        <form method="post">
        <table>
            {% csrf_token %}
            {% if space_accesses_form %}
                <tr>
                    <td class="input-cell">
                        {{ space_accesses_form.space }}
                    </td>
                    <td class="input-cell">
                        {{ space_accesses_form.can_edit }}
                    </td>
                    <td class="button-cell">
                        <button type="submit" name="submit_space_access" value="1">{% trans 'Add' %}</button>
                    </td>
                </tr>
            {% endif %}
                    </form>
        </table>
    {% endif %}
{% endblock %}
