{% extends 'base.html' %}
{% import 'fomanticui.j2' as sui %}
{% import 'flask.j2' as flask %}

{% block script %}
    <script src="/static/js/profile.js"></script>
{% endblock %}

{% block content %}
{% if self_deletion or (user.moderator and edited_user) %}
<div class="ui basic modal">
    <div class="ui icon header">
        <i class="user minus icon"></i>
        {% trans %}Account deletion{% endtrans %}
    </div>
    <div class="content">
        <p>
        {% if user.uid != edited_user.uid %}
            {{ _("Are you sure you want to delete this user? This action is unrevokable and all the data about this user will be removed.") }}
        {% else %}
            {{ _("Are you sure you want to delete your account? This action is unrevokable and all your data will be removed forever.") }}
        {% endif %}
        </p>
    </div>
    <div class="actions">
        <div class="ui inverted cancel button">{% trans %}Cancel{% endtrans %}</div>
        <div class="ui inverted red approve button">{% trans %}Delete{% endtrans %}</div>
    </div>
</div>
{% endif %}

<div class="ui clearing segment">
    <h2 class="ui center aligned header">
        <div class="content">
            {% if not edited_user %}
                {% trans %}User creation{% endtrans %}
            {% elif user.uid == edited_user.uid %}
                {% trans %}My profile{% endtrans %}
            {% else %}
                {% trans %}User profile edition{% endtrans %}
            {% endif %}

        </div>
        <div class="sub header">
            {% if not edited_user %}
                {% trans %}Create a new user account{% endtrans %}
            {% elif user.uid == edited_user.uid %}
                {% trans %}Edit your personal informations{% endtrans %}
            {% else %}
                {% trans %}Edit informations about an user{% endtrans %}
            {% endif %}
        </div>
    </h2>

    {{ flask.messages() }}

    <form method="POST"
      id="{{ form.__class__.__name__|lower }}"
      action="{{ request.url }}"
      role="form"
      enctype="multipart/form-data"
      class="ui form{% if user.moderator and edited_user and not edited_user.has_password() %} warning{% endif %}"
      >

        {#{ sui.render_field(form.csrf_token) }#}

        <h4 class="ui dividing header">{% trans %}Personal information{% endtrans %}</h4>
        <div class="two fields">
            {% if "givenName" in form %}
                {{ sui.render_field(form.givenName) }}
            {% endif %}
            {% if "sn" in form %}
                {{ sui.render_field(form.sn) }}
            {% endif %}
        </div>
        {% if "mail" in form %}
            {{ sui.render_field(form.mail) }}
        {% endif %}
        {% if "telephoneNumber" in form %}
            {{ sui.render_field(form.telephoneNumber) }}
        {% endif %}
        {% if "employeeNumber" in form %}
            {{ sui.render_field(form.employeeNumber) }}
        {% endif %}

        <h4 class="ui dividing header">{% trans %}Account information{% endtrans %}</h4>
        {% if "uid" in form %}
            {{ sui.render_field(form.uid) }}
        {% endif %}
        {% if user.moderator and edited_user and not edited_user.has_password() %}
        <div class="ui message warning">
            {% if request.method == "POST" and request.form.action == "password-initialization-mail" %}
                <button type="submit" name="action" value="password-initialization-mail" class="ui right floated button">
                    {% trans %}Send again{% endtrans %}
                </button>
            {% else %}
                <button type="submit" name="action" value="password-initialization-mail" class="ui right floated primary button">
                    {% trans %}Send{% endtrans %}
                </button>
            {% endif %}
            <div class="header">
                {% trans %}This user does not have a password yet{% endtrans %}
            </div>
            <ul>
                <li>{% trans %}Set a password using this form;{% endtrans %}</li>
                <li>{% trans %}send the user a password initialization mail, by clicking this button;{% endtrans %}</li>
                <li>{% trans %}or simply wait for the user to sign-in a first time, and then receive a password initialization mail.{% endtrans %}</li>
            </ul>
        </div>
        {% endif %}
        <div class="two fields">
            {% if "password1" in form %}
                {{ sui.render_field(form.password1) }}
                {{ sui.render_field(form.password2) }}
            {% endif %}
        </div>

        <button type="submit" class="ui right floated primary button" name="action" value="edit" id="edit">
            {{ _("Submit") }}
        </button>
        {% if user.admin and edited_user and user.uid != edited_user.uid %}
        <a href="{{ url_for('canaille.account.impersonate', username=edited_user.uid[0]) }}" class="ui right floated basic button" name="action" value="impersonate" id="impersonate">
            {{ _("Impersonate") }}
        </a>
        {% endif %}
        {% if user.moderator and edited_user or self_deletion %}
        <button type="submit" class="ui right floated basic negative button confirm" name="action" value="delete" id="delete">
            {% if user.uid != edited_user.uid %}
                {{ _("Delete the user") }}
            {% else %}
                {{ _("Delete my account") }}
            {% endif %}
        </button>
        {% endif %}
    </form>
</div>
{% endblock %}
