{% extends "base_wide_page.html" %}

{% load seahub_tags avatar_tags i18n %}


{% 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="#" 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 40 %}" 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 data-name="{{ dirent.obj_name }}">
                    <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 data-name="{{ dirent.props.obj_name }}">
                    <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').on('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() {
            var msg = "{% trans "Successfully restored {dirent_name}" %}".replace('{dirent_name}', _this.closest('tr').attr('data-name'));
            feedback(msg, 'success');
        },
        error: ajaxErrorHandler
    });
    return false;
});

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

{% include 'snippets/go_back_js.html' %}
</script>
{% endblock %}
