{% 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-trash-hd">{% blocktrans %}<span class="op-target">{{repo_dir_name}}</span> Trash{% endblocktrans %}</h2>
{% if referer %}
<a href="#" 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 show_recycle_root %}
<div id="trash-more">
    <div id="trash-more-loading" class="hide"><span class="loading-icon"></span></div>
    <button id="trash-more-btn" class="full-width-btn hide">{% trans 'More' %}</button>
</div>
{% endif %}

{% if enable_clean %}
<form id="gc-form" class="hide" action="">
    <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>
    <p class="error hide"></p>
    <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').on('click', function() {
    $('#gc-form').modal({focus:false});
    $("#simplemodal-container").css({'height':'auto'});
});
$('#gc-form').on('submit', function(){
    var $form = $(this);
    var $submit = $('[type="submit"]', $form);
    var $select = $('[name="day"]', $form);
    var $error = $('.error', $form);
    var value = $('option:selected', $select).val();
    disable($submit);
    $.ajax({
        url: '{% url "api-v2.1-repo-trash" repo.id %}',
        type: 'DELETE',
        cache: false,
        dataType: 'json',
        data: {
            'keep_days':  value
        },
        beforeSend: prepareCSRFToken,
        success: function(data) {
            location.reload();
        },
        error: function(xhr) {
            var error_msg = prepareAjaxErrorMsg(xhr);
            $error.html(error_msg).show();
            enable($submit);
        }
    });
    return false;
})
{% endif %}

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

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

    var data = {
        'path': dir_path,
        'referer': '{{referer|escapejs}}' // for 'back'
    }; // for the first request
    if (scan_stat != undefined) {
        $.extend(data, {'scan_stat': scan_stat});
    }

    $.ajax({
        url:'{% url "ajax_repo_dir_recycle_more" repo.id %}',
        data: data,
        cache: false,
        dataType: 'json',
        success: function(data) {
            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');
                }
            } else if (scan_stat) {
                // no trash dir or file
                // have not scan all commit
                get_more_trash();
            } 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_msg = prepareAjaxErrorMsg(xhr);
            feedback(error_msg, 'error');
        }
    });
};

// get 'first' batch of trash
get_more_trash();

$('#trash-more-btn').on('click', function() {
    get_more_trash();
});

$('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: ajaxErrorHandler
    });
    return false;
});
{% endif %}

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