{% extends "framework_modern.html" %}

{% block content %}

  {% block action_modals %}
    {% include "modals/objects/edit_group_modal.html" %}
    {% include "modals/objects/edit_group_member_modal.html" %}
    {% include "modals/objects/generic_delete_modal.html" %}
  {% endblock action_modals %}

  <div class="space-y-2 p-4">
    <!-- Header Section -->
    <div class="card bg-base-100">
      <div class="card-body">
        <div class="flex items-center justify-between">
          <h2 class="card-title text-2xl">{{ group.name }}</h2>
          <div id="actionsButton" class="dropdown dropdown-end hidden">
            <div tabindex="0" role="button" class="btn btn-ghost btn-sm">
              <svg
                xmlns="http://www.w3.org/2000/svg"
                width="16"
                height="16"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                class="lucide lucide-wrench"
              >
                <path
                  d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"
                />
              </svg>
              Actions
            </div>
            <ul
              tabindex="-1"
              class="dropdown-content menu bg-base-100 rounded-box z-50 w-52 p-2"
            >
              {% block actions %}
                {% include "actions/objects/group.html" %}
              {% endblock %}
            </ul>
          </div>
        </div>
        <p class="mt-2">{{ group.description }}</p>
      </div>
    </div>

    <!-- Members -->
    <div class="card bg-base-100">
      <div class="card-body">
        <h3 class="card-title mb-4 text-xl">Members</h3>
        <p class="mb-4">List of members of this group</p>
        <ul class="menu bg-base-200 rounded-box">
          {% for um in group.user_member.all %}
            <li>
              <a
                href="{% if user.is_superuser %}{{ um.url }}{% else %}{{ "#" }}{% endif %}"
                class="hover:bg-base-300"
                >{{ um.username }}
                {% if not um.is_active %}<i>(inactive)</i>{% endif %}</a
              >
            </li>
          {% endfor %}
          {% for gm in group.group_member.all %}
            <li>
              <a href="{{ gm.url }}" class="hover:bg-base-300">{{ gm.name }}</a>
            </li>
          {% endfor %}
        </ul>
      </div>
    </div>

    <!-- Packages -->
    <div class="card bg-base-100">
      <div class="card-body">
        <h3 class="card-title mb-4 text-xl">Packages</h3>
        <p class="mb-4">Packages where this group has access to</p>
        <ul class="menu bg-base-200 rounded-box">
          {% for p in packages %}
            <li>
              <a href="{{ p.url }}" class="hover:bg-base-300">{{ p.name }}</a>
            </li>
          {% endfor %}
        </ul>
      </div>
    </div>
  </div>

  <script>
    // Show actions button if there are actions
    document.addEventListener("DOMContentLoaded", function () {
      const actionsButton = document.getElementById("actionsButton");
      const actionsList = actionsButton?.querySelector("ul");
      if (actionsList && actionsList.children.length > 0) {
        actionsButton?.classList.remove("hidden");
      }
    });
  </script>
{% endblock content %}
