{% extends "myhome_base.html" %}

{% load i18n avatar_tags seahub_tags %}

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

{% block extra_style %}
<style type="text/css">
.side-textnav {
    margin-top:10px;
}
</style>
{% 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>
<div class="side-textnav">
    <ul class="side-textnav-tabs">
        <li class="tab tab-cur"><a href="#basic-info">{% trans "Basic Info" %}</a></li>
        <li class="tab"><a href="#transfer-ownership">{% trans "Transfer Ownership" %}</a></li>
        {% if repo.encrypted and repo.enc_version == 2 %}
        <li class="tab"><a href="#change-password">{% trans "Change Password" %}</a></li>
        {% endif %}
    </ul>
</div>
{% endblock %}

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

    <div id="basic-info" class="setting-item">
        <h3>{% trans "Basic Info" %}</h3>
        <form id="repo-basic-info-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>
    </div>

    <div id="transfer-ownership" class="setting-item hide">
        <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>
    </div>

    {% if repo.encrypted and repo.enc_version == 2 %}
    <div id="change-password" class="setting-item hide">
        <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>
    </div>
    {% endif %}

</div>
{% endblock %}

{% block extra_script %}
<script type="text/javascript">
$('#repo-basic-info-form input[name="history"]').change(function() {
    var value = $(this).attr('value'),
        days_input = $('#repo-basic-info-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-basic-info-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_change_basic_info' 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_transfer_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 %}
$('#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 '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 %}

$('.side-textnav-tabs .tab').click(function() {
    var cur_tab = $(this),
        cur_tab_con = $($('a', cur_tab).attr('href'));

    $('.side-textnav-tabs .tab').removeClass('tab-cur');
    $('.lib-setting .setting-item').addClass('hide');
    cur_tab.addClass('tab-cur');
    cur_tab_con.removeClass('hide')
    return false;
});
</script>
{% endblock %}
