{% extends "admin-base.twig" %}

{% block title %}Admin / Users{% endblock %}
{% block adminpage %}

{% set current_user = user %}

<div class="Admin-Users widget docs" data-widget="admin_users.AdminUsers">
    <div class="error-email-missing alert alert-error hidden">
        {% trans "email-missing" %}
    </div>
    <div class="error-email-invalid error-email-is-invalid alert alert-error hidden">
        {% trans "email-invalid" %}
    </div>
    <div class="error-email-already-exists alert alert-error hidden">
        {% trans "email-already-exists" %}
    </div>
     <div class="error-password-already-reset alert alert-error hidden">
        {% trans "password-already-reset" %}
    </div>
    <div class="success alert alert-success hidden">
    </div>
    <!-- NOTE:  Never shown like that, used to translate this confirmation dialog's texts.
                Need to be inlined.                                                         -->
    <div class="confirm-admin-lost hidden">{% trans "will lose his administrator status. Continue?" %}</div>
    <div class="confirm-admin-new hidden">{% trans "will become an administrator. Continue?" %}</div>
    <div class="confirm-delete hidden">{% trans "will be deleted. Continue?" %}</div>

    <form action="/admin/users" method="GET" class="pull-right form-horizontal">
        <input type="text" name="q" value="{{ q }}" placeholder="Seach by name or email" /> <input type="submit" class="btn" value="Search" />
    </form>
    <p>Below you find the complete list of users. You can search by email.</p>

    <a href="#" class="btn do when-not-addUserForm" data-action="showAddUserForm">{% trans "Add a user"%}</a>
    <div class="addUser when-addUserForm">
        <form action="#" method="post" class="form-inline">
            <fieldset>
                <legend>{% trans "Add a user" %}</legend>
                <div class="input-prepend">
                    <span class="add-on"><i class="icon-envelope"></i></span>
                    <input class="span2" name="email" placeholder="{% trans "email" %}" type="text" />
                </div>
                <div class="input-prepend">
                    <span class="add-on"><i class="icon-tag"></i></span>
                    <select name="status">
                        <option value="editor">{% trans "Editor" %}</option>
                        <option value="admin">{% trans "Administrator" %}</option>
                        <!-- <option value="graphic-editor">{% trans "Graphic Editor" %}</option> -->
                        {% if user.isSysAdmin %}<option value="sysadmin">{% trans "System Administrator" %}</option>{% endif %}
                    </select>
                </div>
                <input type="submit" name="addUser" value="{% trans "Create" %}" class="btn btn-danger do" data-action="addUserAction" />
            </fieldset>
            <p class="loading hidden">{% trans "Loading ..." %}</p>
        </form>
    </div>
    <table class="table users">
        <thead>
            <tr>
                <th><a href="?sort=id">#</a></th>
                <th><a href="?sort=name">{% trans "Name" %}</a></th>
                <th><a href="?sort=email">{% trans "Sign-In" %}</a></th>
                <th>{% trans "Status" %}</th>
                <th><a href="?sort=created_at">{% trans "Created at" %}</a></th>
                <th class="center">{% trans "Charts" %}</th>
                {% if has_plugin('admin-organizations') %}<th class="center">{% trans "Organization" %}</th>{% endif %}
                <th>{% trans "Actions" %}</th>
            </tr>
        </thead>
        <tbody class="users">
            {% for user in users %}
                <tr class="user {{ user.role }} {% if user.activateToken %}pending{% endif %}" data-id="{{ user.id }}">
                    <td class="id out"><a href="/admin/users/{{ user.id }}">{{ user.id }}</a></td>
                    <td class="name out">{{ user.name ? user.name : '' }}</td>
                    <td class="email out">{{ user.email ? user.email : user.oauthSignin }}</td>
                    <td>
                        {% if user.isAdmin %}
                            <i class="icon-fire" title="{% trans "Administrator" %}"></i>
                        {% else %}
                            <i class="icon-user" title="{% trans "Editor" %}"></i>
                        {% endif %}
                        {{ user.role }}
                    </td>
                    <td class="creation out">{{ user.createdAt | date('M jS, Y') }}</td>
                    <td class="center">
                        <a href="/admin/charts/{{ user.id }}">{{ user.chartCount() }}</a></td>
                    {% if has_plugin('admin-organizations') %}
                    <td class="center organizations">
                        {% for org in user.organizations %}<a href="/admin/organizations/{{ org.id }}">{{ org.id }}</a>{% endfor %}
                    </td>
                    {% endif %}
                    <td class="actions">
                        <a href="#" class="do" data-action="editAction" data-id="{{user.id}}"><i class="icon-pencil" title="{% trans "edit" %}"></i></a>
                        {% if user.activateToken %}
                            <a href="#" class="do" data-action="resendAction" data-id="{{user.id}}"><i class="icon-envelope" title="{% trans "resend an invitation" %}"></i></a>
                        {% else %}
                            <a href="#" class="do" data-action="resetAction" data-id="{{user.id}}"><i class="icon-envelope" title="{% trans "reset the password and send a mail" %}"></i></a>
                        {% endif %}
                        {% if current_user != user %}
                            <a href="#" class="do" data-action="removeAction" data-id="{{user.id}}"><i class="icon-trash" title="{% trans "delete" %}"></i></a>
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
            <!-- Template of a editable row -->
             <tr class="user edition template">
                <td class="id out"></td>
                <td class="email"><input type="text"/></td>
                <td>
                    <select name="status">
                        <option value="pending">{% trans "Pending" %}</option>
                        <option value="editor">{% trans "Editor" %}</option>
                        <option value="admin">{% trans "Administrator" %}</option>
                        <option value="graphic-editor">{% trans "Graphic Editor" %}</option>
                        {% if user.isSysAdmin %}<option value="sysadmin">{% trans "System Administrator" %}</option>{% endif %}
                    </select>
                </td>
                <td class="creation out"></td>
                <td class="center">--</td>
                <td class="center">--</td>
                <td class="actions">
                    <a href="#" class="do" data-action="saveEditAction"><i class="icon-ok" title="{% trans "save" %}"></i></a>
                    <a href="#" class="do" data-action="cancelEditAction"><i class="icon-remove" title="{% trans "cancel" %}"></i></a>
                </td>
            </tr>
        </tbody>
    </table>
    <div class="pull-right">
        {{ block('pagination') }}
    </div>
</div>

{% endblock %}
