{% extends 'manage/users/master.twig' %}
{% from 'macros.twig' import container_title %}
{% from 'manage/macros.twig' import permissions_table %}
{% from '_layout/input.twig' import input_csrf, input_text, input_checkbox, input_file, input_select, input_colour, input_hidden %}

{% set site_link = url('user-profile', {'user': user_info.id}) %}

{% block manage_content %}
    <div class="manage__user" style="--accent-colour: {{ user_info.userColour }}">
        {% if manage_notices|length > 0 %}
            <div class="warning">
                <div class="warning__content">
                    {% for notice in manage_notices %}
                        <p>{{ notice }}</p>
                    {% endfor %}
                </div>
            </div>
        {% endif %}

        <form method="post" action="{{ url('manage-user', {'user': user_info.id}) }}" class="container manage__user__container">
            {{ container_title('Editing ' ~ user_info.username ~ ' (' ~ user_info.id ~ ')') }}
            {{ input_csrf() }}

            <div class="manage__user__details">
                <label class="form__label">
                    <div class="form__label__text">Username</div>
                    <div class="form__label__input">
                        {{ input_text(can_edit_user ? 'user[username]' : '', 'manage__user__input', user_info.username, 'text', '', true, {'maxlength':16}) }}
                    </div>
                </label>

                <label class="form__label">
                    <div class="form__label__text">E-mail address</div>
                    <div class="form__label__input">
                        {{ input_text(can_edit_user ? 'user[email]' : '', 'manage__user__input', user_info.emailAddress, 'text', '', true, {'maxlength':255}) }}
                    </div>
                </label>

                <label class="form__label">
                    <div class="form__label__text">Title</div>
                    <div class="form__label__input">
                        {{ input_text(can_edit_user ? 'user[title]' : '', 'manage__user__input', user_info.title, 'text', '', false, {'maxlength':64}) }}
                    </div>
                </label>

                <label class="form__label">
                    <div class="form__label__text">Joined</div>
                    <div class="form__label__input">
                        {{ input_text('', 'manage__user__input', user_info.createdTime|date('c')) }}
                    </div>
                </label>

                <label class="form__label">
                    <div class="form__label__text">Last online</div>
                    <div class="form__label__input">
                        {{ input_text('', 'manage__user__input', user_info.activeTime|date('c')) }}
                    </div>
                </label>

                <label class="form__label">
                    <div class="form__label__text">Register IP</div>
                    <div class="form__label__input">
                        {{ input_text('', 'manage__user__input', user_info.registerRemoteAddress) }}
                    </div>
                </label>

                <label class="form__label">
                    <div class="form__label__text">Last IP</div>
                    <div class="form__label__input">
                        {{ input_text('', 'manage__user__input', user_info.lastRemoteAddress) }}
                    </div>
                </label>

                <label class="form__label">
                    <div class="form__label__text">Country</div>
                    <div class="form__label__input">
                        {{ input_text(can_edit_user ? 'user[country]' : '', 'manage__user__input', user_info.country, 'text', 'XX', true, {'maxlength':2,'minlength':2}) }}
                    </div>
                </label>

                {% if can_edit_user %}
                    <label class="form__label">
                        <div class="form__label__text">New Password</div>
                        <div class="form__label__input">
                            {{ input_text('password[new]', 'manage__user__input', '', 'password') }}
                        </div>
                    </label>

                    <label class="form__label">
                        <div class="form__label__text">Confirm Password</div>
                        <div class="form__label__input">
                            {{ input_text('password[confirm]', 'manage__user__input', '', 'password') }}
                        </div>
                    </label>
                {% endif %}
            </div>

            <div class="manage__user__details">
                <label class="form__label">
                    <div class="form__label__text">Custom Colour</div>
                    <div class="form__label__input">
                        {{ input_checkbox('colour[enable]', '', not user_info.userColour.shouldInherit, '', '', false, null, not can_edit_user) }}
                    </div>
                </label>

                {{ input_colour(can_edit_user ? 'colour[hex]' : '', '', user_info.userColour) }}
            </div>

            {# TODO: if the hierarchy of the current user is too low to touch the role then opacity should be lowered and input disabled #}
            <div class="manage__user__details">
                <div class="manage__tags manage__tags--fixed">
                    {% for role in manage_roles %}
                        <label class="manage__tag" style="--accent-colour: {{ role.colour }}">
                            <div class="manage__tag__background"></div>
                            <div class="manage__tag__content">
                                {{ input_checkbox('roles[]', '', user_info.hasRole(role), 'manage__tag__checkbox', role.id, false, null, not can_edit_user) }}
                                <div class="manage__tag__title">
                                    {{ role.name }}
                                </div>
                                {{ input_checkbox('user[display_role]', '', role.id == user_info.displayRoleId, 'manage__tag__checkbox', role.id, true, null, not can_edit_user) }}
                            </div>
                        </label>
                    {% endfor %}
                </div>
            </div>

            {% if can_edit_user %}
                <div class="manage__user__buttons">
                    <button class="input__button manage__user__button">Save</button>
                    <button class="input__button manage__user__button" type="reset">Reset</button>
                </div>
            {% endif %}
        </form>

        {% if current_user.super %}
            <form method="post" action="{{ url('manage-user', {'user': user_info.id}) }}" class="container manage__user__container">
                {{ container_title('Send test e-mail to ' ~ user_info.username ~ ' (' ~ user_info.id ~ ')') }}

                <p style="font-size: 2em; line-height: 2em; text-align: center; font-weight: 700;">DO NOT ABUSE THIS SHIT</p>

                {{ input_csrf() }}
                {{ input_hidden('send_test_email', 'yes_send_it') }}

                <div class="manage__user__buttons">
                    <button class="input__button manage__user__button">Send</button>
                </div>
            </form>

            <form method="post" action="{{ url('manage-user', {'user': user_info.id}) }}" class="container manage__user__container">
                {{ container_title('Impersonate ' ~ user_info.username ~ ' (' ~ user_info.id ~ ')') }}

                <div class="container__content">
                    <p>Uses a special token to completely impersonate this user for testing.</p>
                </div>

                {{ input_csrf() }}
                {{ input_hidden('impersonate_user', 'meow') }}

                <div class="manage__user__buttons">
                    <button class="input__button manage__user__button">Impersonate</button>
                </div>
            </form>
        {% endif %}

        <form method="post" action="{{ url('manage-user', {'user': user_info.id}) }}" class="container manage__user__container">
            {{ container_title('Permissions for ' ~ user_info.username ~ ' (' ~ user_info.id ~ ')') }}

            {{ permissions_table(permissions, not can_edit_perms) }}

            {% if can_edit_perms %}
                {{ input_csrf() }}

                <div class="manage__user__buttons">
                    <button class="input__button manage__user__button">Save</button>
                    <button class="input__button manage__user__button" type="reset">Reset</button>
                </div>
            {% endif %}
        </form>
    </div>
{% endblock %}
