{% extends "base.html" %}

{% block content %}
<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h2">Voter Registry</h1>
        <div class="d-flex gap-2">
            <a href="{% url 'voter_advanced_search' %}" class="btn btn-outline-primary btn-sm">Advanced Search</a>
            <a href="/admin/core/voter/add/" class="btn btn-primary btn-sm">+ Add New Voter</a>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body p-4">
            <form action="." method="GET" class="row g-3">
                <div class="col-md-10">
                    <input type="text" name="q" class="form-control" placeholder="Search by name or Voter ID..." value="{{ query|default:'' }}">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">Filter</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="table-responsive">
            <table class="table table-hover mb-0 align-middle">
                <thead class="bg-light">
                    <tr>
                        <th class="ps-4">Name</th>
                        <th>District</th>
                        <th>Phone</th>
                        <th>Target Voter</th>
                        <th class="pe-4">Supporter</th>
                    </tr>
                </thead>
                <tbody>
                    {% for voter in voters %}
                    <tr>
                        <td class="ps-4">
                            <a href="{% url 'voter_detail' voter.id %}" class="fw-semibold text-primary text-decoration-none d-block">
                                {{ voter.first_name }} {{ voter.last_name }}
                            </a>
                            <div class="small text-muted">{{ voter.address|default:"No address provided" }}</div>
                        </td>
                        <td><span class="badge bg-light text-dark border">{{ voter.district|default:"-" }}</span></td>
                                                <td>
                            {{ voter.phone|default:"-" }}
                            {% if voter.secondary_phone %}
                                <br><small class="text-muted">{{ voter.secondary_phone }}</small>
                            {% endif %}
                        </td>
                        <td>
                            {% if voter.is_targeted %}
                                <span class="badge bg-primary">Yes</span>
                            {% else %}
                                <span class="badge bg-secondary-subtle text-secondary">No</span>
                            {% endif %}
                        </td>
                        <td class="pe-4">
                            {% if voter.candidate_support == 'supporting' %}
                                <span class="badge bg-success">Supporting</span>
                            {% elif voter.candidate_support == 'not_supporting' %}
                                <span class="badge bg-danger">Not Supporting</span>
                            {% else %}
                                <span class="badge bg-secondary">Unknown</span>
                            {% endif %}
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="5" class="text-center py-5 text-muted">
                            <p class="mb-0">No voters found matching your search.</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        
        {% if voters.paginator.num_pages > 1 %}
        <div class="card-footer bg-white border-0 py-3">
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center mb-0">
                    {% if voters.has_previous %}
                    <li class="page-item">
                        <a class="page-item" href="?page=1{% if query %}&q={{ query }}{% endif %}" aria-label="First">
                            <span class="page-link">&laquo;&laquo;</span>
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ voters.previous_page_number }}{% if query %}&q={{ query }}{% endif %}" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    {% endif %}

                    <li class="page-item active"><span class="page-link">{{ voters.number }} of {{ voters.paginator.num_pages }}</span></li>

                    {% if voters.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ voters.next_page_number }}{% if query %}&q={{ query }}{% endif %}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ voters.paginator.num_pages }}{% if query %}&q={{ query }}{% endif %}" aria-label="Last">
                            <span aria-hidden="true">&raquo;&raquo;</span>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}
