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

{% block content %}
<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">{% trans "Financial Overview" %}</h2>
            <p class="text-muted">{% trans "Manage all platform transactions, payments, and revenue." %}</p>
        </div>
        <div class="card bg-primary text-white border-0 shadow-sm p-3">
            <h6 class="text-uppercase small mb-1">{% trans "Total Revenue" %}</h6>
            <h3 class="mb-0">{{ total_revenue }}</h3>
        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-header bg-white py-3">
            <h5 class="mb-0">{% trans "Recent Transactions" %}</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>{% trans "User" %}</th>
                            <th>{% trans "Role" %}</th>
                            <th>{% trans "Receipt #" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Amount" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Action" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for transaction in transactions %}
                        <tr>
                            <td>
                                <strong>{{ transaction.user.username }}</strong><br>
                                <small class="text-muted">{{ transaction.user.email }}</small>
                            </td>
                            <td>
                                <span class="badge bg-light text-dark">{{ transaction.user.profile.get_role_display }}</span>
                            </td>
                            <td><code>{{ transaction.receipt_number }}</code></td>
                            <td>{{ transaction.created_at|date:"Y-m-d" }}</td>
                            <td><strong>{{ transaction.amount }}</strong></td>
                            <td>
                                <span class="badge {% if transaction.status == 'COMPLETED' %}bg-success{% elif transaction.status == 'PENDING' %}bg-warning{% else %}bg-danger{% endif %}">
                                    {{ transaction.get_status_display }}
                                </span>
                            </td>
                            <td>
                                <a href="{% url 'transaction_receipt' transaction.receipt_number %}" class="btn btn-sm btn-outline-info me-1" title="{% trans 'View Receipt' %}" target="_blank">
                                    <i class="fas fa-eye"></i>
                                </a>
                                {% if transaction.transaction_type == 'PAYMENT' and transaction.status == 'COMPLETED' %}
                                <a href="{% url 'issue_refund' transaction.receipt_number %}" class="btn btn-sm btn-outline-danger" onclick="return confirm('{% trans 'Are you sure you want to issue a refund?' %}')" title="{% trans 'Issue Refund' %}">
                                    <i class="fas fa-undo"></i>
                                </a>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}