@extends('layouts.app')

@section('title', 'ড্যাশবোর্ড - আমবাগান যুব কল্যাণ সমিতি')

@section('styles')
<style>
    .stat-card {
        border-radius: 10px;
        transition: all 0.3s;
        border: none;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        margin-bottom: 20px;
        height: 100%;
        cursor: default;
    }
    
    .stat-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 20px rgba(0,0,0,0.15);
    }
    
    .stat-icon {
        font-size: 2.5rem;
        opacity: 0.8;
        margin-bottom: 10px;
    }
    
    .stat-number {
        font-size: 1.8rem;
        font-weight: 700;
        margin-bottom: 5px;
    }
    
    .stat-title {
        font-size: 0.9rem;
        color: #6c757d;
        text-transform: uppercase;
        letter-spacing: 1px;
    }
    
    .card-member {
        border-left: 4px solid #1a5f7a;
    }
    
    .card-saving {
        border-left: 4px solid #28a745;
    }
    
    .card-loan {
        border-left: 4px solid #dc3545;
    }
    
    .card-balance {
        border-left: 4px solid #ffc107;
    }
    
    .card-income {
        border-left: 4px solid #17a2b8;
    }
    
    .card-expense {
        border-left: 4px solid #6f42c1;
    }
    
    .activity-item {
        padding: 15px;
        border-bottom: 1px solid #eee;
        transition: all 0.2s;
    }
    
    .activity-item:hover {
        background-color: #f8f9fa;
    }
    
    .activity-icon {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.2rem;
    }
    
    .chart-container {
        position: relative;
        height: 300px;
        width: 100%;
    }
    
    .notice-card {
        border-left: 4px solid #1a5f7a;
        margin-bottom: 15px;
        border-radius: 8px;
        background-color: #fff;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        transition: all 0.2s;
    }
    
    .notice-card:hover {
        box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    
    .notice-card.important {
        border-left-color: #dc3545;
        background-color: #fff5f5;
    }
    
    .quick-link-btn {
        transition: all 0.2s;
        text-align: left;
        border: 1px solid #dee2e6;
        background-color: #f8f9fa;
    }
    
    .quick-link-btn:hover {
        background-color: #e9ecef;
        border-color: #adb5bd;
        transform: translateY(-2px);
    }
    
    .bg-purple {
        background-color: #6f42c1;
    }
    
    .text-purple {
        color: #6f42c1;
    }
    
    .btn-outline-purple {
        color: #6f42c1;
        border-color: #6f42c1;
    }
    
    .btn-outline-purple:hover {
        color: #fff;
        background-color: #6f42c1;
        border-color: #6f42c1;
    }
</style>
@endsection

@section('content')
<div class="container-fluid">
    <!-- স্বাগতম বার্তা -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h4 class="mb-1">
                                <i class="fas fa-hand-peace me-2"></i>
                                স্বাগতম, {{ Auth::user()->name }}!
                            </h4>
                            <p class="mb-0 opacity-75">
                                <i class="fas fa-calendar-alt me-2"></i>
                                আজ {{ now()->format('d F, Y') }}, {{ now()->format('l') }}
                            </p>
                        </div>
                        <div>
                            <span class="badge bg-light text-dark p-2">
                                <i class="fas fa-user-circle me-1"></i>
                                @if(Auth::user()->user_type == 'admin')
                                    অ্যাডমিন
                                @elseif(Auth::user()->user_type == 'committee')
                                    কমিটি সদস্য
                                @else
                                    সাধারণ সদস্য
                                @endif
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- পরিসংখ্যান কার্ড -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card stat-card card-member">
                <div class="card-body text-center">
                    <div class="stat-icon text-primary">
                        <i class="fas fa-users"></i>
                    </div>
                    <div class="stat-number">{{ $totalMembers ?? 0 }}</div>
                    <div class="stat-title">সক্রিয় সদস্য</div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stat-card card-saving">
                <div class="card-body text-center">
                    <div class="stat-icon text-success">
                        <i class="fas fa-piggy-bank"></i>
                    </div>
                    <div class="stat-number">{{ number_format($totalSavings ?? 0, 2) }} টাকা</div>
                    <div class="stat-title">মোট সঞ্চয়</div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stat-card card-loan">
                <div class="card-body text-center">
                    <div class="stat-icon text-danger">
                        <i class="fas fa-hand-holding-usd"></i>
                    </div>
                    <div class="stat-number">{{ number_format($totalLoans ?? 0, 2) }} টাকা</div>
                    <div class="stat-title">চলমান ঋণ</div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stat-card card-balance">
                <div class="card-body text-center">
                    <div class="stat-icon text-warning">
                        <i class="fas fa-balance-scale"></i>
                    </div>
                    <div class="stat-number">{{ number_format($totalLoanBalance ?? 0, 2) }} টাকা</div>
                    <div class="stat-title">বকেয়া ঋণ</div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- আয়-ব্যয় পরিসংখ্যান -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card stat-card card-income">
                <div class="card-body text-center">
                    <div class="stat-icon text-info">
                        <i class="fas fa-money-bill-wave"></i>
                    </div>
                    <div class="stat-number">{{ number_format($monthlyIncome ?? 0, 2) }} টাকা</div>
                    <div class="stat-title">এই মাসের আয়</div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stat-card card-expense">
                <div class="card-body text-center">
                    <div class="stat-icon" style="color: #6f42c1;">
                        <i class="fas fa-receipt"></i>
                    </div>
                    <div class="stat-number">{{ number_format($monthlyExpense ?? 0, 2) }} টাকা</div>
                    <div class="stat-title">এই মাসের ব্যয়</div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stat-card">
                <div class="card-body text-center">
                    <div class="stat-icon text-secondary">
                        <i class="fas fa-coins"></i>
                    </div>
                    <div class="stat-number">{{ number_format(($monthlyIncome ?? 0) - ($monthlyExpense ?? 0), 2) }} টাকা</div>
                    <div class="stat-title">নিট আয়</div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stat-card">
                <div class="card-body text-center">
                    <div class="stat-icon text-dark">
                        <i class="fas fa-calendar-times"></i>
                    </div>
                    <div class="stat-number">{{ ($overdueSavings ?? 0) + ($overdueLoans ?? 0) }}</div>
                    <div class="stat-title">বকেয়া পেমেন্ট</div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- চার্ট এবং কার্যক্রম -->
    <div class="row mb-4">
        <!-- আয়-ব্যয় চার্ট -->
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-chart-line me-2"></i>আয়-ব্যয় বিশ্লেষণ ({{ now()->year }})
                    </h5>
                </div>
                <div class="card-body">
                    <div class="chart-container">
                        <canvas id="incomeExpenseChart"></canvas>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- সাম্প্রতিক কার্যক্রম -->
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-history me-2"></i>সাম্প্রতিক কার্যক্রম
                    </h5>
                </div>
                <div class="card-body p-0">
                    @if(isset($recentActivities) && count($recentActivities) > 0)
                    <div class="list-group list-group-flush">
                        @foreach($recentActivities as $activity)
                        <div class="activity-item">
                            <div class="d-flex">
                                <div class="activity-icon bg-{{ $activity['color'] ?? 'primary' }} text-white me-3">
                                    <i class="{{ $activity['icon'] ?? 'fas fa-circle' }}"></i>
                                </div>
                                <div class="flex-grow-1">
                                    <h6 class="mb-1">{{ $activity['title'] ?? 'শিরোনাম নেই' }}</h6>
                                    <p class="mb-1 small text-muted">{{ $activity['description'] ?? '' }}</p>
                                    <small class="text-muted">
                                        <i class="fas fa-clock me-1"></i>{{ $activity['time'] ?? '' }}
                                    </small>
                                </div>
                            </div>
                        </div>
                        @endforeach
                    </div>
                    @else
                    <div class="text-center py-5">
                        <i class="fas fa-history fa-3x text-muted mb-3"></i>
                        <p class="text-muted">কোনো সাম্প্রতিক কার্যক্রম নেই</p>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
    
    <!-- নোটিশ এবং মিটিং -->
    <div class="row mb-4">
        <!-- সাম্প্রতিক নোটিশ -->
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="card-title mb-0">
                            <i class="fas fa-bullhorn me-2"></i>সাম্প্রতিক নোটিশ
                        </h5>
                        <a href="{{ route('notices.index') }}" class="btn btn-sm btn-outline-primary">
                            সব দেখুন
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    @if(isset($recentNotices) && count($recentNotices) > 0)
                    @foreach($recentNotices as $notice)
                    <div class="notice-card p-3 mb-3 {{ $notice->is_important ? 'important' : '' }}">
                        <h6 class="mb-2">
                            <a href="{{ route('notices.show', $notice) }}" class="text-decoration-none">
                                {{ $notice->title }}
                            </a>
                            @if($notice->is_important)
                            <span class="badge bg-danger ms-2">গুরুত্বপূর্ণ</span>
                            @endif
                        </h6>
                        <p class="mb-2 small text-muted">{{ Str::limit(strip_tags($notice->content ?? ''), 100) }}</p>
                        <div class="d-flex justify-content-between">
                            <small class="text-muted">
                                <i class="fas fa-user me-1"></i>
                                {{ $notice->creator->name ?? 'অজানা' }}
                            </small>
                            <small class="text-muted">
                                <i class="fas fa-calendar me-1"></i>
                                {{ $notice->created_at ? $notice->created_at->format('d/m/Y') : '' }}
                            </small>
                        </div>
                    </div>
                    @endforeach
                    @else
                    <div class="text-center py-4">
                        <i class="fas fa-bell-slash fa-2x text-muted mb-3"></i>
                        <p class="text-muted">কোনো নোটিশ পাওয়া যায়নি</p>
                    </div>
                    @endif
                </div>
            </div>
        </div>
        
        <!-- আসন্ন মিটিং -->
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="card-title mb-0">
                            <i class="fas fa-calendar-alt me-2"></i>আসন্ন মিটিং
                        </h5>
                        <a href="{{ route('meetings.index') }}" class="btn btn-sm btn-outline-primary">
                            সব দেখুন
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    @if(isset($upcomingMeetings) && count($upcomingMeetings) > 0)
                    @foreach($upcomingMeetings as $meeting)
                    <div class="notice-card p-3 mb-3">
                        <h6 class="mb-2">{{ $meeting->title }}</h6>
                        <p class="mb-2 small text-muted">
                            <i class="fas fa-map-marker-alt me-1"></i>{{ $meeting->location ?? $meeting->venue ?? 'অজানা' }}
                        </p>
                        <div class="d-flex justify-content-between">
                            <small class="text-muted">
                                <i class="fas fa-calendar-day me-1"></i>
                                {{ $meeting->date ? date('d/m/Y', strtotime($meeting->date)) : '' }}
                            </small>
                            <small class="text-muted">
                                <i class="fas fa-clock me-1"></i>
                                {{ isset($meeting->time) ? date('h:i A', strtotime($meeting->time)) : '' }}
                            </small>
                        </div>
                        <div class="mt-2">
                            <a href="{{ route('meetings.show', $meeting) }}" class="btn btn-sm btn-outline-primary">
                                বিস্তারিত
                            </a>
                        </div>
                    </div>
                    @endforeach
                    @else
                    <div class="text-center py-4">
                        <i class="fas fa-calendar-times fa-2x text-muted mb-3"></i>
                        <p class="text-muted">কোনো আসন্ন মিটিং নেই</p>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
    
    <!-- দ্রুত লিংক -->
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-bolt me-2"></i>দ্রুত লিংক
                    </h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3 mb-3">
                            <a href="{{ route('members.index') }}" class="btn btn-outline-primary w-100 d-flex align-items-center justify-content-center py-3">
                                <i class="fas fa-users fa-2x me-3"></i>
                                <div>
                                    <div>সদস্য ব্যবস্থাপনা</div>
                                    <small class="text-muted">{{ $totalMembers ?? 0 }} সদস্য</small>
                                </div>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{{ route('savings.create') }}" class="btn btn-outline-success w-100 d-flex align-items-center justify-content-center py-3">
                                <i class="fas fa-piggy-bank fa-2x me-3"></i>
                                <div>
                                    <div>সঞ্চয় সংগ্রহ</div>
                                    <small class="text-muted">{{ number_format($totalSavings ?? 0, 2) }} টাকা</small>
                                </div>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{{ route('loans.index') }}" class="btn btn-outline-danger w-100 d-flex align-items-center justify-content-center py-3">
                                <i class="fas fa-hand-holding-usd fa-2x me-3"></i>
                                <div>
                                    <div>ঋণ ব্যবস্থাপনা</div>
                                    <small class="text-muted">{{ number_format($totalLoans ?? 0, 2) }} টাকা</small>
                                </div>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            @if(Route::has('reports.summary'))
                            <a href="{{ route('reports.summary') }}" class="btn btn-outline-info w-100 d-flex align-items-center justify-content-center py-3">
                                <i class="fas fa-chart-bar fa-2x me-3"></i>
                                <div>
                                    <div>আর্থিক রিপোর্ট</div>
                                    <small class="text-muted">সারাংশ রিপোর্ট</small>
                                </div>
                            </a>
                            @else
                            <a href="#" class="btn btn-outline-info w-100 d-flex align-items-center justify-content-center py-3 disabled">
                                <i class="fas fa-chart-bar fa-2x me-3"></i>
                                <div>
                                    <div>রিপোর্ট</div>
                                    <small class="text-muted">শীঘ্রই আসছে</small>
                                </div>
                            </a>
                            @endif
                        </div>
                    </div>
                    
                    <!-- দ্বিতীয় সারির দ্রুত লিংক -->
                    <div class="row mt-2">
                        <div class="col-md-3 mb-3">
                            <a href="{{ route('incomes.index') }}" class="btn btn-outline-info w-100 d-flex align-items-center justify-content-center py-3">
                                <i class="fas fa-money-bill-wave fa-2x me-3"></i>
                                <div>
                                    <div>আয় ব্যবস্থাপনা</div>
                                    <small class="text-muted">{{ number_format($monthlyIncome ?? 0, 2) }} টাকা</small>
                                </div>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            <a href="{{ route('expenses.index') }}" class="btn btn-outline-purple w-100 d-flex align-items-center justify-content-center py-3">
                                <i class="fas fa-receipt fa-2x me-3"></i>
                                <div>
                                    <div>ব্যয় ব্যবস্থাপনা</div>
                                    <small class="text-muted">{{ number_format($monthlyExpense ?? 0, 2) }} টাকা</small>
                                </div>
                            </a>
                        </div>
                        <div class="col-md-3 mb-3">
                            @if(Route::has('reports.monthly'))
                            <a href="{{ route('reports.monthly') }}" class="btn btn-outline-secondary w-100 d-flex align-items-center justify-content-center py-3">
                                <i class="fas fa-calendar-alt fa-2x me-3"></i>
                                <div>
                                    <div>মাসিক রিপোর্ট</div>
                                    <small class="text-muted">আয়-ব্যয় বিবরণী</small>
                                </div>
                            </a>
                            @else
                            <a href="#" class="btn btn-outline-secondary w-100 d-flex align-items-center justify-content-center py-3 disabled">
                                <i class="fas fa-calendar-alt fa-2x me-3"></i>
                                <div>
                                    <div>মাসিক রিপোর্ট</div>
                                    <small class="text-muted">শীঘ্রই আসছে</small>
                                </div>
                            </a>
                            @endif
                        </div>
                        <div class="col-md-3 mb-3">
                            @if(Route::has('reports.income-expense'))
                            <a href="{{ route('reports.income-expense') }}" class="btn btn-outline-warning w-100 d-flex align-items-center justify-content-center py-3">
                                <i class="fas fa-chart-line fa-2x me-3"></i>
                                <div>
                                    <div>আয়-ব্যয় রিপোর্ট</div>
                                    <small class="text-muted">গ্রাফিক্যাল বিশ্লেষণ</small>
                                </div>
                            </a>
                            @else
                            <a href="#" class="btn btn-outline-warning w-100 d-flex align-items-center justify-content-center py-3 disabled">
                                <i class="fas fa-chart-line fa-2x me-3"></i>
                                <div>
                                    <div>রিপোর্ট</div>
                                    <small class="text-muted">শীঘ্রই আসছে</small>
                                </div>
                            </a>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
$(document).ready(function() {
    // আয়-ব্যয় চার্ট
    @if(isset($incomeExpenseData) && count($incomeExpenseData) > 0)
    const ctx = document.getElementById('incomeExpenseChart').getContext('2d');
    
    const incomeExpenseChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: [
                @foreach($incomeExpenseData as $data)
                    '{{ $data['month'] ?? '' }}',
                @endforeach
            ],
            datasets: [
                {
                    label: 'আয়',
                    data: [
                        @foreach($incomeExpenseData as $data)
                            {{ $data['income'] ?? 0 }},
                        @endforeach
                    ],
                    borderColor: '#17a2b8',
                    backgroundColor: 'rgba(23, 162, 184, 0.1)',
                    tension: 0.4,
                    fill: true
                },
                {
                    label: 'ব্যয়',
                    data: [
                        @foreach($incomeExpenseData as $data)
                            {{ $data['expense'] ?? 0 }},
                        @endforeach
                    ],
                    borderColor: '#dc3545',
                    backgroundColor: 'rgba(220, 53, 69, 0.1)',
                    tension: 0.4,
                    fill: true
                }
            ]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    position: 'top',
                },
                tooltip: {
                    mode: 'index',
                    intersect: false,
                    callbacks: {
                        label: function(context) {
                            return context.dataset.label + ': ' + context.parsed.y.toFixed(2) + ' টাকা';
                        }
                    }
                }
            },
            scales: {
                y: {
                    beginAtZero: true,
                    ticks: {
                        callback: function(value) {
                            return value.toLocaleString() + ' টাকা';
                        }
                    }
                }
            }
        }
    });
    @else
    // যদি ডাটা না থাকে, তাহলে একটি মেসেজ দেখান
    if(document.getElementById('incomeExpenseChart')) {
        document.getElementById('incomeExpenseChart').getContext('2d').fillText('কোনো ডাটা নেই', 100, 100);
    }
    @endif
    
    // কার্ড হোভার এফেক্ট
    $('.stat-card').hover(
        function() {
            $(this).addClass('shadow-lg');
        },
        function() {
            $(this).removeClass('shadow-lg');
        }
    );
    
    // অটো রিফ্রেশ (ঐচ্ছিক) - প্রতি ৫ মিনিটে
    @if(Route::has('api.dashboard.stats'))
    setInterval(function() {
        $.ajax({
            url: "{{ route('api.dashboard.stats') }}",
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                if(data.success) {
                    // এখানে আপনার আপডেট লজিক
                    console.log('Dashboard stats updated', data);
                }
            },
            error: function(xhr, status, error) {
                console.log('Dashboard stats update failed:', error);
            }
        });
    }, 300000); // ৫ মিনিট
    @endif
});
</script>
@endsection