{% extends "myhome_base.html" %}

{% load i18n %}

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

{% block left_panel %}
{% endblock %}

{% block right_panel %}
<h2>{% trans "Library Settings" %}</h2>

<h3>{% trans "Basic Info" %}</h3>
<form id="repo-setting-form" action="" method="post" style="margin-bottom:30px;">{% 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 history_limit < 0 %}checked="checked"{% endif %} class="vam" /> <span class="vam">{% trans "Keep full history" %}</span><br />
    <input type="radio" name="history" value="no_history" {% if history_limit == 0 %}checked="checked"{% endif %} class="vam" /> <span class="vam">{% trans "Don't keep history" %}</span><br />
    <input type="radio" name="history" value="partial_history" {% if history_limit > 0 %}checked="checked"{% endif %} class="vam" /> <span calss="vam">{% trans "Only keep a period of history:" %} <input type="text" name="days" size="4" {% if history_limit <= 0 %} disabled="disabled" class="input-disabled" {% else %} value="{{history_limit}}" {% endif %} /> {% 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">{% 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>
{% 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;
});
</script>
{% endblock %}
