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

<div class="container">
    <h1 class="my-4">Search Species</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="category">Category:</label>
                        <!--select name="category" id="category" class="form-select" onchange="this.form.submit()"-->
                        <select name="category" id="category" class="form-select">
                            <option value="">-- All Categories --</option>
                            {% for value, label in categories %}
                                <option value="{{ value }}" {% if value == selected_category %}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>
                
                <!-- Text search -->
                <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-filters" class="btn btn-secondary mt-4" onclick="window.location.href='{% url 'speciesSearch' %}'">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'>Aquarists</th>
                                <th style='text-align:center'>Category</th>
                                <th style='text-align:center'>Region</th>
                                <th>Description</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for species in species_list %}
                                <tr>
                                    <td>
                                        <a href="{% url 'species' species.id %}">{{ species.name }}</a>
                                        {% if species.render_cares %}
                                            <font style="color:#860404;">CARES</font>
                                        {% endif %}
                                    </td>
                                    <td style='text-align:center'>
                                        {% if species.instance_count %}
                                            {{ species.instance_count }}
                                        {% else %}
                                            &nbsp;
                                        {% endif %}
                                    </td>
                                    <td style='text-align:center'>{{ species.get_category_display }}</td>
                                    <td style='text-align:center'>{{ species.get_global_region_display }}</td>
                                    <td>{{ species.description|truncatewords:10 }}</td>
                                </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_category %}&category={{ selected_category }}{% 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_category %}&category={{ selected_category }}{% 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_category %}&category={{ selected_category }}{% 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_category %}&category={{ selected_category }}{% 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_category %}&category={{ selected_category }}{% 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 class="alert alert-info">No species found matching your criteria.</div-->
                <div>
                    <h3  style="text-align:center;">Species Not Found</h3>
                    <div>
                        <p style="text-align:center;"><b>Your species search returned no results.</b> This can happen if the <i>Species</i> does not yet exist in the database.</p> 
                        <p style="text-align:center;">It can also happen if you misspelled the <i>Species</i> name or entered additional search keywords not found. 
                        <p style="text-align:center;"><b>Tip:</b> <i>Try entering partial text.</i> For example 'apisto' brings up all Apistogramma species in the database.</p>
                        <p style="text-align:center;">If you'd like help adding your species you can use the <a href="{% url 'addSpeciesInstanceWizard1'%}">Add My Species Guided Tutorial</a>.</p>
                        <p style="text-align:center;">If you're confident the species needs to be added you can use <a href="{% url 'createSpecies'%}">Add Species</a> now.</p>
                    </div>
                    <div>
                        <p>&nbsp;</p>
                        <p style="text-align:center;"><a href="{% url 'speciesSearch'%}"><img src={% static 'images\ASN_WhereAquaristMeet_02.jpg' %}></a></p>
                    </div>
                </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('category').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 %}
