{% extends theme('base.html') %}
{% import 'fomanticui.html' as fui %}

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

{% block content %}
    <div class="invitationform">
        {% if form_validated %}

            <div class="ui attached segment">
                <h2 class="ui center aligned header">
                    <div class="content">
                        {% if request.form["action"] == "generate" %}
                            {{ _("Invitation link") }}
                        {% elif mail_sent %}
                            {{ _("Invitation sent") }}
                        {% else %}
                            {{ _("Invitation not sent") }}
                        {% endif %}
                    </div>
                </h2>
            </div>

            {% if request.form["action"] == "generate" %}

                <div class="ui attached success message">
                    {% trans %}Here is the invitation link you can provide to the user you want to invite:{% endtrans %}
                </div>

            {% elif mail_sent %}

                <div class="ui attached success message">
                    {% set email = form.mail.data %}
                    {% trans %}This invitation link has been sent to {{ email }}{% endtrans %}
                    {% trans %}If you need to provide this link by other ways than email, you can copy it there:{% endtrans %}
                </div>

            {% else %}

                <div class="ui attached error message">
                    {% set email = form.mail.data %}
                    {% trans %}This invitation link could not be sent to {{ email }} due to technical issues.{% endtrans %}
                    {% trans %}However you can copy the link there to provide it by other ways than email:{% endtrans %}
                </div>

            {% endif %}


            <div class="ui attached segment">
                <div class="ui center aligned container">
                    <div class="ui fields">
                        <div class="ui action input">
                            <input type="text" value="{{ registration_url }}" readonly id="copy-text">
                            <button class="ui primary right labeled icon button" id="copy-button">
                                <i class="copy icon"></i>
                                {% trans %}Copy{% endtrans %}
                            </button>
                        </div>
                    </div>
                    <br>
                    <div class="ui stackable buttons">
                        <a class="ui right floated button" href="{{ url_for("account.profile_creation") }}">
                            {{ _("Create a user") }}
                        </a>
                        {% if request.form["action"] == "generate" or mail_sent %}
                            <a href="{{ url_for('account.user_invitation') }}" class="ui right floated button" name="action" value="invite" id="invite">
                                {{ _("Invite another user") }}
                            </a>
                        {% endif %}
                    </div>
                </div>
            </div>

        {% else %}

            <div class="ui attached segment">
                <h2 class="ui center aligned header">
                    <div class="content">
                        {{ _("Invitate an user") }}
                    </div>
                </h2>
            </div>

            <div class="ui attached message">
                {% trans %}
                    After this form is sent, the recipient your indicated will receive an email
                    containing an account creation link.
                {% endtrans %}
            </div>

            <div class="ui attached segment">
                <form method="POST"
                    id="{{ form.id or form.__class__.__name__|lower }}"
                    action="{{ request.url }}"
                    role="form"
                    class="ui form"
                >
                    {{ form.hidden_tag() if form.hidden_tag }}
                    {% block uid_field scoped %}
                        {{ fui.render_field(form.uid, icon="user") }}
                    {% endblock %}

                    {% block uid_editable_field scoped %}
                        {{ fui.render_checkbox(form.uid_editable) }}
                    {% endblock %}

                    {% block mail_field scoped %}
                        {{ fui.render_field(form.mail, icon="mail") }}
                    {% endblock %}

                    {% block groups_field scoped %}
                        {{ fui.render_field(form.groups, icon="users") }}
                    {% endblock %}

                    <div class="ui right aligned container">
                        <div class="ui stackable buttons">
                            <a class="ui right floated button" href="{{ url_for("account.profile_creation") }}">
                                {{ _("Create a user") }}
                            </a>
                            <button type="submit" name="action", value="generate" class="ui right floated primary button">
                                {{ _("Generate a link") }}
                            </button>
                            <button type="submit" name="action" value="send" class="ui right floated primary button">
                                {{ _("Send the invitation") }}
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        {% endif %}
    </div>
{% endblock %}
