{% from "macros/box.html" import m_box %}

{% macro m_vocabulary(voc) %}
  {%- set items = voc.query.all() %}
  {%- set voc_url = url_for('.vocabularies_model', group=voc.Meta.group or '_', Model=voc.Meta.name) %}
  {%- set m_voc_caller = caller %} {# save caller value of this macro for use inside m_box #}
  {%- call m_box(title=voc.Meta.label, href=voc_url) %}
    {%- if items %}
      <form class="form" method="POST" action="{{ url_for('.vocabularies') }}">
        {{ csrf.field() }}
        {%- if voc.Meta.group %}
          <input type="hidden" name="group" value="{{ voc.Meta.group }}" />
          {%- endif %}
        <input type="hidden" name="Model" value="{{ voc.Meta.name }}" />

        {%- if edit_return_to %}
          <input type="hidden" name="return_to" value="{{ edit_return_to }}" />
        {%- endif %}

        <table class="table table-condensed table-striped">
          <thead>
            <tr>
              <th>{{ _('Label') }}</th>
              <th>{{ _('Default') }}</th>
              <th>{{ _('Active') }}</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {%- for item in items %}
              <tr>
                <td><a href="{{ url_for_voc_edit(item) }}">{{ item.label|safe }}</a></td>
                <td>{%- if item.default %}{{ icon_checked }}{%- endif %}</td>
                <td>{%- if item.active %}{{ icon_checked }}{%- endif %}</td>
                <td>
                  <button type="submit" name="down" value="{{ item.id }}" class="btn btn-inline{%- if loop.last %} disabled{%- endif %}">
                    <span class="caret-down"></span>
                  </button>

                  <button type="submit" name="up" value="{{ item.id }}" class="btn btn-inline{%- if loop.first %} disabled{%- endif %}">
                    <span class="caret-up"</span>
                  </button>
                </td>
              </tr>
            {%- endfor %}
          </tbody>
        </table>
      </form>
    {%- else %}
      <p class="text-muted text-center h3">
        <i>{{ _('No item in this vocabulary') }}</i>
      </p>
    {%- endif %}

    {{ m_voc_caller() }}
  {%- endcall %}
{% endmacro %}
