{% extends "base_wide_page.html" %}

{% load seahub_tags avatar_tags i18n %}
{% load url from future %}

{% block extra_style %}
<style type="text/css">
.go-back { top:-3px; }
</style>
{% endblock %}

{% block wide_page_content %}
<h2 class="repo-snapshot-hd">{% blocktrans with repo_name=repo.props.name %}<span class="op-target">{{repo_name}}</span> Snapshot{% endblocktrans %} <span class="commit-time">({{ current_commit.props.ctime|tsstr_sec }})</span></h2>

{% if referer %}
<a href="{{referer}}" class="go-back" title="{% trans "Back" %}">
    <span class="icon-chevron-left"></span>
</a>
{% endif %}

            {% if path == '/' %}
            <div id="repo-latest-commit" class="repo-snapshot-commit">
                <span class="commit-msg">{{ current_commit.props.desc|translate_commit_desc }}</span>
                <span class="meta-info split">
                    <span class="author">
                        {% if current_commit.props.creator_name %}
                        <img src="{% avatar_url current_commit.props.creator_name 20 %}" width="20" class="avatar" />
                        <a class="name" href="{% url 'user_profile' current_commit.props.creator_name %}">{{ current_commit.props.creator_name|email2nickname }}</a>
                        {% else %}
                        {% trans "Unknown"%}
                        {% endif %}
                    </span>
                    <span class="time">{{ current_commit.props.ctime|translate_seahub_time }}</span>
                </span>
            </div>
            {% endif %}

            <div class="repo-file-list-topbar ovhd">
                <p class="path fleft">
                {% trans "Current path: "%}
                {% for name, link in zipped %}
                {% if not forloop.last %}
                <a href="{% url 'repo_history_view' repo.id %}?commit_id={{ current_commit.id }}&p={{ link|urlencode }}" class="repo-history-view-link">{{ name }}</a> /
                {% else %}
                {{ name }}
                {% endif %}
                {% endfor %}
                </p>

                {% if path == '/' and is_repo_owner %}
                    <button data-url="{% url 'repo_revert_history' repo.id %}?commit_id={{ current_commit.id }}" id="repo-revert" class="op-btn fright">{% trans "Restore"%}</button>
                {% endif %}
            </div>
            <!-- /.repo-file-list-topbar -->
            <table>
                <tr>
                    <th width="5%"></th>
                    <th width="45%">{% trans "Name"%}</th>
                    <th width="23%">{% trans "Size"%}</th>
                    <th width="27%">{% trans "Operations"%}</th>
                </tr>

                {% for dirent in dir_list %}
                <tr>
                    <td class="alc"><img src="{{ MEDIA_URL }}img/folder-24.png" alt="{% trans "Directory"%}" /></td>
                    <td><a href="{% url 'repo_history_view' repo.id %}?commit_id={{ current_commit.id }}&p={{ path|urlencode }}{{ dirent.obj_name|urlencode }}" class="repo-history-view-link">{{ dirent.obj_name }}</a></td>
                    <td></td>
                    <td><a class="op vh restore-dir" data-commit_id="{{ current_commit.id }}" data-commit_path="{{ path }}{{ dirent.obj_name }}" href="#">{% trans "Restore" %}</a></td>
                </tr>
                {% endfor %}

                {% for dirent in file_list %}
                <tr>
                    <td class="alc"><img src="{{ MEDIA_URL }}img/file/{{ dirent.obj_name|file_icon_filter }}" alt="{% trans "File"%}" /></td>
                    <td><a class="normal" href="{% url 'view_snapshot_file' repo.props.id %}?obj_id={{ dirent.props.obj_id }}&commit_id={{ current_commit.id }}&p={{ path|urlencode }}{{ dirent.obj_name|urlencode }}" target="_blank">{{ dirent.props.obj_name }}</a></td>
                    <td>{{ dirent.file_size|filesizeformat }}</td>
                    <td>
                        <a class="op vh restore-file" data-commit_id="{{ current_commit.id }}" data-commit_path="{{ path }}{{ dirent.obj_name }}" href="#">{% trans "Restore" %}</a>
                        <a class="op vh" href="{% url 'download_file' repo.id dirent.obj_id%}?file_name={{ dirent.obj_name|urlencode }}&p={{path|urlencode}}{{ dirent.obj_name|urlencode }}">{% trans "Download" %}</a>
                    </td>
                </tr>
                {% endfor %}
            </table>
    {% endblock %}

{% block extra_script %}
<script type="text/javascript">
addConfirmTo($('#repo-revert'), {
    'title':"{% trans "Restore Library" %}",
    'con':"{% trans "Are you sure you want to restore this library?" %}",
    'post':true
});

$('.restore-file, .restore-dir').click(function() {
    var _this = $(this),
        commit_id = _this.attr('data-commit_id'),
        path = _this.attr('data-commit_path'),
        url;

    if (_this.hasClass('restore-file')) {
        url = "{% url 'api-v2.1-file-view' repo.id %}" + "?p=" + encodeURIComponent(path);
    } else {
        url = "{% url 'api-v2.1-dir-view' repo.id %}" + "?p=" + encodeURIComponent(path);
    }

    $.ajax({
        url: url,
        type: 'POST',
        data: {'operation': 'revert', 'commit_id': commit_id},
        cache: false,
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        success: function() {
            _this.closest('tr').remove();
            feedback("{% trans "Success" %}", 'success');
        },
        error: ajaxErrorHandler
    });
    return false;
});

$('.repo-history-view-link').each(function() {
    $(this).attr('href', $(this).attr('href') + '&referer=' + encodeURIComponent(location.href));
});
</script>
{% endblock %}
