{% extends "base.html" %}

{% block title %}{{ current_user.full_name }} - Member Dashboard{% endblock %}

{% block content %}
<div class="min-h-screen bg-gray-50">
    <!-- Enhanced Navigation -->
    <nav class="bg-white shadow-sm border-b border-gray-200">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-16">
                <div class="flex items-center">
                    <h1 class="text-xl font-semibold text-gray-900">
                        <i class="fas fa-user-circle text-green-600 mr-2"></i>
                        {{ member_group.name }} - Member Portal
                    </h1>
                </div>
                <div class="flex items-center space-x-4">
                    <!-- Profile Picture -->
                    <div class="w-8 h-8 bg-green-100 rounded-full flex items-center justify-center">
                        <i class="fas fa-user text-green-600"></i>
                    </div>
                    <div class="flex flex-col">
                        <span class="text-sm font-medium text-gray-900">
                            {% if current_user.custom_id %}
                                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800 mr-1">{{ current_user.custom_id }}</span>
                            {% endif %}
                            {{ current_user.full_name }}
                        </span>
                        <span class="text-xs text-gray-500">Member</span>
                    </div>
                    <!-- Profile Menu -->
                    <div class="relative">
                        <button id="profile-menu-button" class="flex items-center text-gray-500 hover:text-gray-700 focus:outline-none">
                            <i class="fas fa-chevron-down"></i>
                        </button>
                        <div id="profile-menu" class="hidden absolute right-0 mt-2 w-48 bg-white rounded-md shadow-lg py-1 z-50">
                            <a href="{{ url_for('member_profile') }}" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                <i class="fas fa-user-edit mr-2"></i>Update Profile
                            </a>
                            <a href="{{ url_for('member_contributions') }}" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                <i class="fas fa-list mr-2"></i>My Contributions
                            </a>
                            <a href="{{ url_for('member_events') }}" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                <i class="fas fa-calendar-alt mr-2"></i>Events
                            </a>
                            <div class="border-t border-gray-100"></div>
                            <a href="{{ url_for('logout') }}" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">
                                <i class="fas fa-sign-out-alt mr-2"></i>Logout
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <!-- Quick Actions Bar -->
    <div class="bg-white border-b border-gray-200">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex space-x-8 overflow-x-auto py-4">
                <a href="{{ url_for('member_dashboard') }}" class="flex items-center px-4 py-2 text-sm font-medium text-green-600 bg-green-50 rounded-lg border border-green-200">
                    <i class="fas fa-home mr-2"></i>Dashboard
                </a>
                <a href="{{ url_for('member_profile') }}" class="flex items-center px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 rounded-lg border border-gray-200">
                    <i class="fas fa-user-edit mr-2"></i>Update Profile
                </a>
                <a href="{{ url_for('member_contributions') }}" class="flex items-center px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 rounded-lg border border-gray-200">
                    <i class="fas fa-list mr-2"></i>My Contributions
                </a>
                <a href="{{ url_for('member_events') }}" class="flex items-center px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 rounded-lg border border-gray-200">
                    <i class="fas fa-calendar-alt mr-2"></i>Events
                </a>
            </div>
        </div>
    </div>

    <!-- Dashboard Content -->
    <div class="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
        <!-- Monthly Payment Alert -->
        {% if monthly_payment_due %}
        <div class="mb-6">
            <div class="bg-gradient-to-r from-red-500 to-orange-500 rounded-2xl p-6 text-white shadow-lg">
                <div class="flex items-center justify-between">
                    <div class="flex items-center space-x-4">
                        <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                            <i class="fas fa-exclamation-triangle text-2xl"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-bold">Monthly Payment Due</h3>
                            <p class="text-red-100">Your payment for {{ current_month.strftime('%B %Y') }} is overdue</p>
                        </div>
                    </div>
                    <div class="flex flex-col items-end">
                        <span class="text-sm text-red-100 mb-2">Contact admin to record payment</span>
                        <div class="flex space-x-2">
                            <span class="px-4 py-2 bg-white/20 rounded-lg text-sm font-medium">
                                <i class="fas fa-clock mr-1"></i>Overdue
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% else %}
        <div class="mb-6">
            <div class="bg-gradient-to-r from-green-500 to-teal-500 rounded-2xl p-6 text-white shadow-lg">
                <div class="flex items-center space-x-4">
                    <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                        <i class="fas fa-check-circle text-2xl"></i>
                    </div>
                    <div>
                        <h3 class="text-xl font-bold">Payment Complete</h3>
                        <p class="text-green-100">Your payment for {{ current_month.strftime('%B %Y') }} has been recorded</p>
                    </div>
                </div>
            </div>
        </div>
        {% endif %}

        <!-- Enhanced Statistics Grid -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <!-- My Total Contributed -->
            <div class="bg-white overflow-hidden shadow rounded-lg">
                <div class="p-5">
                    <div class="flex items-center">
                        <div class="flex-shrink-0">
                            <i class="fas fa-piggy-bank text-2xl text-green-600"></i>
                        </div>
                        <div class="ml-5 w-0 flex-1">
                            <dl>
                                <dt class="text-sm font-medium text-gray-500 truncate">My Total Contributed</dt>
                                <dd class="text-lg font-medium text-gray-900">৳{{ "%.2f"|format(total_contributed) }}</dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Monthly Contributions -->
            <div class="bg-white overflow-hidden shadow rounded-lg">
                <div class="p-5">
                    <div class="flex items-center">
                        <div class="flex-shrink-0">
                            <i class="fas fa-calendar-check text-2xl text-blue-600"></i>
                        </div>
                        <div class="ml-5 w-0 flex-1">
                            <dl>
                                <dt class="text-sm font-medium text-gray-500 truncate">Monthly Contributions</dt>
                                <dd class="text-lg font-medium text-gray-900">{{ monthly_contributions|length }}</dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Group Members -->
            <div class="bg-white overflow-hidden shadow rounded-lg">
                <div class="p-5">
                    <div class="flex items-center">
                        <div class="flex-shrink-0">
                            <i class="fas fa-users text-2xl text-purple-600"></i>
                        </div>
                        <div class="ml-5 w-0 flex-1">
                            <dl>
                                <dt class="text-sm font-medium text-gray-500 truncate">Group Members</dt>
                                <dd class="text-lg font-medium text-gray-900">{{ total_members }}</dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Active Events -->
            <div class="bg-white overflow-hidden shadow rounded-lg">
                <div class="p-5">
                    <div class="flex items-center">
                        <div class="flex-shrink-0">
                            <i class="fas fa-star text-2xl text-yellow-600"></i>
                        </div>
                        <div class="ml-5 w-0 flex-1">
                            <dl>
                                <dt class="text-sm font-medium text-gray-500 truncate">Active Events</dt>
                                <dd class="text-lg font-medium text-gray-900">{{ active_events|length }}</dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Two Column Layout -->
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
            <!-- My Contribution Chart -->
            <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-line text-green-600 mr-2"></i>My Contribution Trends
                    </h3>
                </div>
                <div class="p-6">
                    <canvas id="contributionChart" width="400" height="200"></canvas>
                </div>
            </div>

            <!-- Member Rankings -->
            <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-trophy text-yellow-600 mr-2"></i>Top Contributors
                    </h3>
                </div>
                <div class="p-6">
                    <div class="space-y-4">
                        {% for ranking in member_rankings[:5] %}
                        <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                            <div class="flex items-center space-x-3">
                                <div class="w-8 h-8 bg-blue-100 rounded-full flex items-center justify-center">
                                    <span class="text-xs font-medium text-blue-600">{{ loop.index }}</span>
                                </div>
                                <div>
                                    <div class="font-medium text-gray-900">{{ ranking.full_name }}</div>
                                    {% if ranking.custom_id %}
                                    <div class="text-xs text-gray-500">{{ ranking.custom_id }}</div>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="text-right">
                                <div class="font-medium text-green-600">৳{{ "%.2f"|format(ranking.total_amount) }}</div>
                                {% if ranking.id == current_user.id %}
                                <div class="text-xs text-blue-600">You</div>
                                {% endif %}
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>

        <!-- Recent Activity and Upcoming Events -->
        <div class="mt-8 grid grid-cols-1 lg:grid-cols-2 gap-6">
            <!-- Recent Group Contributions -->
            <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-clock text-blue-600 mr-2"></i>Recent Group Activity
                    </h3>
                </div>
                <div class="p-6">
                    <div class="space-y-4">
                        {% for contribution in recent_group_contributions[:8] %}
                        <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                            <div class="flex items-center space-x-3">
                                <div class="w-8 h-8 bg-green-100 rounded-full flex items-center justify-center">
                                    <i class="fas fa-plus text-green-600 text-xs"></i>
                                </div>
                                <div>
                                    <div class="font-medium text-gray-900">{{ contribution.member.full_name }}</div>
                                    <div class="text-xs text-gray-500">{{ contribution.contribution_type.title() }} contribution</div>
                                </div>
                            </div>
                            <div class="text-right">
                                <div class="font-medium text-green-600">৳{{ "%.2f"|format(contribution.amount) }}</div>
                                <div class="text-xs text-gray-500">{{ contribution.created_at.strftime('%b %d') }}</div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>

            <!-- Upcoming Events -->
            <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-calendar-alt text-purple-600 mr-2"></i>Upcoming Events
                    </h3>
                </div>
                <div class="p-6">
                    {% if upcoming_events %}
                    <div class="space-y-4">
                        {% for event in upcoming_events %}
                        <div class="border border-gray-200 rounded-lg p-4">
                            <div class="flex items-center justify-between">
                                <div>
                                    <h4 class="font-medium text-gray-900">{{ event.name }}</h4>
                                    <p class="text-sm text-gray-500">{{ event.description }}</p>
                                </div>
                                <div class="text-right">
                                    <div class="font-medium text-purple-600">৳{{ "%.2f"|format(event.target_amount) }}</div>
                                    <div class="text-xs text-gray-500">Target</div>
                                </div>
                            </div>
                            <div class="mt-3 flex items-center justify-between">
                                <div class="text-sm text-gray-500">
                                    <i class="fas fa-calendar mr-1"></i>
                                    {{ event.start_date.strftime('%b %d') }} - {{ event.end_date.strftime('%b %d, %Y') }}
                                </div>
                                <div class="text-sm">
                                    {% if event.is_active %}
                                    <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                        Active
                                    </span>
                                    {% else %}
                                    <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
                                        Inactive
                                    </span>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                    {% else %}
                    <div class="text-center py-4">
                        <i class="fas fa-calendar-alt text-gray-400 text-3xl mb-2"></i>
                        <p class="text-gray-500">No upcoming events</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
// Profile dropdown toggle
document.getElementById('profile-menu-button').addEventListener('click', function() {
    document.getElementById('profile-menu').classList.toggle('hidden');
});

// Close dropdown when clicking outside
document.addEventListener('click', function(event) {
    const menu = document.getElementById('profile-menu');
    const button = document.getElementById('profile-menu-button');
    
    if (!menu.contains(event.target) && !button.contains(event.target)) {
        menu.classList.add('hidden');
    }
});

// Contribution Chart
const monthlyData = {{ monthly_stats|tojson if monthly_stats else '[]' }};
const chartLabels = monthlyData.map(stat => {
    const date = new Date(stat.month);
    return date.toLocaleDateString('en-US', { month: 'short', year: 'numeric' });
});
const chartData = monthlyData.map(stat => stat.total);

const ctx = document.getElementById('contributionChart').getContext('2d');
new Chart(ctx, {
    type: 'line',
    data: {
        labels: chartLabels,
        datasets: [{
            label: 'My Contributions',
            data: chartData,
            borderColor: 'rgb(34, 197, 94)',
            backgroundColor: 'rgba(34, 197, 94, 0.1)',
            tension: 0.1
        }]
    },
    options: {
        responsive: true,
        scales: {
            y: {
                beginAtZero: true,
                ticks: {
                    callback: function(value) {
                        return '৳' + value.toFixed(2);
                    }
                }
            }
        },
        plugins: {
            tooltip: {
                callbacks: {
                    label: function(context) {
                        return 'Amount: ৳' + context.parsed.y.toFixed(2);
                    }
                }
            }
        }
    }
});
</script>
{% endblock %}