{% from "_helpers.html" import copr_url %}


{% macro permissions_table(permissions, current_user_permissions, copr, permissions_applier_form, permissions_form) %}
  {% if permissions or g.user != copr.user %} {# display the whole table if there are permissions or user can ask for them #}
    {% if permissions_applier_form and g.user %}
  <form action="{{ copr_url('coprs_ns.copr_permissions_applier_change', copr)}}" method=post>
      {{ permissions_applier_form.csrf_token }}
    {% endif %}
    {% if permissions_form and g.user %}
  <form action="{{ copr_url('coprs_ns.copr_update_permissions', copr)}}" method=post>
      {{ permissions_form.csrf_token }}
    {% endif %}
    <!--permissions-table-->
    <table class="table table-striped table-bordered permissions">
      <thead>
        <tr>
          <th>Username</th>
          <th>Is Builder</th>
          <th>Is Admin</th>
        </tr>
      </thead>
      <tbody>
      {% for perm in permissions %}
        {% if perm.user_id != g.user.id %} {# if user is logged in, only display his form below, not a row #}
          {{ permissions_table_row_other_user(perm, permissions_applier_form, permissions_form) }}
        {% endif %}
      {% endfor %}
      {{ permissions_table_row_current_user(current_user_permissions, permissions_applier_form, permissions_form) }}
    </table>
    {% set show_form = g.user and (permissions_applier_form or permissions_form)  %}
    {% if show_form %}
      <input class="btn btn-primary pull-right" type="submit" value="{% if g.user.can_edit(copr) %}Update{% else %}Request{% endif %}">
    {% endif %}
  </form>
  {% else %}
    No permissions for other users for this Copr.
  {% endif %}
{% endmacro %}

{% macro permissions_table_row_other_user(perm, permissions_applier_form, permissions_form) %}
  <tr>
    <td>{{ perm.user.name }}</td>
    <td>
      {% if permissions_form %}
        {% if perm.copr_builder != 0 %}
          {{ permissions_form['copr_builder_{0}'.format(perm.user.id)] }}
        {% endif %}
      {% else %}
        {{ perm.copr_builder|perm_type_from_num }}
      {% endif %}
    </td>
    <td>
      {% if permissions_form %}
        {% if perm.copr_admin != 0 %}
          {{ permissions_form['copr_admin_{0}'.format(perm.user.id)] }}
        {% endif %}
      {% else %}
        {{ perm.copr_admin|perm_type_from_num }}
      {% endif %}
    </td>
  </tr>
{% endmacro %}

{% macro permissions_table_row_current_user(current_user_permissions, permissions_applier_form, permissions_form) %}
  {# if user is logged in and permissions_applier_form is defined, display it #}
  {% if g.user and permissions_applier_form %}
    <tr>
      <td>{{ g.user.name }}</td>
      <td>
        {% if current_user_permissions %}
          {{ current_user_permissions.copr_builder|perm_type_from_num }}
        {% else %}
          Not requested
        {% endif %}
        <br>
        {{ permissions_applier_form.copr_builder|safe }}
      </td>
      <td>
        {% if current_user_permissions %}
          {{ current_user_permissions.copr_admin|perm_type_from_num }}
        {% else %}
          Not requested
        {% endif %}
        <br>
        {{ permissions_applier_form.copr_admin|safe }}
    </tr>
  {% endif %}

  {# if user is admin (means current_user_permissions is set), display his own permissions for changing #}
  {% if g.user and permissions_form and current_user_permissions %}
    <tr>
      <td>{{ g.user.name }}</td>
      <td>
        {{ permissions_form['copr_builder_{0}'.format(g.user.id)] }}
      </td>
      <td>
        {{ permissions_form['copr_admin_{0}'.format(g.user.id)] }}
      </td>
    </tr>
  {% endif %}
{% endmacro %}
