{% extends "layout.html" %}

{% block content_header %}
  {{ build_breadcrumbs(current_breadcrumbs + [
    { 'name': resource.name, 'link': url_for('.PolicyView:get', id=resource.id), 'icon': 'lock' }
  ]) }}
{% endblock %}

{% block content %}
  {% call build_section_row() %}
    {% call build_form_tabs_box() %}
      {% call build_tabs_navigation() %}
        {{ add_tab_navigation_header(_('Policy'), resource.name, 'lock') }}
        {{ add_tab_navigation_item('policy', _('Policy'), active=True) }}
        {{ add_tab_navigation_item('acl', _('ACL')) }}
      {% endcall %}

      {% call build_form(action=url_for('.PolicyView:put', id=resource.uuid)) %}
        {% call build_tabs_content() %}

          {% call build_tab_content_item('policy', active=True) %}
            {% call add_default_fields(form=form, submit_value=_('Update')) %}
              {{ render_field(form.name) }}
            {% endcall %}
          {% endcall %}

          {% call build_tab_content_item('acl') %}
            {{ build_add_row_entry_header() }}
            {% call add_default_fields(form=form, submit_value=_('Update')) %}
              {% call build_table() %}

                {% call build_table_headers() %}
                  <th>{{  _('acl') }}</th>
                  <th style="width: 21px"></th>
                {% endcall %}

                {% call build_table_body(class_='dynamic-table') %}
                  {% do form.acl.append_entry() %}
                  {{ _build_access_entry(form.acl.pop_entry(), template=True) }}

                  {% for access in form.acl %}
                    {{ _build_access_entry(access) }}
                  {% endfor %}
                {% endcall %}

              {% endcall %}
            {% endcall %}

            {{ build_add_row_entry_header() }}
          {% endcall %}

        {% endcall %}
      {% endcall %}
    {% endcall %}
  {% endcall %}
{% endblock %}

{% macro _build_access_entry(option, template=False) %}
  {% if template %}
    {% set tr_class = "row-template hidden" %}
  {% else %}
    {% set tr_class = "row-line" %}
  {% endif %}
  <tr class="{{ tr_class }}">
    <td>{{ render_field(option.value, with_label=False) }}</td>
    <td>{{ add_delete_entry_button() }}</td>
  </tr>
{% endmacro %}

