{# templates/adminpanel/user_list.html #}
{% extends "adminpanel/base.html" %}

{% block pagetitle %}Users{% endblock %}
{% block page_header %}Users{% endblock %}
{% block header_actions %}
  <div class="d-flex align-items-center gap-2">
    <form method="get" action="" class="d-none d-md-flex">
      <input type="text" name="q" value="{{ q }}" class="form-control form-control-sm" placeholder="Search by name, email...">
    </form>
    <a class="btn btn-primary btn-sm" href="/dj-admin/auth/user/add/">+ Add</a>
  </div>
{% endblock %}

{% block content %}
<div class="mm-soft-card p-0">
  <div class="table-responsive">
    <table class="table table-hover table-sm m-0 align-middle">
      <thead class="table-light sticky-top">
        <tr>
          <th>Username</th>
          <th>Email</th>
          <th>Role</th>
          <th class="text-center">Status</th>
          <th class="text-end">Joined</th>
          <th class="text-end">Last login</th>
          <th class="text-end">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for u in page_obj.object_list %}
        <tr>
          <td class="fw-semibold">{{ u.username }}</td>
          <td class="text-muted">{{ u.email|default:"—" }}</td>
          <td>
            {% if u.is_superuser %}Superuser
            {% elif u.is_staff %}Staff
            {% else %}Customer{% endif %}
          </td>
          <td class="text-center">
            {% if u.is_active %}
              <span class="badge bg-success">Active</span>
            {% else %}
              <span class="badge bg-secondary">Inactive</span>
            {% endif %}
          </td>
          <td class="text-end"><span class="small">{{ u.date_joined|date:"Y-m-d H:i" }}</span></td>
          <td class="text-end">
            {% if u.last_login %}
              <span class="small">{{ u.last_login|date:"Y-m-d H:i" }}</span>
            {% else %}
              <span class="text-muted small">—</span>
            {% endif %}
          </td>
          <td class="text-end">
            <a class="btn btn-outline-secondary btn-sm" href="/dj-admin/auth/user/{{ u.id }}/change/">Open</a>
          </td>
        </tr>
        {% empty %}
        <tr>
          <td colspan="7" class="text-center text-muted py-4">No users found.</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>

<nav class="mt-3">
  <ul class="pagination pagination-sm mb-0">
    {% if page_obj.has_previous %}
      <li class="page-item">
        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if q %}&q={{ q }}{% endif %}">Prev</a>
      </li>
    {% else %}
      <li class="page-item disabled"><span class="page-link">Prev</span></li>
    {% endif %}

    <li class="page-item disabled">
      <span class="page-link">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span>
    </li>

    {% if page_obj.has_next %}
      <li class="page-item">
        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if q %}&q={{ q }}{% endif %}">Next</a>
      </li>
    {% else %}
      <li class="page-item disabled"><span class="page-link">Next</span></li>
    {% endif %}
  </ul>
</nav>
{% endblock %}
