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


{% block right_panel %}
<div class="tabnav">
    {% with cur_tab='setting' %}
    {% include 'sysadmin/snippets/sys_org_info_nav.html' %}
    {% endwith %}
</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').on('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 %}
