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

{% block pagetitle %}Categories{% endblock %}
{% block page_header %}Categories{% 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="{{ request.GET.q }}" class="form-control form-control-sm" placeholder="Search...">
    </form>
    <a class="btn btn-primary btn-sm" href="{% url 'admin_category_create' %}">+ 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>Name</th>
          <th>Parent</th>
          <th class="text-end">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for c in items %}
        <tr>
          <td class="fw-semibold">{{ c.name }}</td>
          <td class="text-muted">{% if c.parent %}{{ c.parent.name }}{% else %}—{% endif %}</td>
          <td class="text-end">
            <a class="btn btn-outline-secondary btn-sm" href="{% url 'admin_category_edit' c.pk %}">Edit</a>
            <a class="btn btn-outline-danger btn-sm" href="{% url 'admin_category_delete' c.pk %}">Delete</a>
          </td>
        </tr>
        {% empty %}
        <tr>
          <td colspan="3" class="text-center text-muted py-4">No items found.</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>

{% if page_obj %}
<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 request.GET.q %}&q={{ request.GET.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 request.GET.q %}&q={{ request.GET.q }}{% endif %}">Next</a>
      </li>
    {% else %}
      <li class="page-item disabled"><span class="page-link">Next</span></li>
    {% endif %}
  </ul>
</nav>
{% endif %}
{% endblock %}
