{% extends "sysadmin/base.html" %}
{% load i18n seahub_tags %}

{% block cur_invitations %}tab-cur{% endblock %}

{% block right_panel %}

<div class="hd ovhd">
    <h3 class="fleft">{% trans "All Invitations" %}</h3>
    <button id="rm-all-expired" class="fright btn-white">{% trans "Delete Expired Invitations" %}</button>
</div>

{% if invitations %}
<table>
    <tr>
        <th width="20%">{% trans "Inviter" %}</th>
        <th width="20%">{% trans "Accepter" %}</th>
        <th width="9%">{% trans "Type" %}</th>
        <th width="14%">{% trans "Invited at" %}</th>
        <th width="14%">{% trans "Accepted at" %}</th>
        <th width="18%">{% trans "Expired at" %}</th>
        <th width="8%"></th>
    </tr>
    {% for invitation in invitations %}
    <tr>
        <td><a href="{% url 'user_info' invitation.inviter %}">{{ invitation.inviter }}</a></td>
        <td>
            {% if invitation.accept_time %}
            <a href="{% url 'user_info' invitation.accepter %}">{{ invitation.accepter }}</a>
            {% else %}
            {{ invitation.accepter }}
            {% endif %}
        </td>
        <td>{{ invitation.invite_type }}</td>
        <td>{{ invitation.invite_time|translate_seahub_time }}</td>
        <td>
            {% if invitation.accept_time %}
            {{ invitation.accept_time|translate_seahub_time }}
            {% else %}
            --
            {% endif %}
        </td>
        {% if invitation.is_expired %}
        <td class="error">{{ invitation.expire_time }}</td>
        {% else %}
        <td>{{ invitation.expire_time }}</td>
        {% endif %}
        <td>
            <a href="#" data-id="{{invitation.pk}}" class="sf2-icon-delete sf2-x rm op-icon vh" title="{% trans "Delete" %}" aria-label="{% trans "Delete" %}"></a>
        </td>
    </tr>
    {% endfor %}
</table>

{% include "snippets/admin_paginator.html" %}
{% else %}
<div class="empty-tips">
    <p>{% trans "There is no invitation yet." %}</p>
</div>
{% endif %}
{% endblock %}

{% block extra_script %}
<script type="text/javascript">
// rm a single invitation
$('.rm').on('click', function() {
    var _this = $(this);
    $.ajax({
        url: '{% url 'sys_invitation_remove' %}',
        type: 'POST',
        data: {'inv_id': _this.attr('data-id')},
        cache: false,
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        success: function() {
            _this.closest('tr').remove();
        },
        error: ajaxErrorHandler
    });
    return false;
});

// rm all expired invitations
$("#rm-all-expired").on('click', function() {
    $.ajax({
        url: '{% url 'api-v2.1-admin-invitations' %}?type=expired',
        type: 'DELETE',
        cache: false,
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        success: function(data) {
            location.reload(true);
        },
        error: ajaxErrorHandler
    });
});

</script>
{% endblock %}
