{% 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">Advanced Voter Search</h1>
        <a href="{% url 'voter_list' %}" class="btn btn-outline-secondary btn-sm">Back to Registry</a>
    </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-4">
                    <label class="form-label small fw-bold text-muted">First Name</label>
                    {{ form.first_name }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">Last Name</label>
                    {{ form.last_name }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">Voter ID</label>
                    {{ form.voter_id }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">Birth Month</label>
                    {{ form.birth_month }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">City</label>
                    {{ form.city }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">Zip Code</label>
                    {{ form.zip_code }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">District</label>
                    {{ form.district }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">Precinct</label>
                    {{ form.precinct }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">Phone Type</label>
                    {{ form.phone_type }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">Candidate Support</label>
                    {{ form.candidate_support }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">Yard Sign</label>
                    {{ form.yard_sign }}
                </div>
                <div class="col-md-4">
                    <label class="form-label small fw-bold text-muted">Window Sticker</label>
                    {{ form.window_sticker }}
                </div>
                <div class="col-md-4 d-flex align-items-end">
                    <div class="form-check mb-2">
                        {{ form.is_targeted }}
                        <label class="form-check-label ms-1" for="{{ form.is_targeted.id_for_label }}">
                            Targeted Only
                        </label>
                    </div>
                </div>
                <div class="col-12 text-end">
                    <a href="{% url 'voter_advanced_search' %}" class="btn btn-light me-2">Clear Filters</a>
                    <button type="submit" class="btn btn-primary px-4">Search Voters</button>
                </div>
            </form>
        </div>
    </div>

    <form id="bulk-action-form" action="{% url 'export_voters_csv' %}" method="POST">
        {% csrf_token %}
        <!-- Hidden inputs to pass search filters for "Export All" scenario -->
        {% for key, value in request.GET.items %}
            {% if key != 'csrfmiddlewaretoken' %}
            <input type="hidden" name="filter_{{ key }}" value="{{ value }}">
            {% endif %}
        {% endfor %}

        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white py-3 border-0 d-flex justify-content-between align-items-center">
                <h5 class="mb-0 fw-bold">Search Results ({{ voters.count }})</h5>
                <div id="bulk-actions" class="d-none">
                    <button type="submit" name="action" value="export_selected" class="btn btn-success btn-sm me-2">
                        <i class="bi bi-file-earmark-spreadsheet me-1"></i> Export Selected
                    </button>
                </div>
                <div>
                    <button type="submit" name="action" value="export_all" class="btn btn-outline-success btn-sm">
                        <i class="bi bi-file-earmark-spreadsheet me-1"></i> Export All Results
                    </button>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table table-hover mb-0 align-middle">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4" style="width: 40px;">
                                <input type="checkbox" class="form-check-input" id="select-all">
                            </th>
                            <th>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">
                                <input type="checkbox" name="selected_voters" value="{{ voter.id }}" class="form-check-input voter-checkbox">
                            </td>
                            <td>
                                <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.phone %}
                                <div class="small text-muted">{{ voter.get_phone_type_display }}</div>
                                {% 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="6" class="text-center py-5 text-muted">
                                <p class="mb-0">No voters found matching your search criteria.</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </form>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const selectAll = document.getElementById('select-all');
    const checkboxes = document.querySelectorAll('.voter-checkbox');
    const bulkActions = document.getElementById('bulk-actions');

    function updateBulkActionsVisibility() {
        const checkedCount = document.querySelectorAll('.voter-checkbox:checked').length;
        if (checkedCount > 0) {
            bulkActions.classList.remove('d-none');
        } else {
            bulkActions.classList.add('d-none');
        }
    }

    if (selectAll) {
        selectAll.addEventListener('change', function() {
            checkboxes.forEach(cb => {
                cb.checked = selectAll.checked;
            });
            updateBulkActionsVisibility();
        });
    }

    checkboxes.forEach(cb => {
        cb.addEventListener('change', function() {
            const allChecked = Array.from(checkboxes).every(c => c.checked);
            selectAll.checked = allChecked;
            updateBulkActionsVisibility();
        });
    });
});
</script>
{% endblock %}