{#
  Copyright (C) 2024 I-Form.

  invenio-theme-iform is free software; you can redistribute it and/or modify it
  under the terms of the MIT License; see LICENSE file for more details.
#}
{% extends "invenio_administration/base.html" %}

{% block admin_page_content %}
<div class="ui container dashboard rel-mt-5">
  <div class="ui grid">
    <div class="sixteen wide column">
      <h2 class="ui header">
        <i class="users icon"></i>
        <div class="content">
          {{ _("Role Assignment") }}
          <div class="sub header">{{ _("Select a role and enable or disable it for users.") }}</div>
        </div>
      </h2>

      {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
          {% for category, message in messages %}
            <div class="ui {{ 'positive' if category == 'success' else 'negative' }} message">
              {{ message }}
            </div>
          {% endfor %}
        {% endif %}
      {% endwith %}

      <div class="ui segment">
        <form method="GET" class="ui form">
          <div class="field">
            <label>{{ _("Select a Role") }}</label>
            <div class="ui action input">
              <select name="role_id" class="ui dropdown">
                <option value="">{{ _("-- Select a role --") }}</option>
                {% for r in roles %}
                  <option value="{{ r.id }}" {% if selected_role and selected_role.id == r.id %}selected{% endif %}>
                    {{ r.name }}
                  </option>
                {% endfor %}
              </select>
              <button class="ui button primary" type="submit">{{ _("Select") }}</button>
            </div>
          </div>
        </form>
      </div>

      {% if selected_role %}
      <div class="ui segment">
        <h3 class="ui header">
          {{ _("Users assigned to:") }} {{ selected_role.name }}
          {% if selected_role.description %}
          <div class="sub header" style="margin-top: 5px;">
            <i class="info circle icon"></i> {{ selected_role.description }}
          </div>
          {% endif %}
        </h3>
        <table class="ui celled striped table">
          <thead>
            <tr>
              <th>{{ _("Email") }}</th>
              <th>{{ _("Active") }}</th>
              <th class="center aligned">{{ _("Has Role?") }}</th>
              <th class="center aligned">{{ _("Actions") }}</th>
            </tr>
          </thead>
          <tbody>
            {% for user in users %}
            <tr>
              <td>{{ user.email }}</td>
              <td>
                {% if user.active %}
                  <div class="ui green horizontal label">{{ _("Active") }}</div>
                {% else %}
                  <div class="ui grey horizontal label">{{ _("Inactive") }}</div>
                {% endif %}
              </td>
              
              {% set has_role = selected_role in user.roles %}
              <td class="center aligned">
                {% if has_role %}
                  <i class="green check large icon"></i>
                {% else %}
                  <i class="red times large icon"></i>
                {% endif %}
              </td>
              
              <td class="center aligned">
                <form method="POST" style="display:inline;">
                  <input type="hidden" name="user_id" value="{{ user.id }}">
                  <input type="hidden" name="role_id" value="{{ selected_role.id }}">
                  {% if has_role %}
                    <input type="hidden" name="action" value="disable">
                    <button class="ui tiny red button" type="submit">
                      <i class="minus icon"></i> {{ _("Disable") }}
                    </button>
                  {% else %}
                    <input type="hidden" name="action" value="enable">
                    <button class="ui tiny green button" type="submit">
                      <i class="plus icon"></i> {{ _("Enable") }}
                    </button>
                  {% endif %}
                </form>
              </td>
            </tr>
            {% else %}
            <tr>
              <td colspan="4" class="center aligned">{{ _("No users found.") }}</td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      {% endif %}
    </div>
  </div>
</div>
{% endblock %}
