{% 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-trash-hd">{% blocktrans %}<span class="op-target">{{repo_dir_name}}</span> Trash{% endblocktrans %}</h2>
{% if referer %}
<a href="{{referer}}" class="go-back" title="{% trans "Back" %}">
    <span class="icon-chevron-left"></span>
</a>
{% endif %}
<div class="repo-file-list-topbar ovhd">
    <p class="path fleft">
    {% trans "Current path: " %}
    {% if not show_recycle_root %}
    <a href="?dir_path={{dir_path|urlencode}}&referer={{referer|urlencode}}">{{repo_dir_name}}</a>
    {% for name, link in zipped %}
        {% if not forloop.last %}
        / <a href="?commit_id={{ commit_id }}&base={{ basedir|urlencode }}&p={{ link|urlencode }}&dir_path={{dir_path|urlencode}}&referer={{referer|urlencode}}">{{ name }}</a>
        {% else %}
        / {{ name }}
        {% endif %}
    {% endfor %}
    {% else %}
    {{repo_dir_name}}
    {% endif %}
    </p>

    {% if enable_clean %}
    <button id="online-gc" class="op-btn fright">{% trans "Clean" %}</button>
    {% endif %}
</div>
<table>
    <thead>
        <tr>
            <th width="5%"></th>
            <th width="45%">{% trans "Name" %}</th>
            <th width="20%">{% trans "Delete Time" %}</th>
            <th width="15%">{% trans "Size" %}</th>
            <th width="15%">{% trans "Operations" %}</th>
        </tr>
    </thead>

    <tbody>
    {% include "snippets/repo_dir_trash_tr.html" %}
    </tbody>
</table>

{% if trash_more %}
<div id="trash-more">
    <div id="trash-more-loading" class="hide"><span class="loading-icon"></span></div>
    <button id="trash-more-btn">{% trans 'More' %}</button>
</div>
{% endif %}

{% if enable_clean %}
<form id="gc-form" class="hide" method="post" action="{% url "repo_online_gc" repo.id %}">{% csrf_token %}
    <h3 id="dialogTitle">{% trans "Clean" %}</h3>
    <p>{% trans "Clear files in trash and history：" %}</p>
    <select name="day" class="w100" style="margin-bottom:5px;">
        <option value="3">{% trans "3 days ago" %}</option>
        <option value="7">{% trans "1 week ago" %}</option>
        <option value="30">{% trans "1 month ago" %}</option>
        <option value="0">{% trans "all" %}</option>
    </select>
    <button type="submit" class="submit">{% trans "Submit" %}</button>
    <button type="button" class="simplemodal-close">{% trans "Cancel" %}</button>
</form>
{% endif %}
{% endblock %}

{% block extra_script %}
<script type="text/javascript">
$('#main-panel').removeClass('ovhd');

{% if enable_clean %}
$('#online-gc').click(function() {
    $('#gc-form').modal({focus:false});
    $("#simplemodal-container").css({'height':'auto'});
});
{% endif %}

var dir_path = '{{dir_path|escapejs}}',
    scan_stat = '{{scan_stat|escapejs}}',
    $filesContainer = $('tbody'),
    $trash_more_btn = $('#trash-more-btn'),
    $trash_more_loading = $('#trash-more-loading');

var get_more_trash = function(current_scan_stat) {
    $trash_more_btn.addClass('hide');
    $trash_more_loading.removeClass('hide');

    $.ajax({
        url:'{% url "ajax_repo_dir_recycle_more" repo.id %}' + '?scan_stat=' + encodeURIComponent(current_scan_stat) + '&path=' + encodeURIComponent(dir_path),
        dataType: 'json',
        cache: false,
        success: function(data) {
            var new_scan_stat = data['new_scan_stat'];

            if (data['html']) {
                // have trash dir or file
                $filesContainer.append(data['html']);
                $trash_more_loading.addClass('hide');

                if (data['trash_more']) {
                    $trash_more_btn.removeClass('hide');
                }
                scan_stat = new_scan_stat;
            } else if (new_scan_stat) {
                // no trash dir or file
                // have not scan all commit
                get_more_trash(new_scan_stat);
            } else {
                // no trash dir or file
                // scan all commit
                $trash_more_loading.addClass('hide');
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            $trash_more_loading.addClass('hide');
            var error;
            if (xhr.responseText) {
                error = $.parseJSON(xhr.responseText).error;
            } else {
                error = "{% trans "Failed. Please check the network." %}";
            }
            feedback(error, 'error');
        }
    });
}


$('table').on("click", ".restore-file, .restore-dir", function() {
    var _this = $(this),
        commit_id = _this.data('commit_id'),
        path = _this.data('path');

    $.ajax({
        url: _this.data('url'),
        type: 'PUT',
        dataType: 'json',
        cache: false,
        beforeSend: prepareCSRFToken,
        data: {'commit_id': commit_id, 'p': path},
        success: function(data) {
            _this.closest('tr').remove();
            feedback("{% trans "Successfully restored 1 item." %}", 'success');
        },
        error: function ajaxErrorHandler(xhr, textStatus, errorThrown) {
            if (xhr.responseText) {
                feedback($.parseJSON(xhr.responseText).error_msg, 'error');
            } else {
                feedback("{% trans "Failed. Please check the network." %}", 'error');
            }
        }
    });
    return false;
});

// has 'scan_stat' means have not scanned all commit
// 'not dir_entries' means no trash returned
// so continue scan by send ajax
{% if scan_stat and not dir_entries %}
get_more_trash(scan_stat);
{% endif %}

{% if trash_more %}
$('#trash-more-btn').click(function() {
    get_more_trash(scan_stat);
});
{% endif %}

</script>
{% endblock %}
