{% extends "sysadmin/sys_org_info_base.html" %}
{% load i18n seahub_tags %}
{% load url from future %}

{% block right_panel %}
<div class="tabnav">
    <ul class="tabnav-tabs">
        <li class="tabnav-tab"><a href="{% url 'sys_org_info_user' org.org_id %}">{% trans "Members" %}</a></li>
        <li class="tabnav-tab"><a href="{% url 'sys_org_info_group' org.org_id %}">{% trans "Groups" %}</a></li>
        <li class="tabnav-tab"><a href="{% url 'sys_org_info_library' org.org_id %}">{% trans "Libraries" %}</a></li>
        <li class="tabnav-tab tabnav-tab-cur"><a href="{% url 'sys_org_info_setting' org.org_id %}">{% trans "Settings" %}</a></li>
    </ul>
</div>

<div class="setting-item">
    <h3>{% trans "Rename Organization" %}</h3>
    <form action="{% url 'sys_org_rename' org.org_id %}" method="post">{% csrf_token %}
        <label>{% trans "New Name" %}</label></br>
        <input type="text" name="new_name" class="input" value="{{ org.org_name }}"/></br>
        <input type="submit" value="{% trans "Submit" %}" class="submit" />
    </form>

    {% if org_member_quota %}
    <h3>{% trans "Limit Organization User Number" %}</h3>
    <form id="org_member_quota" action="" method="post" class="form">{% csrf_token %}
        <label>{% trans "Max User Number" %}</label></br>
        <input type="text" name="member_quota" class="input" value="{{ org_member_quota }}"/></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">
{% include 'sysadmin/sys_org_set_quota_js.html' %}

{% if org_member_quota %}
$('#org_member_quota').submit(function() {
    var form = $(this),
        form_id = form.attr('id'),
        member_quota = $('[name="member_quota"]', form).val(),
        submit_btn = $('input[type="submit"]', form);

    if (!$.trim(member_quota)) {
        return false;
    }

    if (isNaN(member_quota)) {
        apply_form_error(form_id, "{% trans "Input should be a number" %}");
        return false;
    } else if (member_quota <= 0) {
        apply_form_error(form_id, "{% trans "Input number should be greater than 0" %}");
        return false;
    }

    disable(submit_btn);
    $.ajax({
        url: '{% url 'sys_org_set_member_quota' org.org_id %}',
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: {
            'member_quota': member_quota
        },
        success: function(data) {
            if (data['success']) {
                location.href = '{% url 'sys_org_info_setting' org.org_id %}';
            }
        },
        error: ajaxErrorHandler
    });
    return false;
});
{% endif %}
</script>
{% endblock %}
