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

{% block title %}M-Pesa Transactions{% 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">M-Pesa Transactions</h1>
                <a href="{% url 'payments:dashboard' %}" class="btn btn-outline-secondary">
                    <span>←</span> Back to Dashboard
                </a>
            </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-2">
                    <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-2">
                    <input type="text" name="phone" class="form-control" placeholder="Phone number" 
                           value="{{ filters.phone|default:'' }}">
                </div>
                <div class="col-md-2">
                    <input type="number" name="amount_min" class="form-control" placeholder="Min amount" 
                           value="{{ filters.amount_min|default:'' }}" step="0.01">
                </div>
                <div class="col-md-2">
                    <input type="number" name="amount_max" class="form-control" placeholder="Max amount" 
                           value="{{ filters.amount_max|default:'' }}" step="0.01">
                </div>
                <div class="col-md-2">
                    <input type="date" name="date_from" class="form-control" 
                           value="{{ filters.date_from|default:'' }}">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary">
                        <span>🔍</span> Filter
                    </button>
                    <a href="{% url 'payments:transactions' %}" class="btn btn-outline-secondary">
                        <span>✕</span> Clear
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Transactions Table -->
    <div class="card shadow">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Transactions</h6>
        </div>
        <div class="card-body">
            {% if page_obj %}
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Transaction ID</th>
                                <th>Customer</th>
                                <th>Phone</th>
                                <th>Amount</th>
                                <th>Status</th>
                                <th>Borrower</th>
                                <th>Loan</th>
                                <th>Date</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for transaction in page_obj %}
                            <tr>
                                <td>
                                    <a href="{% url 'payments:transaction_detail' transaction.id %}">
                                        {{ transaction.trans_id }}
                                    </a>
                                </td>
                                <td>{{ transaction.get_customer_name|default:"N/A" }}</td>
                                <td>{{ transaction.msisdn }}</td>
                                <td>KES {{ transaction.trans_amount|floatformat:2|intcomma }}</td>
                                <td>
                                    <span class="badge badge-{% if transaction.status == 'processed' %}success{% elif transaction.status == 'failed' %}danger{% elif transaction.status == 'pending' %}warning{% else %}info{% endif %}">
                                        {{ transaction.get_status_display }}
                                    </span>
                                </td>
                                <td>
                                    {% if transaction.borrower %}
                                        {{ transaction.borrower.get_full_name }}
                                    {% else %}
                                        <span class="text-muted">Not matched</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if transaction.loan %}
                                        {{ transaction.loan.loan_number }}
                                    {% else %}
                                        <span class="text-muted">Not matched</span>
                                    {% endif %}
                                </td>
                                <td>{{ transaction.created_at|date:"M d, Y H:i" }}</td>
                                <td>
                                    <a href="{% url 'payments:transaction_detail' transaction.id %}" 
                                       class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    {% if not transaction.repayment %}
                                        <a href="{% url 'payments:reprocess_transaction' transaction.id %}" 
                                           class="btn btn-sm btn-outline-warning"
                                           title="Retry Matching (useful if ID was added after payment)">
                                            <span>↷</span> Retry
                                        </a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                <nav aria-label="Transactions pagination">
                    <ul class="pagination justify-content-center">
                        {% if page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page=1{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">First</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">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 }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">Next</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">Last</a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
                {% endif %}
            {% else %}
                <p class="text-muted">No transactions found.</p>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}