{% load i18n %}
{% load url from future %}
$('#repo-decrypt-form').submit(function() {
    var form = $(this);
    var form_id = form.attr('id');
    var input_password = form.find('input[name="password"]');
    var repo_id = form.find('input[name="repo_id"]').val();
    var password = input_password.val();
    var cur_data = form.data();
    if (!password) {
        apply_form_error(form_id, "{% trans "Password is required." %}");
    } else {
        apply_form_error(form_id, '');
        $.ajax({
            url: '{% url 'repo_set_password' %}',
            type: 'POST',
            dataType: 'json',
            cache: 'false',
            beforeSend: prepareCSRFToken,
            data: {
                repo_id: repo_id,
                password: password,
                username: '{{request.user.username}}'
            },
            success: function(data) {
                $.modal.close();
                $('.lsch-encrypted[data-repoid="' + repo_id + '"]').attr('data-passwordset', true);
                listCommitDetails(cur_data.url, cur_data.time);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                var err = '';
                if (jqXHR.responseText) {
                    err = $.parseJSON(jqXHR.responseText).error;
                } else {
                    err = "{% trans "Failed. Please check the network." %}";
                }
                apply_form_error(form_id, err);
                input_password.val('');
            }
        });
    }
    return false;
});
