{% extends 'base.html' %}
{% load humanize %}

{% block title %}Unknown SasaPay Payments{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0">⚠️ Unknown SasaPay Payments</h1>
        <a href="{% url 'payments:sasapay_dashboard' %}" class="btn btn-outline-secondary btn-sm">
            ← Back to SasaPay Dashboard
        </a>
    </div>

    <!-- Filter -->
    <div class="card shadow mb-4">
        <div class="card-body py-2">
            <form method="get" class="form-inline">
                <label class="mr-2">Show:</label>
                <select name="resolved" class="form-control form-control-sm mr-2" onchange="this.form.submit()">
                    <option value="false" {% if resolved_filter != 'true' %}selected{% endif %}>Unresolved</option>
                    <option value="true" {% if resolved_filter == 'true' %}selected{% endif %}>Resolved</option>
                </select>
            </form>
        </div>
    </div>

    <div class="card shadow">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="thead-dark">
                        <tr>
                            <th>Date</th>
                            <th>Amount (KES)</th>
                            <th>Paid By</th>
                            <th>MSISDN</th>
                            <th>Bill Ref</th>
                            <th>M-Pesa Ref</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for payment in page_obj %}
                        <tr>
                            <td>{{ payment.created_at|date:"d/m/Y H:i" }}</td>
                            <td><strong>{{ payment.amount|floatformat:2|intcomma }}</strong></td>
                            <td>{{ payment.paid_by }}</td>
                            <td>{{ payment.msisdn }}</td>
                            <td>{{ payment.bill_ref }}</td>
                            <td>{{ payment.reference }}</td>
                            <td>
                                {% if payment.resolved %}
                                <span class="badge badge-success">Resolved</span>
                                {% else %}
                                <span class="badge badge-warning">Pending</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if not payment.resolved %}
                                <form method="post" action="{% url 'payments:sasapay_resolve_unknown' payment.id %}" style="display:inline">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-sm btn-success"
                                        onclick="return confirm('Mark this payment as resolved?')">
                                        ✓ Resolve
                                    </button>
                                </form>
                                {% endif %}
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                No unknown payments found.
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav class="mt-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 }}&resolved={{ resolved_filter }}">Previous</a>
            </li>
            {% endif %}
            <li class="page-item disabled">
                <span class="page-link">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span>
            </li>
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}&resolved={{ resolved_filter }}">Next</a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}
