{% extends "admin_base.html" %}
{% load seahub_tags i18n %}
{% block nav_useradmin_class %}class="cur"{% endblock %}

{% block title_panel %}
<div class="tabnav">
    <ul class="tabnav-tabs fleft">
        <li class="tabnav-tab tabnav-tab-cur">{% trans "Database" %}</li>
        {% if have_ldap %}
        <li class="tabnav-tab"><a href="{% url 'sys_ldap_useradmin' %}">{% trans "LDAP" %}</a></li>
        {% endif %}
    </ul>
    <button class="icon-search fright" id="search-user-btn"> {% trans "Search" %}</button>    
</div>

{% endblock %}

{% block main_panel %}

<button id="add-user-btn"><span class="add icon-plus-sign-alt"></span>{% trans "Add new user" %}</button>

<form id="add-user-form" action="" method="post" class="hide">{% csrf_token %}
    <h3>{% trans "Add new user" %}</h3>
    <label for="id_email">{% trans "Email" %}</label><br />
    <input type="text" name="email" id="id_email" class="input" /><br />    
    <label for="id_password1">{% trans "Password" %}</label><br />
    <input type="password" name="password1" id="id_password1" class="input" /><br />    
    <label for="id_password2">{% trans "Confirm Password" %}</label><br />
    <input type="password" name="password2" id="id_password2" class="input" /><br />    
    <p class="error hide"></p>
    <input type="submit" value="{% trans "Submit" %}" class="submit" />
</form>

{% include "sysadmin/useradmin_table.html"%}
{% include "sysadmin/useradmin_paginator.html" %}

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

{% block extra_script %}
<script type="text/javascript">
$('#search-user-btn').click(function() {
    location.href = "{% url 'user_search' %}";
});

$('#add-user-form').submit(function() {
    var form = $(this),
        form_id = $(this).attr('id'),
        email = $.trim(form.children('[name="email"]').val()),
        pwd1 = $.trim(form.children('[name="password1"]').val()),
        pwd2 = $.trim(form.children('[name="password2"]').val());

    if (!email) {
        apply_form_error(form_id, "{% trans "Email cannot be blank" %}");
        return false;
    }
    if (!pwd1) {
        apply_form_error(form_id, "{% trans "Password cannot be blank" %}");
        return false;
    } 
    if (!pwd2) {
        apply_form_error(form_id, "{% trans "Please enter the password again" %}");
        return false;
    } 
    if (pwd1 != pwd2) {
        apply_form_error(form_id, "{% trans "Passwords do not match" %}");
        return false;
    } 

    var submit_btn = $(this).find('input[type="submit"]');
    disable(submit_btn);
    $.ajax({
        url: '{% url 'user_add' %}',
        type: 'POST',
        datatype: 'json',
        cache: 'false',
        beforeSend: prepareCSRFToken,
        data: {
            'email': email,
            'password1': pwd1,
            'password2': pwd2
        },
        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).err);
            } else {
                apply_form_error(form_id, "{% trans "Failed. Please check the network." %}");
            }
            enable(submit_btn);
        }
    });
    return false;
});

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