{% extends 'main.html' %}
{% load static %}
{% block content %}

<div class="container">
    <h1 class="my-4">CARES Priority List: <i>Species Search</i></h1>
    
    <!-- Filter Form -->
    <div class="card mb-4">
        <div class="card-header">
            <h4>Search Filters</h4>
        </div>
        <div class="card-body">
            <form method="get" id="filter-form">
                <div class="row mb-3">
                    <div class="col-md-5">
                        <label for="cares_family">CARES Family:</label>
                        <!--select name="cares_family" id="cares_family" class="form-select" onchange="this.form.submit()"-->
                        <select name="cares_family" id="cares_family" class="form-select">
                            <option value="">-- All Families --</option>
                            {% for value, label in cares_families %}
                                <option value="{{ value }}" {% if value == selected_cares_family %}selected{% endif %}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-5">
                        <label for="global_region">Global Region:</label>
                        <!--select name="global_region" id="global_region" class="form-select" onchange="this.form.submit()"-->
                        <select name="global_region" id="global_region" class="form-select">
                            <option value="">-- All Regions --</option>
                            {% for value, label in global_regions %}
                                <option value="{{ value }}" {% if value == selected_region %}selected{% endif %}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-8">
                        <label for="q">Search Species:</label>
                        <input type="text" class="form-control" name="q" id="q" placeholder="Search name, distribution, or description..." value="{{ query_text }}">
                        <small class="form-text text-muted">Press Enter to search</small>
                    </div>
                    <div class="col-md-2">
                        <!--button type="button" id="clear-category-region" class="btn btn-secondary mt-4">Clear Filters</button-->
                        <button type="button" id="clear-filters" class="btn btn-secondary mt-4" onclick="window.location.href='{% url 'caresSpeciesSearch' %}'">Clear Filters</button>
                    </div>                    
                </div>
                
                <!-- Hidden fields to preserve pagination when only clearing specific filters -->
                <input type="hidden" id="preservedPage" name="page" value="{{ request.GET.page|default:'' }}">
            </form>
        </div>
    </div>
    
    <!-- Species List -->
    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h4>Species</h4>
            <span>Total: {{ page_obj.paginator.count }} species found</span>
        </div>
        <div class="card-body">
            {% if species_list %}
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th style='text-align:center'>CARES Family</th>
                                <th style='text-align:center'>Risk Classification</th>
                                <th style='text-align:center'>Global Region</th>
                                <th style='text-align:center'>
                                    {% if userCanEdit %}
                                        Registrations
                                    {% else %}
                                        Description
                                    {% endif %}
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for species in species_list %}
                                <tr>
                                    <td>
                                        <a href="{% url 'caresSpecies' species.id %}">{{species.name}}</a>
                                    </td>
                                    <td style='text-align:center'>{{ species.get_cares_family_display }}</td>
                                    <td style='text-align:center'>{{ species.get_cares_classification_display }}</td>
                                    <td style='text-align:center'>{{ species.get_global_region_display }}</td>
                                    
                                    {% if userCanEdit %}
                                        <td style='text-align:center'>
                                            {% if species.species_registrations.all.count > 0 %}
                                                {{ species.species_registrations.all.count }} 
                                            {% else %}
                                                &nbsp;
                                            {% endif %}
                                        </td>
                                    {% else %}
                                        <td style='text-align:left'>
                                            {{ species.description|truncatewords:12 }}
                                        </td>                                    
                                    {% endif %}
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                {% if is_paginated %}
                <nav aria-label="Page navigation">
                    <ul class="pagination justify-content-center">
                        {% if page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page=1{% if selected_cares_family %}&cares_family={{ selected_cares_family }}{% endif %}{% if selected_region %}&global_region={{ selected_region }}{% endif %}{% if query_text %}&q={{ query_text }}{% endif %}" aria-label="First">
                                    <span aria-hidden="true">&laquo;&laquo;</span>
                                </a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if selected_cares_family %}&cares_family={{ selected_cares_family }}{% endif %}{% if selected_region %}&global_region={{ selected_region }}{% endif %}{% if query_text %}&q={{ query_text }}{% endif %}" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                        {% endif %}
                        
                        {# Show current page and neighboring pages #}
                        {% for i in page_obj.paginator.page_range %}
                            {% if page_obj.number == i %}
                                <li class="page-item active"><span class="page-link">{{ i }}</span></li>
                            {% elif i > page_obj.number|add:'-3' and i < page_obj.number|add:'3' %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ i }}{% if selected_cares_family %}&cares_family={{ selected_cares_family }}{% endif %}{% if selected_region %}&global_region={{ selected_region }}{% endif %}{% if query_text %}&q={{ query_text }}{% endif %}">{{ i }}</a>
                                </li>
                            {% endif %}
                        {% endfor %}
                        
                        {% if page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if selected_cares_family %}&cares_family={{ selected_cares_family }}{% endif %}{% if selected_region %}&global_region={{ selected_region }}{% endif %}{% if query_text %}&q={{ query_text }}{% endif %}" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if selected_cares_family %}&cares_family={{ selected_cares_family }}{% endif %}{% if selected_region %}&global_region={{ selected_region }}{% endif %}{% if query_text %}&q={{ query_text }}{% endif %}" aria-label="Last">
                                    <span aria-hidden="true">&raquo;&raquo;</span>
                                </a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
                {% endif %}
            {% else %}
                <div>
                    <h3  style="text-align:center;">None Found</h3>
                    {% if request.user.is_authenticated %}
                        <p style="text-align:center;"><a href="{% url 'createCaresSpecies' %}">Add CARES Species</a></p>
                    {% endif %}
                </div>
            {% endif %}
        </div>
    </div>
</div>

<script>
    // Reset page to 1 when any filter changes
    function resetPageAndSubmit() {
        document.getElementById('preservedPage').value = '1';
        document.getElementById('filter-form').submit();
    }
    
    // Reset page when dropdowns change
    document.getElementById('cares_family').addEventListener('change', function() {
        resetPageAndSubmit();
    });
    
    document.getElementById('global_region').addEventListener('change', function() {
        resetPageAndSubmit();
    });
    
    // Make Enter key work for text search and reset page
    document.getElementById('q').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            e.preventDefault();
            resetPageAndSubmit();
        }
    });
</script>

{% endblock content %}
