{% extends "myhome_base.html" %}

{% load i18n %}

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

{% block left_panel %}
<a href="{% url 'repo' repo.id %}" title="{% trans "Back to Library" %}"><span class="icon-chevron-left"></span> {% trans "Back to Library" %}</a>
{% endblock %}

{% block right_panel %}
<div class="lib-setting">
<h2>{% trans "Library Settings" %}</h2>

<h3>{% trans "Basic Info" %}</h3>
<form id="repo-setting-form" action="" method="post" class="form">{% csrf_token %}
    <label>{% trans "Name" %}</label><br />
    <input type="text" name="repo_name" value="{{ repo.name }}" class="input" /><br />
    <label>{% trans "Description" %}</label></br />
    <textarea name="repo_desc" class="textarea">{{ repo.desc }}</textarea><br />

    {% if not ENABLE_SUB_LIBRARY or not repo.is_virtual %}
    <label>{% trans "History" %}</label><br />
    <input type="radio" name="history" value="full_history" {% if full_history_checked %}checked="checked"{% endif %} class="vam" {% if not full_history_enabled %}disabled="disabled"{% endif %} /> <span class="vam">{% trans "Keep full history" %}</span><br />
    <input type="radio" name="history" value="no_history" {% if no_history_checked %}checked="checked"{% endif %} class="vam" {% if not full_history_enabled %}disabled="disabled"{% endif %} /> <span class="vam">{% trans "Don't keep history" %}</span><br />
    <input type="radio" name="history" value="partial_history" {% if partial_history_checked %}checked="checked"{% endif %} class="vam" {% if not full_history_enabled %}disabled="disabled"{% endif %} /> <span calss="vam">{% trans "Only keep a period of history:" %}
    <input type="text" name="days" size="4" {% if not days_enabled %} disabled="disabled" class="input-disabled"{% endif %} value="{{history_limit}}" /> {% trans "days" %}</span><br />
    {% endif %}
    
    <p class="error hide"></p>
    <input type="submit" value="{% trans "Submit" %}" class="submit" />
</form>

<h3>{% trans "Transfer Ownership" %}</h3>
<form id="repo-owner-form" action="" method="post" class="form">{% csrf_token %}
    <p>{% trans "Transfer this library to another user:" %}</p>
    <input type="text" name="repo_owner" value="" placeholder="{% trans "Email" %}" class="input" /><br />
    <p class="error hide"></p>
    <input type="submit" value="{% trans "Submit" %}" class="submit" />
</form>

{% if repo.encrypted and repo.enc_version == 2 and not server_crypto %}
<h3>{% trans "Change Password" %}</h3>
<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>
{% endif %}
</div>
{% endblock %}

{% block extra_script %}
<script type="text/javascript">
$('#repo-setting-form input[name="history"]').change(function() {
    var value = $(this).attr('value'),
        days_input = $('#repo-setting-form input[name="days"]');
    if (value == 'full_history' || value == 'no_history') {
        days_input.attr('disabled', true).addClass('input-disabled');
    } else {
        days_input.attr('disabled', false).removeClass('input-disabled');
    }
});

$('#repo-setting-form').submit(function() {
    var form = $(this),
        form_id = form.attr('id');        
    var repo_name = $('[name="repo_name"]', form).val(),
        repo_desc = $('[name="repo_desc"]', form).val();

    if (!$.trim(repo_name)) {
        apply_form_error(form_id, "{% trans "Name is required." %}");
        return false;
    }
    if (!$.trim(repo_desc)) {
        apply_form_error(form_id, "{% trans "Description is required." %}");
        return false;
    }

    var days;
    var value = $(this).find('input[name="history"]:checked').val();

    if (value == 'partial_history') {
        days = $(this).find('input[name="days"]').val();
    } else if (value == 'full_history') {
        days = -1;
    } else {
        days = 0;
    }

    var submit_btn = $(this).children('input[type="submit"]');  
    disable(submit_btn);
    $.ajax({
        url: '{% url 'repo_settings' repo.id %}',
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: {
            'repo_name': repo_name, 
            'repo_desc': repo_desc,
            'days': days 
        },
        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;
});

$('#repo-owner-form').submit(function() {
    var form = $(this),
        form_id = form.attr('id'),
        new_owner = $('[name="repo_owner"]', form).val(),
        submit_btn = $('input[type="submit"]', form);  
    
    if (!$.trim(new_owner)) {
        return false;
    }

    disable(submit_btn);
    $.ajax({
        url: '{% url 'repo_owner' repo.id %}',
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: {
            'repo_owner': new_owner
        },
        success: function(data) {
            if (data['success']) {
                location.href = '{% url 'myhome' %}';
            }
        },
        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;
});

{% if repo.encrypted and repo.enc_version == 2 and not server_crypto %}
$('#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).length > 35) {
        apply_form_error(form_id, "{% trans "New password is too long (maximum is 35 characters)" %}");
        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 '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 %}
