{% extends "base.html" %}
{% load i18n %}
{% load render_table from django_tables2 %}

{% block title %}{% trans "customers" %}{% endblock title %}
{% block customers %}<a class="nav-link active">{% trans "customers"|capfirst %}</a>{% endblock %}

{% block content %}
<div class="d-flex flex-column min-vh-100">
    <div class="d-flex flex-column flex-sm-grow-1 ms-sm-14 p-4">
      <main class="d-grid gap-4 p-1">
        <div class="row g-4">
          <div class="col-lg-6 col-xl-12">

        <div class="container-fluid p-2">
            <form method="get">
                <div class="input-group input-group-sm">
                    <button id="inputGroup-sizing-sm"
                            class="btn btn-sm btn-secondary rounded-start" type="submit">
                        {% trans 'search'|capfirst %}
                    </button>
                    <input type="text"
                           name="q"
                           class="form-control form-control-sm rounded-end"
                           value="{{ request.GET.q }}"
                           aria-describedby="inputGroup-sizing-sm"/>
                 <!-- Clear Button -->
        {% if request.GET.q %}
            <a href="{% url request.resolver_match.view_name %}"
               class="btn btn-sm btn-outline-danger ms-1 rounded">
                <i class="bi bi-x-lg"></i>
            </a>
        {% endif %}
                </div>

            </form>
        </div>

        <table class="table table-hover table-responsive-sm">
            <thead>
                <tr>
                    <th>{% trans 'first name'|capfirst %}</th>
                    <th>{% trans 'middle name'|capfirst %}</th>
                    <th>{% trans 'last name'|capfirst %}</th>
                    <th>{% trans 'national ID'|capfirst %}</th>
                    <th>{% trans 'actions'|capfirst %}</th>
                </tr>
            </thead>
            <tbody>
                {% for customer in customers %}
                <tr>
                    <td>{{ customer.first_name }}</td>
                    <td>{{ customer.middle_name }}</td>
                    <td>{{ customer.last_name }}</td>
                    <td>{{ customer.national_id }}</td>
                    <td>
                        <a class="btn btn-sm btn-success"
                           href="{% url 'customer_detail' customer.id %}">
                            {% trans 'view'|capfirst %}
                        </a>
                    </td>
                </tr>
                {% endfor %}

            </tbody>
        </table>
<!-- Optional: Pagination -->
  {% if is_paginated %}
  <nav aria-label="Page navigation">
    <ul class="pagination pagination-sm justify-content-center">
      {% if page_obj.has_previous %}
      <li class="page-item  py-0">
        <a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
          <span aria-hidden="true">&laquo;</span>
        </a>
      </li>
      {% else %}
      <li class="page-item disabled">
        <a class="page-link" href="#" aria-label="Previous">
          <span aria-hidden="true">&laquo;</span>
        </a>
      </li>
      {% endif %}
        {% for num in page_obj.paginator.page_range %}
            {% if page_obj.number == num %}
      <li class="page-item active"><a class="page-link" href="?page={{ num }}">{{ num }}</a></li>
      {% else %}
      <li class="page-item"><a class="page-link" href="?page={{ num }}">{{ num }}</a></li>
      {% endif %}
        {% endfor %} {% if page_obj.has_next %}
      <li class="page-item">
        <a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
          <span aria-hidden="true">&raquo;</span>
        </a>
      </li>
      {% else %}
      <li class="page-item disabled">
        <a class="page-link" href="#" aria-label="Next">
          <span aria-hidden="true">&raquo;</span>
        </a>
      </li>
      {% endif %}
    </ul>
  </nav>
  {% endif %}
        
</div>
        </div>
      </main>
    </div>
</div>
{% endblock %}

