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

{% block cur_users %}tab-cur{% endblock %}

{% block left_panel %}{{block.super}}
<form action="{% url 'user_search' %}" method="get" class="side-search-form">
    <input type="text" name="email" class="input" value="" placeholder="{% trans "Search users..." %}" />
</form>
{% endblock %}

{% block right_panel %}
<div class="tabnav ovhd">
    <ul class="tabnav-tabs fleft">
        <li class="tabnav-tab"><a href="{% url 'sys_useradmin' %}">{% trans "Database" %}</a></li>
        {% if have_ldap %}
        <li class="tabnav-tab"><a href="{% url 'sys_useradmin_ldap' %}">{% trans "LDAP" %}</a></li>
        <li class="tabnav-tab"><a href="{% url 'sys_useradmin_ldap_imported' %}">{% trans "LDAP(imported)" %}</a></li>
        {% endif %}
        <li class="tabnav-tab tabnav-tab-cur"><a href="{% url 'sys_useradmin_admins' %}">{% trans "Admins" %}</a></li>
    </ul>

    <div class="js-op-for-all fright">
        <button id="add-admin-btn">{% trans "Add admin" %}</button>
    </div>
    <div class="js-op-for-selected fright hide">
        <button id="set-quota-btn">{% trans "Set quota" %}</button>
        <button id="delete-users-btn">{% trans "Delete users" %}</button>
    </div>
</div>

<form id="add-admin-form"  method="post" name="add-admin-form" class="tab-popup hide">{% csrf_token %}
    <h3 class="hd">{% trans "Add admins"%}</h3>
    <div id="add-admin-tabs" class="tab-popup-tabs">
        <ul class="tab-popup-tabs-nav">
            <li class="tab"><a href="#enter" class="a">{% trans 'Enter' %}</a></li>
        </ul>
        <div id="enter">
            <textarea id="added-member-name" name="user_email" placeholder="{% trans "Emails, separated by ','" %}"></textarea><br />
            <p class="tip">{% trans "Tip: the emails should be the users already added."%}</p>
        </div>
    </div>
    <div class="bot">
        <p class="error hide"></p>
        <input type="submit" value="{% trans "Submit"%}" />
    </div>
</form>

{% with is_admin_page=True%}
{% include "sysadmin/useradmin_table.html" %}
{% endwith %}

<div id="activate-msg" class="hide">
    <p>{% trans "Activating..., please wait" %}</p>
</div>
{% endblock %}

{% block extra_script %}
<script type="text/javascript" src="{% static "scripts/lib/jquery-ui.min.js" %}"></script> {# for tabs #}
<script type="text/javascript">
$('#add-admin-btn').on('click', function() {
    var form = $("#add-admin-form");
    form.modal({appendTo: "#main", focus:false});
    $('#simplemodal-container').css({'height':'auto', 'padding':0});
    $('#add-admin-tabs').tabs();
});
$('#add-admin-form').on('submit', function() {
    var $form = $(this),
        emails = $.trim($('[name="user_email"]', $form).val());

    if (!emails) {
        return false;
    }

    var $submitBtn = $('[type="submit"]', $form);
    disable($submitBtn);
    $.ajax({
        url: '{% url 'batch_user_make_admin' %}',
        type: 'POST',
        dataType: 'json',
        cache: false,
        beforeSend: prepareCSRFToken,
        data: {
            'set_admin_emails': emails
        },
        success: function() {
            location.reload(true);
        },
        error: function(xhr) {
            var error_msg = prepareAjaxErrorMsg(xhr);
            $('.error', $form).html(error_msg).removeClass('hide');
            enable($submitBtn);
        }
    });

    return false;
});
$('.admin-role-select').on('change', function() {
    var $select = $(this),
        role = $select.val(),
        email = $select.parents('tr').attr('data-userid'),
        $select_prev = $(this).prev('.admin-role'),
        url = "{% url 'api-v2.1-admin-admin-role' %}",
        data = {'email': email, 'role': role};

    $.ajax({
        url: url,
        type: 'PUT',
        dataType: 'json',
        data: data,
        cache: false,
        beforeSend: prepareCSRFToken,
        success: function(data) {
            feedback("{% trans "Edit succeeded" %}", 'success');
            $('.admin-role-cur-value', $select_prev).html($select.children('option[value="' + $select.val() + '"]').text());
            $select.addClass('hide');
            $select_prev.removeClass('hide');
            $.modal.close();
        },
        error: function() {
            feedback("{% trans "Edit failed." %}", 'error');
            $select.addClass('hide');
            $select_prev.removeClass('hide');
            $.modal.close();
        }
    });
});

{% include "sysadmin/useradmin_js.html" %}
</script>
{% endblock %}
