{% extends "templates/base.html" %}

{% block title %}{{ i18n('interface.resources.hierarchy_title') }}{% endblock %}
{% block container %}
  <h1>{{ self.title() }}</h1>

  <table class="table table-striped hierarchy">
    <thead>
      <tr>
        <th>{{ i18n('interface.common.id') }}</th>
        <th>{{ i18n('interface.common.name') }}</th>
        <th>{{ i18n('interface.common.type') }}</th>
        <th>{{ i18n('interface.resources.permissions') }}</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
    {% for item in items %}
      {% set resource = item['resource'] %}
      <tr class="{{ 'success' if selected_item_id == resource[pkey] else '' }}">
        <td>{{ resource.id }}</td>
        <td>
          {% for d in range(0, item['depth']) %}
            <span class="item-offset">&nbsp;</span>
          {% endfor %}
          {{ utils.render_icon('chevron-right') }} {{ resource.name }}
        </td>
        <td>{{ resource_types[resource.type] }}</td>
        <td>
          {% if item['permissions'] %}
            {% for permission in item['permissions']%}
              {{ permission.role }} {{ utils.render_icon('pencil') if permission.write }}<br/>
            {% endfor %}
            <a href="{{ url_for('permissions', resource_id=resource.id) }}" class="btn btn-default btn-xs" role="button" title="{{ i18n('interface.resources.resource_permissions') }}">
              {{ utils.render_icon('lock') }}
            </a>
          {% endif %}
        </td>
        <td>
          <a href="{{ url_for('edit_%s' % endpoint_suffix, id=resource[pkey]) }}" class="btn btn-primary" role="button">
            {{ utils.render_icon('pencil') }} {{ i18n('interface.common.edit') }}
          </a>
          <a href="{{ url_for('new_permission', resource_id=resource['id']) }}" class="btn btn-success" role="button">
            {{ utils.render_icon('plus-lg') }} {{ i18n('interface.common.new_permission') }}
          </a>
          <form action="{{ url_for('destroy_%s' % endpoint_suffix, id=resource[pkey]) }}" method="post" style="display: inline;">
            <input type="hidden" name="_method" value="DELETE" />
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
            <button type="submit" class="btn btn-danger" onclick="return confirm('{{ i18n('interface.common.confirm_message_remove') }}');">
              {{ utils.render_icon('x') }} {{ i18n('interface.common.delete') }}
            </button>
          </form>
          <form action="{{ url_for('destroy_cascaded_%s' % endpoint_suffix, id=resource[pkey]) }}" method="post" style="display: inline;">
            <input type="hidden" name="_method" value="DELETE" />
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
            <button type="submit" class="btn btn-danger" onclick="return confirm('{{ i18n('interface.resources.confirm_message_remove_cascaded') }}');">
              {{ utils.render_icon('x') }} {{ i18n('interface.resources.remove_cascaded') }}
            </button>
          </form>
        </td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
{% endblock %}
