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

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

{% block right_panel %}
    <div class="hd ovhd">
        <ul class="tab-tabs-nav fleft">
            <li class="tab">
                <a href="{{ SITE_ROOT }}sys/publinkadmin/" class="a">{% trans "Download Links" %}</a>
            </li>
            <li class="tab ui-state-active">
                <a href="{{ SITE_ROOT }}sys/uploadlinkadmin/" class="a">{% trans "Upload Links" %}</a>
            </li>
        </ul>
    </div>

{% if uploadlinks %}
<table>
    <tr>
        <th width="25%">{% trans "Name" %}</th>
        <th width="19%">{% trans "Token" %}</th>
        <th width="22%">{% trans "Owner" %}</th>
        <th width="18%"><a class="table-sort-op by-time" href="#">{% trans "Create At" %} {% if sort_by == 'time' %}<span class="sort-icon icon-caret-down"></span>{% elif sort_by == '-time' %}<span class="sort-icon icon-caret-up"></span>{% endif %}</a></th>
        <th width="8%"><a class="table-sort-op by-count" href="#">{% trans "Visits" %} {% if sort_by == 'count' %}<span class="sort-icon icon-caret-up"></span>{% elif sort_by == '-count' %}<span class="sort-icon icon-caret-down"></span>{% endif %}</a></th>
        <th width="8%"></th>
    </tr>
    {% for uploadlink in uploadlinks %}
    <tr>
        <td>{{ uploadlink.path }}</td>
        <td>{{ uploadlink.token }}</td>
        <td><a href="{% url 'user_info' uploadlink.username %}">{{ uploadlink.username }}</a></td>
        <td>{{ uploadlink.ctime|translate_seahub_time }} </td>
        <td>{{ uploadlink.view_cnt }}</td>
        <td>
            <a class="op vh rm-link" href="#" data-token="{{ uploadlink.token }}">{% trans "Remove" %}</a>
        </td>
    </tr>
    {% endfor %}
</table>

<div id="paginator">
    {% if current_page != 1 %}
    <a href="?page={{ prev_page }}&per_page={{ per_page }}&sort_by={{ sort_by }}">{% trans "Previous" %}</a>
    {% endif %}
    {% if page_next %}
    <a href="?page={{ next_page }}&per_page={{ per_page }}&sort_by={{ sort_by }}">{% trans "Next" %}</a>
    {% endif %}
    {% if current_page != 1 or page_next %}
    |
    {% endif %}
    <span>{% trans "Per page: " %}</span>
    {% if per_page == 25 %}
    <span> 25 </span>
    {% else %}
    <a href="?per_page=25&sort_by={{ sort_by }}" class="per-page">25</a>
    {% endif %}
    {% if per_page == 50 %}
    <span> 50 </span>
    {% else %}
    <a href="?per_page=50&sort_by={{ sort_by }}" class="per-page">50</a>
    {% endif %}
    {% if per_page == 100 %}
    <span> 100 </span>
    {% else %}
    <a href="?per_page=100&sort_by={{ sort_by }}" class="per-page">100</a>
    {% endif %}
</div>
{% else %}
<p>{% trans "Empty" %}</p>
{% endif %}
{% endblock %}

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

var sort_by = '{{ sort_by }}';
var url = '{% url 'sys_upload_link_admin' %}' + '?per_page={{ per_page }}';
$(".by-time").on('click', function () {
    if (sort_by == 'time') {
        location.href = url + '&sort_by=-time';
    } else {
        location.href = url + '&sort_by=time';
    }
    return false;
});
$(".by-count").on('click', function () {
    if (sort_by == 'count') {
        location.href = url + '&sort_by=-count';
    } else {
        location.href = url + '&sort_by=count';
    }
    return false;
});
</script>
{% endblock %}
