{% extends "panel.html" %}

{% load i18n %}
{% load sso %}
{% load inventree_extras %}
{% load socialaccount %}
{% load crispy_forms_tags %}
{% load user_sessions i18n %}

{% block label %}account{% endblock label %}

{% block heading %}
{% trans "Account Settings" %}
{% endblock heading %}

{% block actions %}
{% inventree_customize 'hide_password_reset' as hide_password_reset %}
{% if not hide_password_reset %}
<div class='btn btn-outline-primary' type='button' id='edit-password' title='{% trans "Change Password" %}'>
    <span class='fas fa-key'></span> {% trans "Set Password" %}
</div>
{% endif %}
<div class='btn btn-primary' type='button' id='edit-user' title='{% trans "Edit User Information" %}'>
    <span class='fas fa-user-cog'></span> {% trans "Edit" %}
</div>
{% endblock actions %}

{% block content %}
{% mail_configured as mail_conf %}

<table class='table table-striped table-condensed'>
    <tr>
        <td>{% trans "Username" %}</td>
        <td>{{ user.username }}</td>
    </tr>
    <tr>
        <td>{% trans "First Name" %}</td>
        <td>{{ user.first_name }}</td>
    </tr>
    <tr>
        <td>{% trans "Last Name" %}</td>
        <td>{{ user.last_name }}</td>
    </tr>
</table>

<div class="row">
    <div class='panel-heading'>
        <div class='d-flex flex-span'>
        <h4>{% trans "Email" %}</h4>
        </div>
    </div>

    <div class="col-sm-6">
    {% if user.emailaddress_set.all %}
    <p>{% trans 'The following email addresses are associated with your account:' %}</p>

    <form action="{% url 'account_email' %}" class="email_list" method="post">
        {% csrf_token %}
        <fieldset class="blockLabels">

            {% for emailaddress in user.emailaddress_set.all %}
            <div>
            <div class="ctrlHolder">
                <label for="email_radio_{{ forloop.counter }}" class="{% if emailaddress.primary %}primary_email{% endif %}">

                    <input id="email_radio_{{ forloop.counter }}" type="radio" name="email" {% if emailaddress.primary or user.emailaddress_set.count == 1 %}checked="checked" {% endif %} value="{{ emailaddress.email }}" />

                    {% if emailaddress.primary %}
                    <strong>{{ emailaddress.email }}</strong>
                    {% else %}
                    {{ emailaddress.email }}
                    {% endif %}
                </label>

                {% if emailaddress.verified %}
                <span class='badge badge-right rounded-pill bg-success'>{% trans "Verified" %}</span>
                {% else %}
                <span class='badge badge-right rounded-pill bg-warning'>{% trans "Unverified" %}</span>
                {% endif %}
                {% if emailaddress.primary %}<span class='badge badge-right rounded-pill bg-primary'>{% trans "Primary" %}</span>{% endif %}
            </div>
            </div>
            {% endfor %}

            <div class="buttonHolder">
                <button class="btn btn-primary secondaryAction" type="submit" name="action_primary">{% trans 'Make Primary' %}</button>
                <button class="btn btn-primary secondaryAction" type="submit" name="action_send" {% if not mail_conf %}disabled{% endif %}>{% trans 'Re-send Verification' %}</button>
                <button class="btn btn-primary primaryAction" type="submit" name="action_remove">{% trans 'Remove' %}</button>
            </div>

        </fieldset>
    </form>

    {% else %}
    <div class='alert alert-block alert-danger'>
        <strong>{% trans 'Warning:' %}</strong>
        {% trans "You currently do not have any email address set up. You should really add an email address so you can receive notifications, reset your password, etc." %}
    </div>

    {% endif %}
    </div>

    {% if can_add_email %}
    <div class="col-sm-6">
    <h5>{% trans "Add Email Address" %}</h5>

    <form method="post" action="{% url 'account_email' %}" class="add_email">
        {% csrf_token %}
        {{ add_email_form|crispy }}
        <button class="btn btn-primary" name="action_add" type="submit">{% trans "Add Email" %}</button>
    </form>
    </div>
    {% endif %}
</div>

{% include "InvenTree/settings/user_sso.html" %}

<div class="row">
    <div class='panel-heading'>
        <h4>{% trans "Multifactor" %}</h4>
    </div>

    <div class="col-md-6">
    {% if user.staticdevice_set.all or user.totpdevice_set.all %}
    <p>{% trans 'You have these factors available:' %}</p>

    <table class="table table-striped">
        <thead>
            <th>Type</th>
            <th>Name</th>
        </thead>
        <tbody>
        {% for token in user.totpdevice_set.all %}
        <tr>
            <td>{% trans 'TOTP' %}</td>
            <td>{{ token.name }}</td>
        </tr>
        {% endfor %}
        {% for token in user.staticdevice_set.all %}
        <tr>
            <td>{% trans 'Static' %}</td>
            <td>{{ token.name }}</td>
        </tr>
        {% endfor %}
        </tbody>
    </table>

    {% else %}
    <div class='alert alert-block alert-warning'>
        {% trans "Multifactor authentication is not configured for your account" %}
    </div>

    {% endif %}
    </div>

    <div class="col-md-6">
    <h5>{% trans "Change factors" %}</h5>
    <a href="{% url 'two-factor-setup' %}" class="btn btn-primary {% if user.staticdevice_set.all and user.totpdevice_set.all %}disabled{% endif %}" role="button">{% trans "Setup multifactor" %}</a>
    {% if user.staticdevice_set.all or user.totpdevice_set.all %}
    <a href="{% url 'two-factor-remove' %}" class="btn btn-primary" role="button">{% trans "Remove multifactor" %}</a>
    {% endif %}
    </div>
</div>

<div class="row">
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Active Sessions" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if session_list.count > 1 %}
                <form method="post" action="{% url 'session_delete_other' %}">
                    {% csrf_token %}
                    <button type="submit" class="btn btn-sm btn-default btn-danger" title='{% trans "Log out active sessions (except this one)" %}'>
                        <span class='fas fa-sign-out-alt'></span> {% trans "Log Out Active Sessions" %}
                    </button>
                </form>
                {% endif %}
            </div>
        </div>
    </div>

    <div>
        {% trans "<em>unknown on unknown</em>" as unknown_on_unknown %}
        {% trans "<em>unknown</em>" as unknown %}
        <table class="table table-striped table-condensed">
        <thead>
            <tr>
            <th>{% trans "IP Address" %}</th>
            <th>{% trans "Device" %}</th>
            <th>{% trans "Last Activity" %}</th>
            </tr>
        </thead>
        {% for object in session_list %}
            <tr {% if object.session_key == session_key %}class="active"{% endif %}>
            <td>{{ object.ip }}</td>
            {% if object.user_agent or object.device %}
            <td>{{ object.user_agent|device|default_if_none:unknown_on_unknown|safe }}</td>
            {% else %}
            <td>{{ unknown_on_unknown }}</td>
            {% endif %}
            <td>
                {% if object.session_key == session_key %}
                {% blocktrans with time=object.last_activity|timesince %}{{ time }} ago (this session){% endblocktrans %}
                {% else %}
                {% blocktrans with time=object.last_activity|timesince %}{{ time }} ago{% endblocktrans %}
                {% endif %}
            </td>
            </tr>
        {% endfor %}
        </table>
    </div>
</div>
{% endblock content %}

{% block js_ready %}
(function() {
var message = '{% trans "Do you really want to remove the selected email address?" escape %}';
var actions = document.getElementsByName('action_remove');
if (actions.length) {
actions[0].addEventListener("click", function(e) {
if (! confirm(message)) {
e.preventDefault();
}
});
}
})();
{% endblock js_ready %}
