{% extends "superadmin/base_superadmin.html" %}

{% block title %}Financial Overview - Superadmin{% endblock %}

{% block content %}
<div class="space-y-6">
    <!-- Page Header -->
    <div class="border-b border-gray-200 pb-4">
        <h1 class="text-2xl font-bold text-gray-900">Financial Overview</h1>
        <p class="text-gray-600 mt-1">Comprehensive financial management and analytics</p>
    </div>

    <!-- Financial Summary Cards -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
        <!-- Total Deposits -->
        <div class="bg-white overflow-hidden shadow rounded-lg border-l-4 border-green-500">
            <div class="p-5">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <i class="fas fa-piggy-bank text-3xl text-green-600"></i>
                    </div>
                    <div class="ml-5 w-0 flex-1">
                        <dl>
                            <dt class="text-sm font-medium text-gray-500 truncate">Total Deposits</dt>
                            <dd class="text-2xl font-bold text-green-600">৳{{ "%.2f"|format(total_deposits) }}</dd>
                            <dd class="text-xs text-gray-400 mt-1">All contributions combined</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>

        <!-- Approved Loans -->
        <div class="bg-white overflow-hidden shadow rounded-lg border-l-4 border-blue-500">
            <div class="p-5">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <i class="fas fa-hand-holding-usd text-3xl text-blue-600"></i>
                    </div>
                    <div class="ml-5 w-0 flex-1">
                        <dl>
                            <dt class="text-sm font-medium text-gray-500 truncate">Approved Loans</dt>
                            <dd class="text-2xl font-bold text-blue-600">৳{{ "%.2f"|format(total_loans) }}</dd>
                            <dd class="text-xs text-gray-400 mt-1">Loans disbursed</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>

        <!-- Pending Loans -->
        <div class="bg-white overflow-hidden shadow rounded-lg border-l-4 border-yellow-500">
            <div class="p-5">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <i class="fas fa-clock text-3xl text-yellow-600"></i>
                    </div>
                    <div class="ml-5 w-0 flex-1">
                        <dl>
                            <dt class="text-sm font-medium text-gray-500 truncate">Pending Loans</dt>
                            <dd class="text-2xl font-bold text-yellow-600">৳{{ "%.2f"|format(pending_loans) }}</dd>
                            <dd class="text-xs text-gray-400 mt-1">Awaiting approval</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Group Financial Breakdown -->
    <div class="bg-white shadow rounded-lg">
        <div class="px-6 py-4 border-b border-gray-200">
            <h3 class="text-lg font-medium text-gray-900">
                <i class="fas fa-building text-blue-600 mr-2"></i>Group Financial Breakdown
            </h3>
        </div>
        <div class="p-6">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Group</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Members</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Total Contributions</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Loans</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Loan Amount</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        {% for group in group_financials %}
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="flex items-center">
                                    <div class="w-8 h-8 bg-blue-100 rounded-full flex items-center justify-center">
                                        <i class="fas fa-users text-blue-600"></i>
                                    </div>
                                    <div class="ml-4">
                                        <div class="text-sm font-medium text-gray-900">{{ group.name }}</div>
                                        <div class="text-sm text-gray-500">{{ group.group_code }}</div>
                                    </div>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                    {{ group.active_members or 0 }}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                ৳{{ "%.2f"|format(group.total_contributions or 0) }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                {{ group.total_loans or 0 }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                ৳{{ "%.2f"|format(group.loan_amount or 0) }}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Large Transactions & Contribution Types -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <!-- Recent Large Transactions -->
        <div class="bg-white shadow rounded-lg">
            <div class="px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg font-medium text-gray-900">
                    <i class="fas fa-money-bill-wave text-green-600 mr-2"></i>Large Transactions (৳1,000+)
                </h3>
            </div>
            <div class="p-6">
                <div class="space-y-4">
                    {% for transaction in large_transactions %}
                    <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                        <div class="flex items-center">
                            <div class="w-10 h-10 bg-green-100 rounded-full flex items-center justify-center">
                                <i class="fas fa-arrow-up text-green-600"></i>
                            </div>
                            <div class="ml-3">
                                <p class="text-sm font-medium text-gray-900">{{ transaction.member.full_name }}</p>
                                <p class="text-xs text-gray-500">{{ transaction.contribution_type|title }}</p>
                            </div>
                        </div>
                        <div class="text-right">
                            <p class="text-sm font-bold text-green-600">৳{{ "%.2f"|format(transaction.amount) }}</p>
                            <p class="text-xs text-gray-500">{{ transaction.created_at.strftime('%b %d, %Y') }}</p>
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>

        <!-- Contribution Types -->
        <div class="bg-white shadow rounded-lg">
            <div class="px-6 py-4 border-b border-gray-200">
                <h3 class="text-lg font-medium text-gray-900">
                    <i class="fas fa-chart-pie text-purple-600 mr-2"></i>Contribution Types
                </h3>
            </div>
            <div class="p-6">
                <div class="space-y-4">
                    {% for type in contribution_types %}
                    <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                        <div class="flex items-center">
                            <div class="w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center">
                                {% if type.contribution_type == 'monthly' %}
                                <i class="fas fa-calendar-alt text-purple-600"></i>
                                {% elif type.contribution_type == 'special' %}
                                <i class="fas fa-star text-purple-600"></i>
                                {% elif type.contribution_type == 'penalty' %}
                                <i class="fas fa-exclamation-triangle text-purple-600"></i>
                                {% else %}
                                <i class="fas fa-gift text-purple-600"></i>
                                {% endif %}
                            </div>
                            <div class="ml-3">
                                <p class="text-sm font-medium text-gray-900">{{ type.contribution_type|title }}</p>
                                <p class="text-xs text-gray-500">{{ type.count }} transactions</p>
                            </div>
                        </div>
                        <div class="text-right">
                            <p class="text-sm font-bold text-purple-600">৳{{ "%.2f"|format(type.total) }}</p>
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}