{% macro render_select(field, disabled=False) -%}
{# Renders selects. If there's only one choice, it hides the input. #}
{% if field.iter_choices()|list|length == 1 %}
{{ field.label }}
{{ field(class='form-control', disabled='') }}
{% else %}
{{ field.label }}
{% if disabled %}
{{ field(class='form-control', disabled='') }}
{% else %}
{{ field(class='form-control') }}
{% endif %}
{% endif %}
{% if field.description %}
  <span class="help-block">{{ field.description }}</span>
{% endif %}
{{ render_errors(field) }}
{%- endmacro %}

{% macro render_errors(field) -%}
{% if field.errors %}
<i class="fa fa-times form-control-feedback"></i>
  {% for error in field.errors %}
    {% if error is string %}
<span class="help-block">{{ error }}</span>
    {% elif error is iterable %}
<ul class="help-block">
      {% for sub_error in error %}
  <li>{{ sub_error }}</li>
      {% endfor %}
</ul>
    {% endif %}
  {% endfor %}
{% endif %}
{%- endmacro %}

{% macro render_input(field, inline=False, disabled=False) -%}
<div class="form-group{{ " has-error has-feedback" if field.errors }}">
  {% if inline %}
  {{ field.label(class="sr-only") }}
  {{ field(class="form-control", placeholder=field.label.text, disabled=disabled) }}
  {% else %}
    {% if field.widget.input_type == 'submit' %}
      {{ field(class="btn btn-default") }}
    {% elif field.widget.input_type == 'image' %}
      {{ field(class="", label="") }}
    {% else %}
      {% if field.iter_choices is defined %}
      {{ render_select(field, disabled=disabled) }}
      {% else %}
        {% if field.widget.input_type != 'hidden' %}
          {{ field.label }}
        {% endif %}
        {% if disabled %}
        {{ field(class="form-control", disabled="") }}
        {% else %}
        {{ field(class="form-control") }}
        {% endif %}
        {% if field.description %}
          <span class="help-block">{{ field.description }}</span>
        {% endif %}
        {{ render_errors(field) }}
      {%- endif %}
    {% endif %}
  {% endif %}
</div>
{%- endmacro %}

{% macro render_form(form, inline=False) %}
<form class="form" method="post">
  {% for field in form %}
  {{ render_input(field, inline) }}
  {% endfor %}
</form>
{% endmacro %}

{% macro navbar_button(text, view=None, count=None, id=None) %}
{% if view %}
{% with url = url_for(view) %}
<li{{ ' class="active"'|safe if url == request.path }}>
  <a href="{{ url }}">
    {{ text }}
    {% if count != None %}
    <span class="badge hidden-sm"{{ (' id="' ~ id ~ '"')|safe if id != None }}>{{ count|numberfmt if count != 0 }}</span>
    {% endif %}
  </a>
</li>
{% endwith %}
{% else %}
<li><a href="#">{{ text }}</a></li>
{% endif %}
{% endmacro %}

{% macro status_color(action_type) %}
{% if action_type == ActionType.evaluating -%}
warning
{%- elif action_type == ActionType.approved -%}
info
{%- elif action_type == ActionType.paid -%}
success
{%- elif action_type == ActionType.incomplete or action_type == ActionType.rejected -%}
danger
{%- endif %}
{% endmacro %}

{% macro clipboard_button(value, position="top", title=None, classes='btn btn-default btn-sm') %}
{% with %}
{% if title != None %}
{% set copy_label = title %}
{% else %}
{% set copy_label = ("'" ~ value ~ "'") %}
{% endif %}
{# TRANS: This is a tooltip that appears when you move the mouse over a button that will copy some text to the clipboard when clicked. #}
<button class="{{ classes }} copy-btn" data-clipboard-text="{{ value }}" data-toggle="tooltip" data-placement="{{ position }}" title="{{ gettext('Copy %(copy_label)s to clipboard', copy_label=copy_label) }}">{{ value }}</button>
{% endwith %}
{% endmacro %}

{% macro modifier_value(modifier) %}
{% if modifier._type == 'AbsoluteModifier' %}
{% with value = modifier.value / 1000000 %}
  {% if modifier.value >= 0 %}
  {# TRANS: How a flat ISK bonus is written as text. The %(amount)s is special,
            it will be replaced with the bonus amount (in millions of ISK)
  #}
  {{ gettext("%(amount)sM ISK bonus", amount=(value|numberfmt)) }}
  {% else %}
  {# TRANS: How a flat ISK penalty is written as text. The %(amount)s is
            special, it will be replaced with the penalty amount (in
            millions of ISK).
  #}
  {{ gettext("%(amount)sM ISK penalty", amount=(value|numberfmt)) }}
  {% endif %}
{% endwith %}
{% elif modifier._type == 'RelativeModifier' %}
  {% if modifier.value >= 0 %}
  {# TRANS: How a percentage bonus is written as text. The %(amount)s
            is special, it will be replaced with the amount as a percentage,
            formatted according to the current locale.
  #}
  {{ gettext("%(amount)s bonus", amount=(modifier.value|percentfmt)) }}
  {% else %}
  {# TRANS: How a percentage penalty is written as text. The %(amount)s
            is special, it will be replaced with the amount as a percentage,
            formatted according to the current locale.
  #}
  {{ gettext("%(amount)s penalty", amount=(modifier.value|percentfmt)) }}
  {% endif %}
{% endif %}
{% endmacro %}
