{% extends theme('base.html') %}
{% import 'macro/form.html' as fui %}

{%- block title -%}
    {% if user.user_name == edited_user.user_name %}
        {%- trans %}My profile{% endtrans -%}
    {% else %}
        {%- trans %}User profile edition{% endtrans -%}
    {% endif %}
{%- endblock -%}

{% block submenu %}
    <a class="item" href="{{ url_for('core.account.profile_edition', edited_user=edited_user) }}">
        <i class="id card icon"></i>
        {% trans %}Personal information{% endtrans %}
    </a>
    <a class="active item" href="{{ url_for('core.account.profile_settings', edited_user=edited_user) }}">
        <i class="tools icon"></i>
        {% trans %}Account settings{% endtrans %}
    </a>
{% endblock %}

{% macro render_field(field, noindicator=false) %}
    {% set lock_indicator = field.render_kw and ("readonly" in field.render_kw or "disabled" in field.render_kw) %}
    {% if edited_user.user_name == user.user_name or lock_indicator or noindicator %}
        {{ fui.render_field(field, **kwargs) }}
    {% elif field.name in edited_user.writable_fields %}
        {{ fui.render_field(field, **kwargs) }}
    {% elif field.name in edited_user.readable_fields %}
        {{ fui.render_field(field, indicator_icon="eye", indicator_text=_("This user cannot edit this field"), **kwargs) }}
    {% else %}
        {{ fui.render_field(field, indicator_icon="eye slash", indicator_text=_("This user cannot see this field"), **kwargs) }}
    {% endif %}
{% endmacro %}

{% block content %}
    <div class="ui clearing segment">
        <h2 class="ui center aligned header">
            <div class="content">
                {% trans %}Account settings{% endtrans %}
            </div>
            <div class="sub header" title="{{ edited_user.created|datetimeformat }}">
                {% trans creation_datetime=edited_user.created|dateformat %}Created on {{ creation_datetime }}{% endtrans %}
            </div>
        </h2>

        {% call fui.render_form(form, class_="profile-form info warning") %}

            {% if "user_name" in form %}
                {% block user_name_field scoped %}{{ render_field(form.user_name) }}{% endblock %}
            {% endif %}

            {% if features.has_account_lockability and "lock_date" in form and not edited_user.locked %}
                {% block lock_date_field scoped %}{{ render_field(form.lock_date) }}{% endblock %}
            {% endif %}

            {% if "groups" in form %}
                {% block groups_field scoped %}{{ render_field(form.groups) }}{% endblock %}
            {% endif %}

            <h2 class="ui center aligned header">
                <div class="content">
                    {% trans %}Password{% endtrans %}
                </div>
            </h2>
            <div class="ui info message">
                {% trans %}
                    You can change your password here by typing it once it each fields below.
                    The more a password is long, the more it is strong. A good password is a
                    long password that you will remember, not a complicated password that your
                    use for all your accounts.
                {% endtrans %}
            </div>

            {% if "password1" in form %}
                {% block password_field scoped %}
                    <div class="two fields">
                        {{ render_field(form.password1, noindicator=true) }}
                        {{ render_field(form.password2, noindicator=true) }}
                    </div>
                {% endblock %}
            {% endif %}

            {% if user.can_manage_users %}

                {% if not edited_user.has_password() %}

                    <div class="ui message warning visible">
                        {% if features.has_smtp and edited_user.can_edit_self %}
                            <button type="submit" name="action" value="password-initialization-mail" class="ui right floated primary button">
                                {% trans %}Send mail{% endtrans %}
                            </button>
                        {% endif %}
                        <div class="header">
                            {% trans %}This user does not have a password yet{% endtrans %}
                        </div>
                        {% if features.has_smtp and edited_user.can_edit_self %}
                            <p>{% trans %}You can solve this by:{% endtrans %}</p>
                            <ul class="ui list">
                                <li>{% trans %}setting a password using this form;{% endtrans %}</li>
                                <li>{% trans %}sending the user a password initialization mail, by clicking this button;{% endtrans %}</li>
                                <li>{% trans %}or simply waiting for the user to sign-in a first time, and then receive a password initialization mail.{% endtrans %}</li>
                            </ul>
                        {% else %}
                            <p>{% trans %}The user will not be able to authenticate unless the password is set{% endtrans %}</p>
                        {% endif %}
                    </div>

                {% elif features.has_smtp and edited_user.user_name != user.user_name and edited_user.has_password() and edited_user.can_edit_self %}

                    <div class="ui message info">
                        <button type="submit" name="action" value="password-reset-mail" class="ui right floated button">
                            {% trans %}Send mail{% endtrans %}
                        </button>
                        <div class="header">
                            {% trans %}Password reset{% endtrans %}
                        </div>
                        <p>{% trans %}If the user has forgotten his password, you can send him a password reset email by clicking this button.{% endtrans %}</p>
                    </div>

                {% endif %}

                {% if features.has_account_lockability and edited_user.locked %}

                    <div class="ui message warning visible">
                        <button type="submit" name="action" value="unlock" class="ui right floated button">
                            {% trans %}Unlock{% endtrans %}
                        </button>
                        <div class="header">
                            {% trans %}This user account is locked{% endtrans %}
                        </div>
                        <p>
                            {% trans %}The user won't be able to connect until their account is unlocked.{% endtrans %}
                        </p>
                    </div>

                {% endif %}

            {% endif %}

            <div class="ui right aligned container">
                <div class="ui stackable buttons">
                    {% if features.has_account_lockability and "lock_date" in user.writable_fields and not edited_user.locked %}
                        <button type="submit" class="ui right floated basic negative button confirm" name="action" value="confirm-lock" id="lock" formnovalidate>
                            {% trans %}Lock the account{% endtrans %}
                        </button>
                    {% endif %}

                    {% if user.can_manage_users or self_deletion %}
                        <button type="submit" class="ui right floated basic negative button confirm" name="action" value="confirm-delete" id="delete" formnovalidate>
                            {% if user.user_name != edited_user.user_name %}
                                {{ _("Delete the user") }}
                            {% else %}
                                {{ _("Delete my account") }}
                            {% endif %}
                        </button>
                    {% endif %}

                    {% if user.can_impersonate_users and user.identifier != edited_user.identifier and not edited_user.locked %}
                        <a href="{{ url_for('core.account.impersonate', puppet=edited_user) }}" class="ui right floated basic button" name="action" value="impersonate" id="impersonate" hx-boost="false">
                            {{ _("Impersonate") }}
                        </a>
                    {% endif %}

                    <button type="submit" class="ui right floated primary button" name="action" value="edit-settings" id="edit-settings">
                        {{ _("Edit") }}
                    </button>

                </div>
            </div>
        {% endcall %}
    </div>
{% endblock %}
