{% extends "fortunaisk/base.html" %}
{% load i18n humanize %}

{% block page_title %}
    {% trans "All Anomalies" %}
{% endblock page_title %}

{% block details %}
<div class="container mt-4">
    <h2 class="text-center mb-4">
        <i class="fas fa-exclamation-triangle me-2 text-warning"></i>
        {% trans "All Anomalies" %}
    </h2>

    {% if page_obj.object_list %}
    <div class="table-responsive">
        <table class="table table-bordered table-hover align-middle">
            <thead class="table-danger">
                <tr>
                    <th scope="col">{% trans "Lottery Reference" %}</th>
                    <th scope="col">{% trans "User" %}</th>
                    <th scope="col">{% trans "Character" %}</th>
                    <th scope="col">{% trans "Reason" %}</th>
                    <th scope="col">{% trans "Payment Date" %}</th>
                    <th scope="col">{% trans "Amount (ISK)" %}</th>
                    <th scope="col">{% trans "Payment ID" %}</th>
                    <th scope="col">{% trans "Recorded At" %}</th>
                    <th scope="col">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody>
                {% for anomaly in page_obj.object_list %}
                <tr>
                    <td>
                        {% if anomaly.lottery %}
                            {{ anomaly.lottery.lottery_reference }}
                        {% else %}
                            <span class="text-muted">N/A</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if anomaly.user %}
                            {{ anomaly.user.username }}
                        {% else %}
                            <span class="text-muted">N/A</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if anomaly.character %}
                            {{ anomaly.character.character_name }}
                        {% else %}
                            <span class="text-muted">N/A</span>
                        {% endif %}
                    </td>
                    <td>{{ anomaly.reason }}</td>
                    <td>{{ anomaly.payment_date|date:"Y-m-d H:i" }}</td>
                    <td>{{ anomaly.amount|floatformat:2 }}</td>
                    <td>{{ anomaly.payment_id }}</td>
                    <td>{{ anomaly.recorded_at|date:"Y-m-d H:i" }}</td>
                    <td>
                        <!-- Resolve button -->
                        <form action="{% url 'fortunaisk:resolve_anomaly' anomaly.id %}"
                                method="post"
                                style="display:inline;">
                            {% csrf_token %}
                            <button type="submit"
                                    class="btn btn-sm btn-success"
                                    onclick="return confirm('{% trans "Are you sure you want to resolve this anomaly?" %}');">
                                <i class="fas fa-check-circle"></i>
                                {% trans "Resolve" %}
                            </button>
                        </form>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    <nav aria-label="Page navigation" class="my-3">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}"
                    aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            {% else %}
            <li class="page-item disabled">
                <span class="page-link" aria-hidden="true">&laquo;</span>
            </li>
            {% endif %}

            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                    <li class="page-item active">
                        <span class="page-link">{{ num }}</span>
                    </li>
                {% else %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ num }}">
                            {{ num }}
                        </a>
                    </li>
                {% endif %}
            {% endfor %}

            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}"
                    aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
            {% else %}
            <li class="page-item disabled">
                <span class="page-link" aria-hidden="true">&raquo;</span>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% else %}
    <div class="alert alert-success text-center">
        <i class="fas fa-check-circle alert-icon"></i>
        {% trans "No anomalies detected." %}
    </div>
    {% endif %}
</div>
{% endblock details %}
