{% extends 'admin.twig' %}

{% set title %}{{ 'UsersManager_ManageAccess'|translate }}{% endset %}

{% block content %}

<h2 piwik-enriched-headline
    help-url="http://piwik.org/docs/manage-users/">{{ title }}</h2>
<div id="sites" class="usersManager">
    <section class="sites_selector_container">
        <p>{{ 'UsersManager_MainDescription'|translate }}</p>

        {% set applyAllSitesText %}
            <strong>{{ 'UsersManager_ApplyToAllWebsites'|translate }}</strong>
        {% endset %}

        <div piwik-siteselector
             show-selected-site="true"
             only-sites-with-admin-access="true"
             class="sites_autocomplete"
             siteid="{{ idSiteSelected }}"
             sitename="{{ defaultReportSiteName }}"
             all-sites-text="{{ applyAllSitesText|raw }}"
             all-sites-location="top"
             id="usersManagerSiteSelect"
             switch-site-on-select="false"></div>
    </section>
</div>

{% block websiteAccessTable %}

{% import 'ajaxMacros.twig' as ajax %}
{{ ajax.errorDiv }}
{{ ajax.loadingDiv }}

<div class="entityContainer" style="width:600px;margin-top:16px;">
    {% if anonymousHasViewAccess %}
        <br/>
        <div class="alert alert-warning">
            {{ ['UsersManager_AnonymousUserHasViewAccess'|translate("'anonymous'","'view'"), 'UsersManager_AnonymousUserHasViewAccess2'|translate]|join(' ') }}
        </div>
    {% endif %}
    <table class="entityTable dataTable" id="access" style="display:inline-table;width:550px;">
        <thead>
        <tr>
            <th class='first'>{{ 'UsersManager_User'|translate }}</th>
            <th>{{ 'UsersManager_Alias'|translate }}</th>
            <th>{{ 'UsersManager_PrivNone'|translate }}</th>
            <th>{{ 'UsersManager_PrivView'|translate }}</th>
            <th>{{ 'UsersManager_PrivAdmin'|translate }}</th>
        </tr>
        </thead>

        <tbody>
        {% set accesValid %}<img src='plugins/UsersManager/images/ok.png' class='accessGranted' />{% endset %}
        {% set accesInvalid %}<img src='plugins/UsersManager/images/no-access.png' class='updateAccess' />{% endset %}
        {% set superUserAccess %}<span title="{{ 'UsersManager_ExceptionSuperUserAccess'|translate }}">N/A</span>{% endset %}
        {% for login,access in usersAccessByWebsite %}
            {% if userIsSuperUser or (hasOnlyAdminAccess and (access!='noaccess' or idSiteSelected == 'all'))  %}
            <tr>
                <td id='login'>{{ login }}</td>
                <td>{{ usersAliasByLogin[login]|raw }}</td>
                <td id='noaccess'>
                    {% if login in superUserLogins %}
                        {{ superUserAccess }}
                    {% elseif access=='noaccess' and idSiteSelected != 'all' %}
                        {{ accesValid }}
                    {% else %}
                        {{ accesInvalid }}
                    {% endif %}&nbsp;</td>
                <td id='view'>
                    {% if login in superUserLogins %}
                        {{ superUserAccess }}
                    {% elseif access == 'view' and idSiteSelected != 'all' %}
                        {{ accesValid }}
                    {% else %}
                        {{ accesInvalid }}
                    {% endif %}&nbsp;</td>
                <td id='admin'>
                    {% if login in superUserLogins %}
                        {{ superUserAccess }}
                    {% elseif login == 'anonymous' %}
                        N/A
                    {% else %}
                        {% if access == 'admin' and idSiteSelected != 'all' %}{{ accesValid }}{% else %}{{ accesInvalid }}{% endif %}&nbsp;
                    {% endif %}
                </td>
            </tr>
            {% endif %}
        {% endfor %}
        </tbody>
    </table>
    <div id="accessUpdated" style="vertical-align:top;"></div>
</div>

{% if hasOnlyAdminAccess %}
    <p>
        <button id="showGiveViewAccessForm" class="add-user btn btn-lg btn-flat">
            <span class="icon-add"></span>
            {{ 'UsersManager_GiveViewAccessTitle'|translate('"' ~ defaultReportSiteName ~ '"')|raw }}
        </button>
    </p>
    <form id="giveViewAccessForm">
        <div class="form-group">
            <input type="text" name="user_invite"
                   id="user_invite"
                   placeholder="{{ 'UsersManager_EnterUsernameOrEmail'|translate|e('html_attr') }}"
                   title="{{ 'UsersManager_GiveViewAccessInstructions'|translate("'" ~ defaultReportSiteName ~ "'")|e('html_attr') }}">
        </div>

         <input class="btn" type="button" id="giveUserAccessToViewReports" value="{{ 'UsersManager_GiveViewAccess'|translate|e('html_attr') }}">
    </form>
    {{ ajax.errorDiv('ajaxErrorGiveViewAccess') }}
    {{ ajax.loadingDiv('ajaxLoadingGiveViewAccess') }}
{% endif %}

<div class="ui-confirm" id="confirm">
    <h2>{{ 'UsersManager_ChangeAllConfirm'|translate("<span id='login'></span>")|raw }}</h2>
    <input role="yes" type="button" value="{{ 'General_Yes'|translate }}"/>
    <input role="no" type="button" value="{{ 'General_No'|translate }}"/>
</div>

{% if userIsSuperUser %}
    <div class="ui-confirm" id="confirmUserRemove">
        <h2></h2>
        <input role="yes" type="button" value="{{ 'General_Yes'|translate }}"/>
        <input role="no" type="button" value="{{ 'General_No'|translate }}"/>
    </div>
    <div class="ui-confirm" id="confirmPasswordChange">
        <h2>{{ 'UsersManager_ChangePasswordConfirm'|translate }}</h2>
        <input role="yes" type="button" value="{{ 'General_Yes'|translate }}"/>
        <input role="no" type="button" value="{{ 'General_No'|translate }}"/>
    </div>
    <br/>
    <h2>{{ 'UsersManager_UsersManagement'|translate }}</h2>
    <p>{{ 'UsersManager_UsersManagementMainDescription'|translate }}
        {{ 'UsersManager_ThereAreCurrentlyNRegisteredUsers'|translate("<b>"~usersCount~"</b>")|raw }}</p>
    {% import 'ajaxMacros.twig' as ajax %}
    {{ ajax.errorDiv('ajaxErrorUsersManagement') }}
    {{ ajax.loadingDiv('ajaxLoadingUsersManagement') }}
    <div class="user entityContainer" style="margin-bottom:50px;">
        <table class="entityTable dataTable" id="users">
            <thead>
            <tr>
                <th>{{ 'General_Username'|translate }}</th>
                <th>{{ 'General_Password'|translate }}</th>
                <th>{{ 'UsersManager_Email'|translate }}</th>
                <th>{{ 'UsersManager_Alias'|translate }}</th>
                <th>token_auth</th>
                {% if showLastSeen is defined and showLastSeen %}
                <th>{{ 'UsersManager_LastSeen'|translate }}</th>
                {% endif %}
                <th>{{ 'General_Edit'|translate }}</th>
                <th>{{ 'General_Delete'|translate }}</th>
            </tr>
            </thead>

            <tbody>
            {% for i,user in users %}
                {% if user.login != 'anonymous' %}
                    <tr class="editable" id="row{{ i }}">
                        <td id="userLogin" class="editable">{{ user.login }}</td>
                        <td id="password" class="editable">-</td>
                        <td id="email" class="editable">{{ user.email }}</td>
                        <td id="alias" class="editable">{{ user.alias|raw }}</td>
                        <td id="token_auth" class="token_auth" data-token="{{ user.token_auth }}">{{ user.token_auth|slice(0, 8) }}…</td>
                        {% if user.last_seen is defined %}
                        <td id="last_seen">{% if user.last_seen is empty %}-{% else %}{{ 'General_TimeAgo'|translate(user.last_seen)|raw }}{% endif %}</td>
                        {% endif %}
                        <td class="text-center">
                            <button class="edituser btn btn-flat" id="row{{ i }}" title="{{ 'General_Edit'|translate }}">
                                <span class="icon-edit"></span>
                            </button>
                        </td>
                        <td class="text-center">
                            <button class="deleteuser btn btn-flat" id="row{{ i }}" title="{{ 'General_Delete'|translate }}">
                                <span class="icon-delete"></span>
                            </button>
                        </td>
                    </tr>
                {% endif %}
            {% endfor %}
            </tbody>
        </table>
        <p>
            <button class="add-user btn btn-lg btn-flat">
                <span class="icon-add"></span>
                {{ 'UsersManager_AddUser'|translate }}
            </button>
        </p>
    </div>

    <h2 id="super_user_access">{{ 'UsersManager_SuperUserAccessManagement'|translate }}</h2>
    <p>{{ 'UsersManager_SuperUserAccessManagementMainDescription'|translate }} <br/>
    {{ 'UsersManager_SuperUserAccessManagementGrantMore'|translate }}</p>

    {{ ajax.errorDiv('ajaxErrorSuperUsersManagement') }}
    {{ ajax.loadingDiv('ajaxLoadingSuperUsersManagement') }}

    <table class="entityTable dataTable" id="superUserAccess" style="display:inline-table;width:400px;">
        <thead>
        <tr>
            <th class='first'>{{ 'UsersManager_User'|translate }}</th>
            <th>{{ 'UsersManager_Alias'|translate }}</th>
            <th>{{ 'Installation_SuperUser'|translate }}</th>
        </tr>
        </thead>

        <tbody>
        {% if users|length > 1 %}
            {% for login,alias in usersAliasByLogin if login != 'anonymous' %}
                <tr>
                    <td id='login'>{{ login }}</td>
                    <td>{{ alias|raw }}</td>
                    <td id='superuser' data-login="{{ login|e('html_attr') }}">
                        <img src='plugins/UsersManager/images/ok.png' class='accessGranted' data-hasaccess="1" {% if not (login in superUserLogins) %}style="display:none"{% endif %} />
                        <img src='plugins/UsersManager/images/no-access.png' class='updateAccess' data-hasaccess="0" {% if login in superUserLogins %}style="display:none"{% endif %} />
                        &nbsp;
                    </td>
                </tr>
            {% endfor %}
        {% else %}
            <tr>
                <td colspan="3">
                    {{ 'UsersManager_NoUsersExist'|translate }}
                </td>
            </tr>
        {% endif %}
        </tbody>
    </table>

    <div id="superUserAccessUpdated" style="vertical-align:top;"></div>

    <div class="ui-confirm" id="superUserAccessConfirm">
        <h2> </h2>
        <input role="yes" type="button" value="{{ 'General_Yes'|translate }}"/>
        <input role="no" type="button" value="{{ 'General_No'|translate }}"/>
    </div>

{% endif %}
{% endblock %}

{% endblock %}
