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

{% block title %}My 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">My M-Pesa Payments</h1>
                <a href="{% url 'dashboard' %}" class="btn btn-outline-secondary">
                    <span>←</span> Back to Dashboard
                </a>
            </div>
        </div>
    </div>

    <!-- Payment Instructions -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card shadow">
                <div class="card-header bg-primary text-white">
                    <h6 class="mb-0">
                        <i class="fas fa-mobile-alt"></i> How to Make a Payment
                    </h6>
                </div>
                <div class="card-body">
                    <ol class="mb-0">
                        <li>Go to M-Pesa on your phone</li>
                        <li>Select <strong>Lipa na M-Pesa</strong></li>
                        <li>Select <strong>Pay Bill</strong></li>
                        <li>Enter Business Number: <strong class="text-primary">4159523</strong></li>
                        <li>Enter Account Number: <strong class="text-primary">Your Loan Number</strong> (optional)</li>
                        <li>Enter Amount</li>
                        <li>Enter your M-Pesa PIN and confirm</li>
                    </ol>
                    <div class="alert alert-info mt-3 mb-0">
                        <span>ℹ️</span> 
                        <strong>Tip:</strong> Use your registered phone number ({{ user.phone_number }}) to ensure automatic payment matching.
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Payment History -->
    <div class="card shadow">
        <div class="card-header">
            <h6 class="m-0 font-weight-bold text-primary">
                Payment History
                {% if page_obj %}
                    <span class="badge badge-info">{{ page_obj.paginator.count }} payments</span>
                {% endif %}
            </h6>
        </div>
        <div class="card-body">
            {% if page_obj %}
                <div class="table-responsive">
                    <table class="table table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Transaction ID</th>
                                <th>Amount</th>
                                <th>Loan</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for transaction in page_obj %}
                                <tr>
                                    <td>
                                        {{ transaction.created_at|date:"Y-m-d H:i" }}
                                        <br>
                                        <small class="text-muted">{{ transaction.created_at|timesince }} ago</small>
                                    </td>
                                    <td>
                                        <strong>{{ transaction.trans_id|default:"N/A" }}</strong>
                                        {% if transaction.mpesa_transaction_id %}
                                            <br><small class="text-muted">{{ transaction.mpesa_transaction_id }}</small>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <strong class="text-success">
                                            KES {{ transaction.amount|floatformat:2|intcomma }}
                                        </strong>
                                    </td>
                                    <td>
                                        {% if transaction.loan %}
                                            <a href="{% url 'loans:detail' transaction.loan.id %}">
                                                {{ transaction.loan.loan_number }}
                                            </a>
                                            <br>
                                            <small class="text-muted">
                                                Loan Amount: KES {{ transaction.loan.amount|floatformat:2|intcomma }}
                                            </small>
                                        {% else %}
                                            <span class="text-muted">Not matched</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if transaction.status == 'confirmed' or transaction.status == 'processed' %}
                                            <span class="badge badge-success">
                                                <span>✓</span> Processed
                                            </span>
                                            {% if transaction.repayment %}
                                                <br>
                                                <small class="text-success">
                                                    <span>✓</span> Repayment created
                                                </small>
                                            {% endif %}
                                        {% elif transaction.status == 'pending' or transaction.status == 'validated' %}
                                            <span class="badge badge-warning">
                                                <span>⏰</span> Pending
                                            </span>
                                        {% else %}
                                            <span class="badge badge-danger">
                                                <span>✕</span> {{ transaction.get_status_display }}
                                            </span>
                                        {% endif %}
                                    </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">First</a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}">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 }}">Next</a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                {% endif %}
            {% else %}
                <div class="alert alert-info">
                    <span>ℹ️</span> 
                    You haven't made any M-Pesa payments yet. Follow the instructions above to make your first payment.
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
