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

{% block title %}Unconfirmed Payments{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3 mb-0">Unconfirmed Payments</h1>
                <a href="{% url 'payments:dashboard' %}" class="btn btn-outline-secondary">
                    <span>←</span> Back to Dashboard
                </a>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                        Pending Approval
                    </div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">
                        {{ stats.pending|intcomma }}
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                        Approved
                    </div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">
                        {{ stats.approved|intcomma }}
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-danger shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-danger text-uppercase mb-1">
                        Rejected
                    </div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">
                        {{ stats.rejected|intcomma }}
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                        Processed
                    </div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">
                        {{ stats.processed|intcomma }}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="card shadow mb-4">
        <div class="card-header">
            <h6 class="m-0 font-weight-bold text-primary">Filters</h6>
        </div>
        <div class="card-body">
            <form method="get" class="row">
                <div class="col-md-4">
                    <label>Status</label>
                    <select name="status" class="form-control">
                        <option value="">All Statuses</option>
                        {% for value, label in status_choices %}
                            <option value="{{ value }}" {% if filters.status == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-4">
                    <label>Match Type</label>
                    <select name="match_type" class="form-control">
                        <option value="">All Match Types</option>
                        {% for value, label in match_type_choices %}
                            <option value="{{ value }}" {% if filters.match_type == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-4">
                    <label>&nbsp;</label>
                    <div>
                        <button type="submit" class="btn btn-primary">
                            <span>🔍</span> Filter
                        </button>
                        <a href="{% url 'payments:unconfirmed_payments' %}" class="btn btn-outline-secondary">
                            <span>✕</span> Clear
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Payments Table -->
    <div class="card shadow">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Unconfirmed Payments</h6>
        </div>
        <div class="card-body">
            {% if page_obj %}
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Amount</th>
                                <th>Phone</th>
                                <th>ID Number</th>
                                <th>Suggested Borrower</th>
                                <th>Match Type</th>
                                <th>Status</th>
                                <th>Created</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for payment in page_obj %}
                            <tr>
                                <td>KES {{ payment.mpesa_transaction.amount|floatformat:2|intcomma }}</td>
                                <td>{{ payment.payment_phone|default:"N/A" }}</td>
                                <td>{{ payment.payment_id_number|default:"N/A" }}</td>
                                <td>
                                    {% if payment.suggested_borrower %}
                                        <a href="{% url 'users:client_detail' payment.suggested_borrower.id %}">
                                            {{ payment.suggested_borrower.get_full_name }}
                                        </a>
                                    {% else %}
                                        <span class="text-muted">None</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <span class="badge badge-info">
                                        {{ payment.get_match_type_display }}
                                    </span>
                                </td>
                                <td>
                                    <span class="badge badge-{% if payment.status == 'pending' %}warning{% elif payment.status == 'approved' %}success{% elif payment.status == 'rejected' %}danger{% else %}info{% endif %}">
                                        {{ payment.get_status_display }}
                                    </span>
                                </td>
                                <td>{{ payment.created_at|date:"M d, Y H:i" }}</td>
                                <td>
                                    <a href="{% url 'payments:unconfirmed_payment_detail' payment.id %}" class="btn btn-sm btn-primary">
                                        <i class="fas fa-eye"></i> View
                                    </a>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                <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 filters.status %}&status={{ filters.status }}{% endif %}{% if filters.match_type %}&match_type={{ filters.match_type }}{% endif %}">First</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if filters.status %}&status={{ filters.status }}{% endif %}{% if filters.match_type %}&match_type={{ filters.match_type }}{% endif %}">Previous</a>
                            </li>
                        {% endif %}

                        <li class="page-item active">
                            <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 }}{% if filters.status %}&status={{ filters.status }}{% endif %}{% if filters.match_type %}&match_type={{ filters.match_type }}{% endif %}">Next</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if filters.status %}&status={{ filters.status }}{% endif %}{% if filters.match_type %}&match_type={{ filters.match_type }}{% endif %}">Last</a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
                {% endif %}
            {% else %}
                <p class="text-muted">No unconfirmed payments found.</p>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
