{% extends "admin/base_admin.html" %}

{% block title %}Member Balances - Admin Panel{% endblock %}

{% block page_title %}Member Balances{% endblock %}

{% block content %}
<!-- Header -->
<div class="bg-white shadow rounded-lg p-6 mb-6">
    <div class="flex justify-between items-center">
        <div>
            <h2 class="text-2xl font-bold text-gray-900 mb-2">Member Balance Summary</h2>
            <p class="text-gray-600">Track individual member contributions and savings progress</p>
        </div>
        <div class="text-right">
            <div class="text-3xl font-bold text-green-600">{{ currency_symbol }}{{ "{:,.2f}".format(total_amount) }}</div>
            <div class="text-gray-500">Total Group Savings</div>
        </div>
    </div>
            </div>

<!-- Summary Statistics -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
    <div class="bg-white shadow rounded-lg p-6">
        <div class="flex items-center">
            <div class="p-3 rounded-full bg-blue-100 text-blue-600">
                <i class="fas fa-users text-2xl"></i>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-500">Active Members</p>
                <p class="text-2xl font-semibold text-gray-900">{{ total_members }}</p>
            </div>
        </div>
    </div>
    
    <div class="bg-white shadow rounded-lg p-6">
        <div class="flex items-center">
            <div class="p-3 rounded-full bg-green-100 text-green-600">
                <i class="fas fa-calculator text-2xl"></i>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-500">Average Contribution</p>
                <p class="text-2xl font-semibold text-gray-900">{{ currency_symbol }}{{ "{:,.2f}".format(average_contribution) }}</p>
            </div>
        </div>
    </div>
    
    <div class="bg-white shadow rounded-lg p-6">
        <div class="flex items-center">
            <div class="p-3 rounded-full bg-purple-100 text-purple-600">
                <i class="fas fa-chart-line text-2xl"></i>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-500">Group Progress</p>
                <p class="text-2xl font-semibold text-gray-900">100%</p>
            </div>
        </div>
    </div>
</div>

<!-- Member Balances Table -->
<div class="bg-white shadow rounded-lg overflow-hidden">
    <div class="px-6 py-4 border-b border-gray-200">
        <h3 class="text-lg font-medium text-gray-900">Individual Member Balances</h3>
        <p class="text-gray-500 text-sm">Detailed breakdown of each member's total contributions</p>
    </div>
                
    <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">Member</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Email</th>
                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Total Contributions</th>
                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Payment Count</th>
                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Percentage</th>
                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">
                            {% for member in member_balances %}
                            {% set member_total = member.total_contributions %}
                            {% set percentage = (member_total / total_amount * 100) if total_amount > 0 else 0 %}
                <tr class="hover:bg-gray-50 transition-colors">
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="flex items-center">
                            <div class="flex-shrink-0 h-10 w-10">
                                <div class="h-10 w-10 rounded-full bg-gradient-to-r from-blue-500 to-purple-600 flex items-center justify-center">
                                    <span class="text-white font-semibold text-sm">{{ member.full_name[0] if member.full_name else 'M' }}</span>
                                </div>
                            </div>
                            <div class="ml-4">
                                <div class="text-sm font-medium text-gray-900">{{ member.full_name }}</div>
                                <div class="text-sm text-gray-500">@{{ member.username }}</div>
                            </div>
                        </div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                            {{ member.custom_id }}
                        </span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        {{ member.email }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right">
                        <div class="text-lg font-semibold text-green-600">{{ currency_symbol }}{{ "{:,.2f}".format(member_total) }}</div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right">
                        <div class="text-sm text-gray-900">{{ member.contribution_count }}</div>
                        <div class="text-xs text-gray-500">payments</div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right">
                        <div class="text-sm text-gray-900">{{ "{:.1f}".format(percentage) }}%</div>
                        <div class="w-16 bg-gray-200 rounded-full h-2 ml-auto">
                            <div class="bg-gradient-to-r from-blue-500 to-purple-600 h-2 rounded-full" style="width: {{ percentage }}%"></div>
                        </div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right">
                        {% if member_total >= average_contribution %}
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                            <i class="fas fa-check-circle mr-1"></i> On Track
                        </span>
                        {% elif member_total >= (average_contribution * 0.8) %}
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800">
                            <i class="fas fa-exclamation-triangle mr-1"></i> Behind
                        </span>
                        {% else %}
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800">
                            <i class="fas fa-times-circle mr-1"></i> Low
                        </span>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Summary Footer -->
    <div class="bg-gray-50 px-6 py-4 border-t border-gray-200">
        <div class="flex justify-between items-center text-sm">
            <span class="text-gray-500">{{ member_balances|length }} active members showing contributions</span>
            <span class="text-gray-900 font-semibold">Total: {{ currency_symbol }}{{ "{:,.2f}".format(total_amount) }}</span>
        </div>
    </div>
</div>

<!-- Action Buttons -->
<div class="mt-6 flex justify-end space-x-4">
    <a href="{{ url_for('admin_contributions') }}" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-md text-sm font-medium transition-colors shadow-sm">
        <i class="fas fa-plus mr-2"></i>Add Contribution
    </a>
    <a href="{{ url_for('admin_bulk_import') }}" class="bg-indigo-600 hover:bg-indigo-700 text-white px-6 py-2 rounded-md text-sm font-medium transition-colors shadow-sm">
        <i class="fas fa-upload mr-2"></i>Bulk Import
    </a>
    <a href="{{ url_for('admin_reports') }}" class="bg-green-600 hover:bg-green-700 text-white px-6 py-2 rounded-md text-sm font-medium transition-colors shadow-sm">
        <i class="fas fa-chart-bar mr-2"></i>View Reports
    </a>
</div>
{% endblock %}