<header>
    <h2>
        <i class="icon-user"></i> {{ PMF_LANG['ad_user'] }}
        <div class="pull-right">
            <a class="btn btn-success" href="?action=user&amp;user_action=add">
                <i class="icon-plus"></i> {{ PMF_LANG["ad_user_add"] }}
            </a>
            {% if showListAllUsers %}
            <a class="btn btn-info" href="?action=user&amp;user_action=listallusers">
                <i class="icon-list"></i> {{ PMF_LANG['list_all_users'] }}
            </a>
            {% endif %}
        </div>
    </h2>
</header>

<script type="text/javascript" src="assets/js/user.js"></script>
<script type="text/javascript">
    /* <![CDATA[ */

    /**
     * Returns the user data as JSON object
     *
     * @param user_id User ID
     */
    function getUserData(user_id) {
        $('#user_data_table').empty();
        $.getJSON("index.php?action=ajax&ajax=user&ajaxaction=get_user_data&user_id=" + user_id, function(data) {
            $('#update_user_id').val(data.user_id);
            $('#user_status_select').val(data.status);
            $('#user_list_autocomplete').val(data.login);
            $("#user_list_select").val(data.user_id);
            // Append input fields
            $('#user_data_table').append(
                    '<div class="control-group">' +
                            '<label class="control-label">{{ PMF_LANG['ad_user_realname'] }}</label>' +
                            '<div class="controls">' +
                            '<input type="text" name="display_name" value="' + data.display_name + '" required />' +
                            '</div>' +
                            '</div>' +
                            '<div class="control-group">' +
                            '<label class="control-label">{{ PMF_LANG['ad_entry_email'] }}</label>' +
                            '<div class="controls">' +
                            '<input type="email" name="email" value="' + data.email + '" required />' +
                            '</div>' +
                            '</div>' +
                            '<input type="hidden" name="last_modified" value="' + data.last_modified + '" />'
            );
        });
    }
    /* ]]> */
</script>
<div id="user_message">{{ message|raw }}</div>

<div class="row-fluid">
    <div class="span4" id="userAccounts">
        <fieldset>
            <legend>{{ PMF_LANG['ad_user_username'] }}</legend>
            <form name="user_select" id="user_select" action="?action=user&amp;user_action=delete_confirm"
                  method="post">

                <label for="user_list_autocomplete">{{ PMF_LANG['ad_auth_user'] }}:</label>
                <input type="text" id="user_list_autocomplete" name="user_list_search" data-provide="typeahead" />
                <script type="text/javascript">
                    //<![CDATA[
                    var mappedIds,
                            userNames;
                    $('#user_list_autocomplete').typeahead({
                        source: function (query, process) {
                            return $.get("index.php?action=ajax&ajax=user&ajaxaction=get_user_list", { q: query }, function (data) {
                                mappedIds = [];
                                userNames = [];
                                $.each(data, function(i, user) {
                                    mappedIds[user.name] = user.user_id;
                                    userNames.push(user.name);
                                });
                                return process(userNames);
                            });
                        },
                        updater: function(userName) {
                            userId = mappedIds[userName];
                            $("#user_list_select").val(userId);
                            getUserData(userId);
                            getUserRights(userId);
                        }
                    });
                    //]]>
                </script>
                <p>
                    <input type="hidden" id="user_list_select" name="user_list_select" value="" />
                    <button class="btn btn-danger" type="submit">
                        {{ PMF_LANG['ad_gen_delete'] }}
                    </button>
                </p>
            </form>
        </fieldset>
    </div>
    <div class="span4" id="userDetails">
        <fieldset>
            <legend id="user_data_legend">{{ PMF_LANG['ad_user_profou'] }}</legend>
            <form action="?action=user&amp;user_action=update_data" method="post">
                <input id="update_user_id" type="hidden" name="user_id" value="0" />
                <p>
                    <label for="user_status_select" class="small">
                        {{ PMF_LANG['ad_user_status'] }}
                    </label>
                    <select id="user_status_select" name="user_status" >
                        <option value="active">{{ PMF_LANG['ad_user_active'] }}</option>
                        <option value="blocked">{{ PMF_LANG['ad_user_blocked'] }}</option>
                        <option value="protected">{{ PMF_LANG['ad_user_protected'] }}</option>
                    </select>
                </p>
                <div id="user_data_table"></div><!-- end #user_data_table -->
                <p>
                    <button class="btn btn-primary" type="submit">
                        {{ PMF_LANG['ad_gen_save'] }}
                    </button>
                </p>
            </form>
        </fieldset>
    </div>
    <div class="span4" id="userRights">
        <form id="rightsForm" action="?action=user&amp;user_action=update_rights" method="post">
            <fieldset>
                <legend id="user_rights_legend">{{ PMF_LANG['ad_user_rights'] }}</legend>
                <input id="rights_user_id" type="hidden" name="user_id" value="0" />

                <a class="btn btn-small" href="javascript:formCheckAll('rightsForm')">
                    {{ PMF_LANG['ad_user_checkall'] }}
                </a>
                <a class="btn btn-small" href="javascript:formUncheckAll('rightsForm')">
                    {{ PMF_LANG['ad_user_uncheckall'] }}
                </a>

                {% for right in rights %}
                <label class="checkbox">
                    <input id="user_right_{{ right['right_id'] }}" type="checkbox"
                           name="user_rights[]" value="{{ right['right_id'] }}"/>
                    {{ PMF_LANG['rightsLanguage'][right['name']]|default(right['description']) }}
                </label>
                {% endfor %}

                <button class="btn btn-primary" type="submit">
                    {{ PMF_LANG['ad_gen_save'] }}
                </button>

            </fieldset>
        </form>
    </div>
</div>

{% if renderUpdateUserScript %}
<script type="text/javascript">updateUser({{ updateUserId }});</script>
{% endif %}