{% load i18n%}

$('.repo-transfer-btn').on('click', function(){
    var btn_ct = $(this).parents('td'),
        $tr = $(this).closest('tr'),
        repo_id = btn_ct.data('id'),
        repo_name = btn_ct.attr('data-name'),
        form = $('#repo-transfer-form');

    form.modal({appendTo:'#main'});
    $('#simplemodal-container').css({'width':'auto', 'height':'auto'});

    var detail = $('.detail', form);
    detail.html(detail.html().replace('%(lib_name)s', '<span class="op-target">' + HTMLescape(repo_name) + '</span>'));

    $('[name="email"]', form).select2($.extend({
        width: '268px',
        maximumSelectionSize: 1,
        placeholder: "{% trans "Search user or enter email and press Enter" %}",
        formatInputTooShort: "{% trans "Please enter 1 or more character" %}",
        formatNoMatches: "{% trans "No matches" %}",
        formatSearching: "{% trans "Searching..." %}",
        formatAjaxError: "{% trans "Loading failed" %}",
        formatSelectionTooBig: "{% trans "You cannot select any more choices" %}"
    }, userInputOPtionsForSelect2('{% url 'search-user' %}')));

    $('input[name="repo_id"]', form).val(repo_id);
    form.data('tr', $tr);
    return false;
});

$('#repo-transfer-form').on('submit', function() {
    var form = $(this),
        form_id = form.attr('id'),
        repo_id = $('[name="repo_id"]', form).val(),
        email = $.trim(form.children('[name="email"]').val());
    var $tr = form.data('tr');

    if (!email) {
        return false;
    }

    $.ajax({
        url: '{{SITE_ROOT}}api/v2.1/admin/libraries/' + encodeURIComponent(repo_id) + '/',
        data: {'owner': email},
        type: 'PUT',
        cache: false,
        beforeSend: prepareCSRFToken,
        dataType: 'json',
        success: function() {
            $tr.remove();
            feedback("{% trans "Successfully transfered 1 item." %}", 'success');
        },
        error: ajaxErrorHandler,
        complete: function() {
            $.modal.close();
        }
    });

    return false;
});

$('#main-panel').removeClass('ovhd');

$('.repo-delete-btn').on('click', function() {
    var $tr = $(this).closest('tr');
    var $td = $(this).closest('td');
    var repo_id = $td.data('id');
    var repo_name = $td.data('name');
    var popupTitle = "{% trans "Delete Library" %}";
    var popupContent = "{% trans "Are you sure you want to delete %s ?" %}".replace('%s', '<span class="op-target ellipsis ellipsis-op-target" title="' + HTMLescape(repo_name) + '">' + HTMLescape(repo_name) + '</span>');
    var yesCallback = function() {
        $.ajax({
            url: '{{SITE_ROOT}}api/v2.1/admin/libraries/' + encodeURIComponent(repo_id) + '/',
            type: 'DELETE',
            cache: false,
            beforeSend: prepareCSRFToken,
            dataType: 'json',
            success: function() {
                $tr.remove();
                feedback("{% trans "Successfully deleted 1 item." %}", 'success');
            },
            error: ajaxErrorHandler,
            complete: function() {
                $.modal.close();
            }
        });
    };
    showConfirm(popupTitle, popupContent, yesCallback);
    return false;
});
