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

<!--context['bap_club'] + bap_species_list context_object -->

<div class="container">
    <h3>{{ bap_club.name }}</h3>
    <div style="display: flex; justify-content: space-between; align-items: center;">
        <p>The following BAP <b><i>species exceptions</i></b> have been set. &nbsp;&nbsp;<i>Species not listed use default Genus points.</i></p>
        <span><a class="btn btn-secondary" href="{% url 'bapGenus' bap_club.id %}" role="button">&nbsp;BAP Genus List&nbsp;</a></span>
    </div>
    <small>&nbsp;</small> 

    <!-- Filter Form -->
    <div class="card mb-4">
        <div class="card-header">
            <h4>BAP Species Filter</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()">
                            <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>
                <!-- 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>

    <!-- bap_species_list -->
    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h4>BAP Species Exceptions</h4>
            <span>Total: {{ page_obj.paginator.count }} BAP Genus entries found</span>
        </div>
        <div class="card-body">
            {% if bap_species_list %}
                <div class="table-responsive">
                    <table class="table table-striped table-hover">                    
                        <thead>
                            <tr>
                                <!--th style="width: 40%">Species</th-->
                                <th>Species</th>
                                <th style='text-align:center'>Category</th> 
                                <th style='text-align:center'>CARES</th> 
                                <th style='text-align:center'>Points</th> 
                            </tr>
                        </thead>
                        <tbody>
                            {% for species_entry in bap_species_list %}
                                <tr>
                                    <td><a href="{% url 'speciesInstance' species_entry.id %}">{{species_entry.name}}</a></td>
                                    <td style='text-align:center'>{{ species_entry.species.get_category_display }}</td> 
                                    {% if userCanEdit %}
                                        {% if species_entry.species.render_cares %}
                                            <td style='text-align:center'>Yes</td>  
                                            <td style='text-align:center'><a href="{% url 'editBapSpecies' species_entry.id %}">{{species_entry.points}}</a> x {{bap_club.cares_muliplier}}</td>  
                                        {% else %}
                                            <td></td> 
                                            <td style='text-align:center'><a href="{% url 'editBapSpecies' species_entry.id %}">{{species_entry.points}}</a></td>  
                                        {% endif %}
                                    {% else %}
                                        <td></td>  
                                        {% if species_entry.species.render_cares %}
                                            <td style='text-align:center'>{{species_entry.points}} x {{bap_club.cares_muliplier}}</td>  
                                        {% else %}
                                            <td style='text-align:center'>{{ species_entry.points }}</td>  
                                        {% endif %}
                                    {% 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_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 %}
                <p>None</p>
            {% endif %}
        </div>
    </div>
</div>

{% endblock content %}

