{% extends "myhome_base.html" %}

{% load i18n avatar_tags seahub_tags %}

{% block sub_title %}{{repo.name}} - {% 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"><a href="{% url 'repo_transfer_owner' repo.id %}">{% trans "Transfer Ownership" %}</a></li>
        {% if repo.encrypted and repo.enc_version == 2 %}
        <li class="tab tab-cur"><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 "Change Password" %}</h3>
    <div id="change-password" class="setting-item">
        <form id="repo-change-passwd-form" action="" method="post" class="form">{% csrf_token %}
            <p>{% trans "Change the password of this library:" %}</p>
            <label>{% trans "Old Password" %}</label><br />
            <input type="password" name="old_passwd" class="input" /><br />
            <label>{% blocktrans %}New Password(at least {{repo_password_min_length}} characters){% endblocktrans %}</label><br />
            <input type="password" name="new_passwd" class="input" /><br />
            <label>{% trans "New Password Again" %}</label><br />
            <input type="password" name="new_passwd_again" class="input" /><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">
{% if repo.encrypted and repo.enc_version == 2 %}
$('#repo-change-passwd-form').submit(function() {
    var form = $(this),
        form_id = form.attr('id'),
        old_passwd, new_passwd, new_passwd_again;

    old_passwd = $('input[name="old_passwd"]', form).val();
    new_passwd = $('input[name="new_passwd"]', form).val();
    new_passwd_again = $('input[name="new_passwd_again"]', form).val();

    if (!$.trim(old_passwd)) {
        apply_form_error(form_id, "{% trans "Please enter the old password" %}");
        return false;
    }
    if (!$.trim(new_passwd)) {
        apply_form_error(form_id, "{% trans "Please enter the new password" %}");
        return false;
    }
    if ($.trim(new_passwd).length < {{repo_password_min_length}}) {
        apply_form_error(form_id, "{% trans "New password is too short" %}");
        return false;
    }
    if (!$.trim(new_passwd_again)) {
        apply_form_error(form_id, "{% trans "Please enter the new password again" %}");
        return false;
    }
    if ($.trim(new_passwd) != $.trim(new_passwd_again)) {
        apply_form_error(form_id, "{% trans "New passwords don't match" %}");
        return false;
    }

    var submit_btn = $(this).children('input[type="submit"]');
    disable(submit_btn);

    $.ajax({
        url: '{% url 'ajax_repo_change_passwd' repo.id %}',
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: {
            'old_passwd': old_passwd,
            'new_passwd': new_passwd,
            'new_passwd_again': new_passwd_again
        },
        success: function(data) {
            if (data['success']) {
                location.reload(true);
            }
        },
        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;
});
{% endif %}
</script>
{% endblock %}
