{% extends "base.html" %}

{% block title %}Company List{% endblock %}

{% block content %}
<div class="container">
    <h2 class="my-4">Company List</h2>

    <!-- Search Form -->
    <form method="GET" action="" class="mb-3">
        <div class="input-group">
            <input type="text" name="q" class="form-control" placeholder="Search customers..." value="{{ query }}">
            <button type="submit" class="btn btn-primary">Search</button>
        </div>
    </form>

    <a href="{% url 'add_company' %}" class="btn btn-success mb-3">Add New Company</a>

    <!-- Customer Table -->
    <table class="table table-striped table-bordered">
        <thead class="table-dark">
            <tr>
                
                <th>Company</th>
                <th>Contact</th>
                <th>Email</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for company in page_obj %}
            <tr>
                
                <td>{{ company.company_name }}</td>
                <td>{{ company.phone }}</td>
                <td>{{ company.email }}</td>
                <td>
                    <a href="{% url 'edit_company' company.id %}" class="btn btn-primary btn-sm">View / Edit</a>
                    <a href="{% url 'delete_company' company.id %}" class="btn btn-danger btn-sm">Delete</a>
                </td>
            </tr>
            {% empty %}
            <tr>
                <td colspan="4" class="text-center">No customers found.</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    <!-- Pagination Controls -->
    <nav>
        <ul class="pagination">
            {% if page_obj.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?q={{ query }}&page=1">First</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?q={{ query }}&page={{ page_obj.previous_page_number }}">Previous</a>
                </li>
            {% endif %}

            <li class="page-item active">
                <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="?q={{ query }}&page={{ page_obj.next_page_number }}">Next</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?q={{ query }}&page={{ page_obj.paginator.num_pages }}">Last</a>
                </li>
            {% endif %}
        </ul>
    </nav>
</div>
{% endblock %}
