<!-- Sources Table Component -->
<style>
.mark-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
    margin: 2px 0;
}

.mark-present {
    background: #28a745;
    color: white;
}

.mark-absent {
    background: #dc3545;
    color: white;
}
</style>

<div class="card">
    <div class="card-header bg-primary text-white">
        <h5 class="mb-0">Объекты (Sources)</h5>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive" style="max-height: 70vh; overflow-y: auto;">
            <table class="table table-striped table-hover table-sm table-bordered mb-0">
                <thead class="table-dark sticky-top">
                    <tr>
                        <th>ID</th>
                        <th>Спутники</th>
                        <th>Кол-во объектов</th>
                        <th>Усреднённые координаты</th>
                        <th>Координаты Кубсата</th>
                        <th>Координаты оперативников</th>
                        <th>Справочные координаты</th>
                        <th>Дата создания</th>
                        {% if show_marks == '1' %}
                        <th>Отметки</th>
                        {% endif %}
                    </tr>
                </thead>
                <tbody>
                    {% for source in processed_sources %}
                    <tr>
                        <td><a href="{% url 'mainapp:source_update' source.id %}">{{ source.id }}</a></td>
                        <td>{{ source.satellites }}</td>
                        <td>{{ source.objitem_count }}</td>
                        <td>{{ source.coords_average }}</td>
                        <td>{{ source.coords_kupsat }}</td>
                        <td>{{ source.coords_valid }}</td>
                        <td>{{ source.coords_reference }}</td>
                        <td>{{ source.created_at|date:"d.m.Y H:i" }}</td>
                        {% if show_marks == '1' %}
                        <td>
                            {% if source.marks %}
                            <div style="max-height: 150px; overflow-y: auto;">
                                {% for mark in source.marks %}
                                <div class="mb-1">
                                    <span class="mark-badge {% if mark.mark %}mark-present{% else %}mark-absent{% endif %}">
                                        {% if mark.mark %}✓ Есть{% else %}✗ Нет{% endif %}
                                    </span>
                                    <br>
                                    <small class="text-muted">{{ mark.timestamp|date:"d.m.Y H:i" }}</small>
                                    <br>
                                    <small class="text-muted">{{ mark.created_by }}</small>
                                </div>
                                {% endfor %}
                            </div>
                            {% else %}
                            <span class="text-muted">-</span>
                            {% endif %}
                        </td>
                        {% endif %}
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="{% if show_marks == '1' %}9{% else %}8{% endif %}" class="text-center py-4 text-muted">
                            Нет данных для выбранных фильтров
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    
    <!-- Pagination -->
    {% if page_obj.paginator.num_pages > 1 %}
    <div class="card-footer">
        <nav>
            <ul class="pagination justify-content-center mb-0">
                {% if page_obj.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page=1">Первая</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.previous_page_number }}">Предыдущая</a>
                </li>
                {% endif %}
                
                <li class="page-item active">
                    <span class="page-link">{{ page_obj.number }} из {{ page_obj.paginator.num_pages }}</span>
                </li>
                
                {% if page_obj.has_next %}
                <li class="page-item">
                    <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.next_page_number }}">Следующая</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.paginator.num_pages }}">Последняя</a>
                </li>
                {% endif %}
            </ul>
        </nav>
        <div class="text-center mt-2">
            <small class="text-muted">Показано {{ page_obj.start_index }}-{{ page_obj.end_index }} из {{ page_obj.paginator.count }} записей</small>
        </div>
    </div>
    {% endif %}
</div>
