{% extends "myhome_base.html" %}

{% load i18n avatar_tags seahub_tags staticfiles %}

{% block sub_title %}{{repo.name}} - {% endblock %}

{% block extra_style %}
<link rel="stylesheet" type="text/css" href="{% static "css/select2-3.5.2.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "css/select2.custom.css" %}" />
{% endblock %}

{% block left_panel %}
<div class="side-textnav">
    <h3 class="hd"><span class="op-target">{{ repo.name }}</span> {% trans "Settings" %}</h3>
    <ul class="side-textnav-tabs">
        <li class="tab"><a href="{% url 'repo_basic_info' repo.id %}">{% trans "Basic Info" %}</a></li>
        <li class="tab tab-cur"><a href="{% url 'repo_transfer_owner' repo.id %}">{% trans "Transfer Ownership" %}</a></li>
        {% if repo.encrypted and repo.enc_version == 2 %}
        <li class="tab"><a href="{% url 'repo_change_password' repo.id %}">{% trans "Change Password" %}</a></li>
        {% endif %}
        {% if not repo.encrypted %}
        <li class="tab"><a href="{% url 'repo_shared_link' repo.id %}">{% trans "Shared Links" %}</a></li>
        {% endif %}
        <li class="tab"><a href="{% url 'repo_share_manage' repo.id %}">{% trans "Sharing Permission" %}</a></li>
        {% if ENABLE_FOLDER_PERM %}
        <li class="tab"><a href="{% url 'repo_folder_perm' repo.id %}">{% trans "Folder Permission" %}</a></li>
        {% endif %}
    </ul>
</div>
{% endblock %}

{% block right_panel %}
<div class="lib-setting">
    <h3 class="hd">{% trans "Transfer Ownership" %}</h3>
    <div id="transfer-ownership" class="setting-item">
        <form id="repo-owner-form" action="" method="post" class="form">{% csrf_token %}
            <p>{% trans "Transfer this library to another user:" %}</p>
            <input type="hidden" name="email" /><br />
            <p class="error hide"></p>
            <input type="submit" value="{% trans "Submit" %}" class="submit" />
        </form>
    </div>
</div>
{% endblock %}

{% block extra_script %}
<script type="text/javascript" src="{% static "scripts/lib/select2-3.5.2.js" %}"></script>
<script type="text/javascript">
$('[name="email"]', $('#repo-owner-form')).select2($.extend({
    width: '268px',
    maximumSelectionSize: 1,
    placeholder: "{% trans "Search user or enter email" %}",
    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' %}')));

$('#repo-owner-form').submit(function() {
    var form = $(this),
        form_id = form.attr('id'),
        new_owner = $('[name="email"]', form).val(),
        submit_btn = $('input[type="submit"]', form);

    if (!$.trim(new_owner)) {
        return false;
    }

    disable(submit_btn);
    $.ajax({
        url: '{% url 'ajax_repo_transfer_owner' repo.id %}',
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: {
            'repo_owner': new_owner
        },
        success: function(data) {
            location.href = '{% url 'repo_transfer_success' repo.id %}';
        },
        error: function(jqXHR, textStatus, errorThrown) {
            if (jqXHR.responseText) {
                apply_form_error(form_id, $.parseJSON(jqXHR.responseText).error);
            } else {
                apply_form_error(form_id, "{% trans "Failed. Please check the network." %}");
            }
            enable(submit_btn);
        }
    });
    return false;
});
</script>
{% endblock %}
