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

{% block nav_orgadmin_class %}class="cur"{% endblock %}

{% block extra_style %}
<style type="text/css">
#set-quota-form {
    min-width:255px;
}
</style>
{% endblock %}

{% block left_panel %}
<a class="go-back" title="{% trans "Back" %}" href="{% url 'sys_inst_admin' %}"><span class="icon-chevron-left"></span></a>
<div class="side-info">
    <h3 class="hd"><a href="{% url 'sys_inst_info_users' inst.pk %}">{{ inst.name }}</a></h3>
    <dl>
        <dt>{% trans "Number of members" %}</dt>
        <dd>{{ users_count }}</dd>

        <dt>{% trans "Space Used" %}</dt>
        <dd>{{ space_usage|seahub_filesizeformat }} {% if space_quota > 0 %} / {{ space_quota|seahub_filesizeformat }} {% endif %}  <a href="#" class="sf-btn-link" style="margin-left:20px;" id="set-quota">{% trans "Set Quota" %}</a></dd>
        
    </dl>

    <form id="set-quota-form" method="post" class="hide">{% csrf_token %}
        <h3>{% trans "Set storage limit" %}</h3>
        <input type="text" name="space_quota" /> MB
        <p class="tip">{% trans "Tip: 0 means default limit" %}</p>
        <p class="error hide"></p>
        <input type="submit" value="{% trans "Submit" %}" class="submit" />
    </form>

    <form action="{% url 'sys_inst_search_user' inst.pk %}" method="get" class="side-search-form">
        <input type="text" name="q" class="input" value="{{q}}" placeholder="{% trans "Search users..." %}" />
    </form>
    
</div>
{% endblock %}

{% block extra_script %}
<script type="text/javascript">
$('#set-quota').on('click', function() {
    $("#set-quota-form").modal({appendTo: "#main"});
    return false;
});

$('#set-quota-form').on('submit', function() {
    var form = $('#set-quota-form'),
        form_id = form.attr('id'),
        space_quota = $('input[name="space_quota"]', form).val();

    if (!$.trim(space_quota)) {
        apply_form_error(form_id, "{% trans "Space Quota can't be empty" %}");
        return false;
    }

    var sb_btn = $(this);
    disable(sb_btn);
    $.ajax({
        url: '{% url 'sys_inst_set_quota' inst.pk %}',
        type: 'POST',
        dataType: 'json',
        cache: false,
        beforeSend: prepareCSRFToken,
        data: {'space_quota': space_quota},
        success: function(data) {
            location.reload(true);
        },
        error: function(xhr, textStatus, errorThrown) {
            var error_msg = prepareAjaxErrorMsg(xhr);
            apply_form_error(form_id, error_msg);
            enable(sb_btn);
        }
    });
    return false;
});
</script>
{% endblock %}
