{% extends "sysadmin/base.html" %}
{% load seahub_tags i18n %}
{% block cur_users %}tab-cur{% endblock %}
{% block extra_style %}
<style type="text/css">
#upload-file-btn .icon-upload-alt {
    color:#777;
    margin-right:3px;
}
</style>
{% 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 tabnav-tab-cur"><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>
        {% endif %}
        <li class="tabnav-tab"><a href="{% url 'sys_useradmin_admins' %}">{% trans "Admins" %}</a></li>
    </ul>
    <div class="fright">
        <button id="upload-file-btn"><span class="icon-upload-alt"></span>{% trans "Import users" %}</button>
        <button id="add-user-btn"><img src="{{ MEDIA_URL }}img/add.png" alt="" class="add vam" /><span class="vam">{% trans "Add user" %}</span></button>
    </div>
</div>

<form id="add-user-form" action="" method="post" class="hide">{% csrf_token %}
    <h3>{% trans "Add user" %}</h3>
    <label for="id_email">{% trans "Email" %}</label><br />
    <input type="text" name="email" id="id_email" class="input" /><br />
    {% if enable_guest %}
    <label>{% trans "Role"%}</label><span class="icon-question-sign" title="{% trans "You can also add a user as a guest, who will not be allowed to create libraries and groups." %}" style="color:#666; margin-left:3px;"></span>
    <select name="role" class="w100">
        <option value={{default_user}} selected="selected">{% trans "Default"%}</option>
        <option value={{guest_user}}>{% trans "Guest"%}</option>
    </select><br />
    {% endif %}
    <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>
    <button type="submit" class="submit">{% trans "Submit" %}</button>
</form>

<form id="upload-csv-form" class="hide" enctype="multipart/form-data" method="post" action="{% url 'batch_add_user' %}">{% csrf_token %}
    <h3>{% trans "Import users from a CSV file" %}</h3>
    <input type="file" name="file" />
    <p class="tip">{% trans "File format: user@mail.com,password"%}</p>
    <p class="error hide">{% trans "Please choose a CSV file" %}</p>
    <button type="submit" class="submit">{% trans "Submit" %}</button>
</form>

{% include "sysadmin/useradmin_table.html"%}
{% include "snippets/admin_paginator.html" %}

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

{% block extra_script %}
<script type="text/javascript">
$(function(){
    // check if server version is the latest one
    $.ajax({
        url: 'http://seafile.com/ajax/check_server_version/?v=' + e('{{seafile_version}}') + '&p=' + e('{{platform}}') + '&id=' + '{{server_id}}',
        dataType: 'jsonp',
        jsonpCallback: 'xx',
        crossDomain: true,
        success: function(data) {
            if (data['need_update']) {
                var tip = "{% trans "A new server version %(v)s is available." %}";
                tip = tip.replace('%(v)s', data['new_version']);
                $('#title-panel').html('<p class="alc" style="background:#fddaa4;color:#1f0600;padding:3px 0;margin:0 0 15px;">' + tip + '</p>').removeClass('hide');
            }
        }
    });
});
$('#add-user-form').submit(function() {
    var form = $(this),
        form_id = $(this).attr('id'),
        email = $.trim(form.children('[name="email"]').val()),
        {% if enable_guest %}
        role = $('select[name="role"]', form).val(),
        {% endif %}
        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('[type="submit"]');
    disable(submit_btn);
    $.ajax({
        url: '{% url 'user_add' %}',
        type: 'POST',
        datatype: 'json',
        cache: 'false',
        beforeSend: prepareCSRFToken,
        data: {
            'email': email,
            {% if enable_guest %}
            'role': role,
            {% endif %}
            '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).error);
            } else {
                apply_form_error(form_id, "{% trans "Failed. Please check the network." %}");
            }
            enable(submit_btn);
        }
    });
    return false;
});
$('#upload-file-btn').click(function () {
    $('#upload-csv-form').modal();
    $('#simplemodal-container').css({'width':'auto', 'height':'auto'});
});
$('#upload-csv-form').submit(function() {
    var form = $(this);
    if (!$('[name=file]', form).val()) {
        $('.error', form).removeClass('hide');
        return false;
    }
});
{% include "sysadmin/useradmin_js.html" %}
</script>
{% endblock %}
