{% extends 'main.html' %}

{% block content %}
<div class="container">
    <h1 class="my-4">{{club.acronym}} CARES Liaison Dashboard</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="member">Member:</label>
                        <select name="member" id="member" class="form-select">
                            <option value="">-- All Members --</option>
                            {% for value, label in club_members %}
                                <option value="{{ value }}" {% if value|stringformat:"s" == selected_member %}selected{% endif %}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-5">
                        <label for="species_kept">Species:</label>
                        <select name="species_kept" id="species_kept" class="form-select">
                            <option value="">-- All Species --</option>
                            {% for value, label in species_kept_list %}
                                <option value="{{ value }}" {% if value|stringformat:"s" == selected_species_kept %}selected{% endif %}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <!--div class="col-md-2 d-flex align-items-end"><button type="submit" class="btn btn-primary w-100">Apply 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>
            {% if selected_member or selected_species_kept %}
                <div class="mt-2">
                    <a href="{% url request.resolver_match.url_name pk=club.id %}" class="btn btn-secondary btn-sm">Clear Filters</a>
                </div>
            {% endif %}
        </div>
    </div>
    
    <!-- member_cares_species_list -->
    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h4>CARES Species Kept:</h4>
            <span>Total: {{ page_obj.paginator.count }} species kept</span>
        </div>
        <div class="card-body">
            {% if member_cares_species_list %}
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>Club Member</th>
                                <th style='text-align:left'>Species Kept</th>
                                <th style='text-align:left'>Risk Classification</th>
                                <th style='text-align:center'>Breeding Colony?</th>
                                <th style='text-align:center'>Young Available?</th>
                                <th style='text-align:center'>CARES Registered?</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for species_instance in member_cares_species_list %}
                                <tr>
                                    <td style='text-align:left'>{{ species_instance.user.first_name }} {{ species_instance.user.last_name }}</td>
                                    <td><a href="{% url 'speciesInstance' species_instance.id %}">{{species_instance.species.name}}</a></td>
                                    <td style='text-align:left'>
                                        {{ species_instance.species.get_cares_classification_display }}
                                    </td>
                                    <td style='text-align:center'>
                                        {% if species_instance.have_spawned  %}
                                            Yes
                                        {% else  %}
                                            No
                                        {% endif  %}                                        
                                    </td>
                                    <td style='text-align:center'>
                                        {% if species_instance.young_available  %}
                                            Yes
                                        {% else  %}
                                            No
                                        {% endif  %}                                        
                                    </td>     
                                    <td style='text-align:center'>
                                        {% if species_instance.cares_registered  %}
                                            Yes
                                        {% else  %}
                                            No
                                        {% endif  %}                                        
                                    </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_member %}&member={{ selected_member }}{% endif %}{% if selected_species_kept %}&species_kept={{ selected_species_kept }}{% 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_member %}&member={{ selected_member }}{% endif %}{% if selected_species_kept %}&species_kept={{ selected_species_kept }}{% 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_member %}&member={{ selected_member }}{% endif %}{% if selected_species_kept %}&species_kept={{ selected_species_kept }}{% 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_member %}&member={{ selected_member }}{% endif %}{% if selected_species_kept %}&species_kept={{ selected_species_kept }}{% 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_member %}&member={{ selected_member }}{% endif %}{% if selected_species_kept %}&species_kept={{ selected_species_kept }}{% 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>
                    <p style="text-align:center;"><a href="{% url 'createCaresSpecies'%}">Add CARES Species</a></p>
                </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('member').addEventListener('change', function() {
        resetPageAndSubmit();
    });
    
    document.getElementById('species_kept').addEventListener('change', function() {
        resetPageAndSubmit();
    });
</script>    

{% endblock content %}