{% extends 'security/login_user.html' %}

{% macro _render_field_with_errors(field) %}
  <p>
    {{ field.label }} {{ field(**kwargs)|safe }}
    {% if field.errors %}
      <ul>
      {% for error in field.errors %}
        <li>{{ error }}</li>
      {% endfor %}
      </ul>
    {% endif %}
  </p>
{% endmacro %}

{% macro render_field(field) %}
  <p>{{ field(**kwargs)|safe }}</p>
{% endmacro %}

{% macro render_field_errors(field) %}
  <p>
    {% if field and field.errors %}
      <ul>
      {% for error in field.errors %}
        <li>{{ error }}</li>
      {% endfor %}
      </ul>
    {% endif %}
  </p>
{% endmacro %}

{% block page_header %}
<h5>{{ _("Two-factor authentication adds an extra layer of security to your account") }}</h1>
<h5>{{ _("In addition to your username and password, you'll need to use a code that we will send you") }}</h2>
{%endblock%}

{% block page_content %}
  <form action="{{ url_for_security("two_factor_setup") }}" method="POST" name="two_factor_setup_form">
        {{ two_factor_setup_form.hidden_tag() }}
        {% for subfield in two_factor_setup_form.setup %}
            {% if subfield.data in choices %}
                {{ _render_field_with_errors(subfield) }}
            {% endif %}
        {% endfor %}
        {{ render_field_errors(two_factor_setup_form.setup) }}
        {{ render_field(two_factor_setup_form.submit) }}
        {% if chosen_method=="email" and chosen_method in choices %}
            <p>{{ _("To complete logging in, please enter the code sent to your mail") }}</p>
        {% endif %}
        {% if chosen_method=="authenticator" and chosen_method in choices %}
            <p>{{ _("Open your authenticator app on your device and scan the following qrcode to start receiving codes:") }}</p>
            <p><img alt="{{ _("Two factor authentication code") }}" id="qrcode" src="{{ url_for_security("two_factor_qrcode") }}"></p>
        {% endif %}
        {% if chosen_method=="sms" and chosen_method in choices %}
            <p>{{ _("To Which Phone Number Should We Send Code To?") }}</p>
            {{ two_factor_setup_form.hidden_tag() }}
            {{ render_field_with_errors(two_factor_setup_form.phone, placeholder="enter phone number") }}
            {{ render_field(two_factor_setup_form.submit) }}
        {% endif %}
    </form>
    <form action="{{ url_for_security("two_factor_token_validation") }}" method="POST"
          name="two_factor_verify_code_form">
        {{ two_factor_verify_code_form.hidden_tag() }}
        {{ render_field_with_errors(two_factor_verify_code_form.code) }}
        {{ render_field(two_factor_verify_code_form.submit) }}
    </form>
    {% include "security/_menu.html" %}
{% endblock %}
