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

<!--context['bap_club', 'bpp', 'userCanEdit'] + bap_genus_list context_object -->

<div class="container">
    <h3>{{ bap_club.name }}</h3>
    <p>
        All species are assigned BAP Points by genus. &nbsp;
        <i>You can optionally set individual species BAP Points using <b>species exceptions</i></b>.
    </p>
    <small>&nbsp;</small>   
    
    <!-- Filter Form -->
    <div class="card mb-4">
        <div class="card-header">
            <h4>BAP Genus 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>    

    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h4>BAP Genus Configurations</h4>
            <span>Total: {{ page_obj.paginator.count }} BAP Genus entries found</span>
        </div>
        <div class="card-body">
            {% if bap_genus_list %}
                <div class="table-responsive">
                    <table class="table table-striped table-hover">                    
                        <thead>
                            <tr>
                                <!-- th style="width: 40%">Genus</th-->
                                <th>Genus</th>
                                <th style='text-align:center'>Category</th>                                
                                <th style='text-align:center'>Points</th>
                                <th style='text-align:center'>Species Count</th>
                                <th style='text-align:center'>Species Exceptions</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for genus_entry in bap_genus_list %}
                                <tr>
                                    <td>{{ genus_entry.name }}</td>
                                    <td style='text-align:center'>{{ genus_entry.example_species.get_category_display }}</td>  
                                    {% if userCanEdit %}
                                        <td style='text-align:center'><a href="{% url 'editBapGenus' genus_entry.id %}">{{ genus_entry.points }}</a></td>  
                                    {% else %}
                                        <td style='text-align:center'>{{ genus_entry.points }}</td>  
                                    {% endif %}
                                    <td style='text-align:center'>{{ genus_entry.species_count }}</td>                                      
                                    <td style='text-align:center'><a href="{% url 'bapGenusSpecies' genus_entry.id %}">{{ genus_entry.species_override_count }}</a></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 %}
                <p>None</p>
            {% endif %}
        </div>
    </div>
                        
    {% if userCanEdit %}
        <div class="css_class='mb-3 section-bordered">
            <h4>Update BAP Genus List</h4>
            <p>Updating the BAP Genus List reads all currently defined Species and finds all <b>Genus</b> names not currently in the BAP Genus List.</p>
            <p>Newly added entries are assigned the default BAP Point value as defined in your club's configuration. </p>
            <p>
                <form method="post" action="{% url 'bapGenus' bap_club.id %}">
                    {% csrf_token %}
                    <input type="hidden" name="next" value="{{ request.path }}">
                    <button type="submit" class="btn btn-secondary">Update BAP Genus Set</button>
                </form>  
            </p>          
        </div>
    {% endif %}   

</div>

{% endblock content %}

