@extends('layouts.app')

@section('title', 'সারাংশ রিপোর্ট - ' . config('app.name', 'Laravel'))

@section('content')
<div class="container-fluid py-4">
    <!-- Header -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h2 class="mb-1"><i class="fas fa-chart-line me-2"></i>সারাংশ রিপোর্ট</h2>
                    <p class="text-muted mb-0">{{ now()->format('d F Y, h:i A') }}</p>
                </div>
                <div class="no-print">
                    <button onclick="window.print()" class="btn btn-print me-2">
                        <i class="fas fa-print me-1"></i> প্রিন্ট
                    </button>
                    <a href="{{ route('reports.monthly') }}" class="btn btn-outline-primary">
                        <i class="fas fa-calendar-alt me-1"></i> মাসিক রিপোর্ট
                    </a>
                </div>
            </div>
            <hr>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="row">
        <!-- সদস্য সংখ্যা -->
        <div class="col-xl-3 col-lg-4 col-md-6 mb-4">
            <div class="card stat-card bg-primary-light">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">মোট সদস্য</h6>
                            <h3 class="mb-0">{{ $total_members }}</h3>
                            <small class="text-success">
                                <i class="fas fa-user-check me-1"></i>
                                সক্রিয়: {{ $active_members }}
                            </small>
                        </div>
                        <div class="stat-icon text-primary">
                            <i class="fas fa-users"></i>
                        </div>
                    </div>
                    <div class="mt-3">
                        <small class="text-muted">
                            <i class="fas fa-user-plus me-1"></i>
                            এই মাসে নতুন: {{ $new_members_this_month }}
                        </small>
                    </div>
                </div>
            </div>
        </div>

        <!-- সঞ্চয় -->
        <div class="col-xl-3 col-lg-4 col-md-6 mb-4">
            <div class="card stat-card bg-success-light">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">মোট সঞ্চয়</h6>
                            <h3 class="mb-0">{{ number_format($total_savings, 2) }} <span class="currency">টাকা</span></h3>
                            <small class="text-primary">
                                <i class="fas fa-calendar-alt me-1"></i>
                                মাসিক: {{ number_format($monthly_savings, 2) }} টাকা
                            </small>
                        </div>
                        <div class="stat-icon text-success">
                            <i class="fas fa-piggy-bank"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- আয়-ব্যয় -->
        <div class="col-xl-3 col-lg-4 col-md-6 mb-4">
            <div class="card stat-card bg-info-light">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">আয়-ব্যয় ব্যালেন্স</h6>
                            <h3 class="mb-0">{{ number_format($balance, 2) }} <span class="currency">টাকা</span></h3>
                            <small class="{{ $balance >= 0 ? 'text-success' : 'text-danger' }}">
                                <i class="fas fa-balance-scale me-1"></i>
                                {{ $balance >= 0 ? 'সারপ্লাস' : 'ডেফিসিট' }}
                            </small>
                        </div>
                        <div class="stat-icon text-info">
                            <i class="fas fa-hand-holding-usd"></i>
                        </div>
                    </div>
                    <div class="mt-3">
                        <div class="row">
                            <div class="col-6">
                                <small class="text-muted">
                                    <i class="fas fa-arrow-down text-success me-1"></i>
                                    আয়: {{ number_format($total_income, 2) }}
                                </small>
                            </div>
                            <div class="col-6">
                                <small class="text-muted">
                                    <i class="fas fa-arrow-up text-danger me-1"></i>
                                    ব্যয়: {{ number_format($total_expense, 2) }}
                                </small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- লোন সারাংশ -->
        <div class="col-xl-3 col-lg-4 col-md-6 mb-4">
            <div class="card stat-card bg-warning-light">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">লোন সারাংশ</h6>
                            <h3 class="mb-0">{{ number_format($total_loan_amount, 2) }} <span class="currency">টাকা</span></h3>
                            <small class="text-info">
                                <i class="fas fa-running me-1"></i>
                                সক্রিয়: {{ $active_loans }}
                            </small>
                        </div>
                        <div class="stat-icon text-warning">
                            <i class="fas fa-handshake"></i>
                        </div>
                    </div>
                    <div class="mt-3">
                        <div class="row">
                            <div class="col-6">
                                <small class="text-success">
                                    <i class="fas fa-check-circle me-1"></i>
                                    পরিশোধিত: {{ number_format($total_paid, 2) }}
                                </small>
                            </div>
                            <div class="col-6">
                                <small class="text-danger">
                                    <i class="fas fa-clock me-1"></i>
                                    বাকি: {{ number_format($total_due, 2) }}
                                </small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- মাসিক আয়-ব্যয় -->
    <div class="row mb-4">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-money-bill-wave me-2"></i>মাসিক আয়</h5>
                </div>
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h4 class="total-box text-success">
                            {{ number_format($monthly_income, 2) }} <span class="currency">টাকা</span>
                        </h4>
                        <span class="badge bg-success">
                            <i class="fas fa-arrow-up me-1"></i> এই মাস
                        </span>
                    </div>
                    @if(isset($growth_rates['income']))
                    <p class="{{ $growth_rates['income'] >= 0 ? 'text-success' : 'text-danger' }}">
                        <i class="fas fa-{{ $growth_rates['income'] >= 0 ? 'arrow-up' : 'arrow-down' }} me-1"></i>
                        {{ $growth_rates['income'] >= 0 ? 'বৃদ্ধি' : 'হ্রাস' }}: {{ number_format(abs($growth_rates['income']), 1) }}%
                    </p>
                    @endif
                    <p class="text-muted">
                        <i class="fas fa-info-circle me-1"></i>
                        মোট আয়: {{ number_format($total_income, 2) }} টাকা
                    </p>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-receipt me-2"></i>মাসিক ব্যয়</h5>
                </div>
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h4 class="total-box text-danger">
                            {{ number_format($monthly_expense, 2) }} <span class="currency">টাকা</span>
                        </h4>
                        <span class="badge bg-danger">
                            <i class="fas fa-arrow-down me-1"></i> এই মাস
                        </span>
                    </div>
                    @if(isset($growth_rates['expense']))
                    <p class="{{ $growth_rates['expense'] >= 0 ? 'text-danger' : 'text-success' }}">
                        <i class="fas fa-{{ $growth_rates['expense'] >= 0 ? 'arrow-up' : 'arrow-down' }} me-1"></i>
                        {{ $growth_rates['expense'] >= 0 ? 'বৃদ্ধি' : 'হ্রাস' }}: {{ number_format(abs($growth_rates['expense']), 1) }}%
                    </p>
                    @endif
                    <p class="text-muted">
                        <i class="fas fa-info-circle me-1"></i>
                        মোট ব্যয়: {{ number_format($total_expense, 2) }} টাকা
                    </p>
                </div>
            </div>
        </div>
    </div>

    <!-- বৃদ্ধির হার -->
    @if(isset($growth_rates) && count($growth_rates) > 0)
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-chart-line me-2"></i>বৃদ্ধির হার (গত মাসের তুলনায়)</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        @foreach(['income' => 'আয়', 'expense' => 'ব্যয়', 'savings' => 'সঞ্চয়', 'new_members' => 'নতুন সদস্য'] as $key => $label)
                            @if(isset($growth_rates[$key]))
                            <div class="col-md-3 col-sm-6 mb-3">
                                <div class="d-flex align-items-center">
                                    <div class="me-3">
                                        @if($key == 'income')
                                            <i class="fas fa-money-bill-wave fa-2x text-success"></i>
                                        @elseif($key == 'expense')
                                            <i class="fas fa-receipt fa-2x text-danger"></i>
                                        @elseif($key == 'savings')
                                            <i class="fas fa-piggy-bank fa-2x text-primary"></i>
                                        @else
                                            <i class="fas fa-user-plus fa-2x text-info"></i>
                                        @endif
                                    </div>
                                    <div>
                                        <h6 class="mb-0">{{ $label }}</h6>
                                        <span class="{{ $growth_rates[$key] >= 0 ? ($key == 'expense' ? 'text-danger' : 'text-success') : 'text-danger' }}">
                                            <i class="fas fa-{{ $growth_rates[$key] >= 0 ? 'arrow-up' : 'arrow-down' }} me-1"></i>
                                            {{ number_format($growth_rates[$key], 1) }}%
                                        </span>
                                    </div>
                                </div>
                            </div>
                            @endif
                        @endforeach
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endif

    <!-- সম্পদ -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-chart-pie me-2"></i>মোট সম্পদ</h5>
                </div>
                <div class="card-body">
                    <div class="row text-center">
                        <div class="col-md-4 mb-3">
                            <div class="p-3 border rounded">
                                <h4 class="text-primary">{{ number_format($total_assets, 2) }}</h4>
                                <p class="mb-0">মোট সম্পদ (টাকা)</p>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="p-3 border rounded">
                                <h4 class="text-success">{{ number_format($total_savings, 2) }}</h4>
                                <p class="mb-0">সঞ্চয় (টাকা)</p>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="p-3 border rounded">
                                <h4 class="text-warning">{{ number_format($total_loan_amount, 2) }}</h4>
                                <p class="mb-0">মোট ঋণ (টাকা)</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- সাম্প্রতিক লেনদেন -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-history me-2"></i>সাম্প্রতিক লেনদেন</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <!-- সঞ্চয় লেনদেন -->
                        <div class="col-md-4 mb-3 mb-md-0">
                            <h6 class="border-bottom pb-2">
                                <i class="fas fa-piggy-bank text-primary me-2"></i>সর্বশেষ সঞ্চয়
                            </h6>
                            @if(isset($recent_transactions['savings']) && count($recent_transactions['savings']) > 0)
                                @foreach($recent_transactions['savings'] as $saving)
                                    <div class="transaction-item border-primary">
                                        <div class="d-flex justify-content-between">
                                            <div>
                                                <strong>{{ $saving->member->name ?? 'অজানা সদস্য' }}</strong>
                                                <br>
                                                <small class="text-muted">
                                                    @if($saving->payment_date)
                                                        {{ \Carbon\Carbon::parse($saving->payment_date)->format('d/m/Y') }}
                                                    @else
                                                        তারিখ নেই
                                                    @endif
                                                </small>
                                            </div>
                                            <div class="text-end">
                                                <span class="text-success">{{ number_format($saving->amount, 2) }} টাকা</span>
                                                <br>
                                                <small class="text-muted">{{ $saving->month_year ?? 'তারিখ নেই' }}</small>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            @else
                                <p class="text-muted text-center py-3">কোনো সঞ্চয় লেনদেন পাওয়া যায়নি</p>
                            @endif
                        </div>

                        <!-- আয় লেনদেন -->
                        <div class="col-md-4 mb-3 mb-md-0">
                            <h6 class="border-bottom pb-2">
                                <i class="fas fa-money-bill-wave text-success me-2"></i>সর্বশেষ আয়
                            </h6>
                            @if(isset($recent_transactions['incomes']) && count($recent_transactions['incomes']) > 0)
                                @foreach($recent_transactions['incomes'] as $income)
                                    <div class="transaction-item border-success">
                                        <div class="d-flex justify-content-between">
                                            <div>
                                                <strong>{{ $income->category ?? 'সাধারণ আয়' }}</strong>
                                                <br>
                                                <small class="text-muted">
                                                    @if($income->income_date)
                                                        {{ \Carbon\Carbon::parse($income->income_date)->format('d/m/Y') }}
                                                    @else
                                                        তারিখ নেই
                                                    @endif
                                                </small>
                                            </div>
                                            <div class="text-end">
                                                <span class="text-success">{{ number_format($income->amount, 2) }} টাকা</span>
                                                <br>
                                                <small class="text-muted">
                                                    @if($income->description)
                                                        {{ Str::limit($income->description, 20) }}
                                                    @else
                                                        বিবরণ নেই
                                                    @endif
                                                </small>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            @else
                                <p class="text-muted text-center py-3">কোনো আয় লেনদেন পাওয়া যায়নি</p>
                            @endif
                        </div>

                        <!-- ব্যয় লেনদেন -->
                        <div class="col-md-4">
                            <h6 class="border-bottom pb-2">
                                <i class="fas fa-receipt text-danger me-2"></i>সর্বশেষ ব্যয়
                            </h6>
                            @if(isset($recent_transactions['expenses']) && count($recent_transactions['expenses']) > 0)
                                @foreach($recent_transactions['expenses'] as $expense)
                                    <div class="transaction-item border-danger">
                                        <div class="d-flex justify-content-between">
                                            <div>
                                                <strong>{{ $expense->category ?? 'সাধারণ ব্যয়' }}</strong>
                                                <br>
                                                <small class="text-muted">
                                                    @if($expense->expense_date)
                                                        {{ \Carbon\Carbon::parse($expense->expense_date)->format('d/m/Y') }}
                                                    @else
                                                        তারিখ নেই
                                                    @endif
                                                </small>
                                            </div>
                                            <div class="text-end">
                                                <span class="text-danger">{{ number_format($expense->amount, 2) }} টাকা</span>
                                                <br>
                                                <small class="text-muted">
                                                    @if($expense->description)
                                                        {{ Str::limit($expense->description, 20) }}
                                                    @else
                                                        বিবরণ নেই
                                                    @endif
                                                </small>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            @else
                                <p class="text-muted text-center py-3">কোনো ব্যয় লেনদেন পাওয়া যায়নি</p>
                            @endif
                        </div>
                    </div>
                    
                    <!-- লোন ইনস্টলমেন্ট -->
                    @if(isset($recent_transactions['loan_installments']) && count($recent_transactions['loan_installments']) > 0)
                    <div class="row mt-4">
                        <div class="col-12">
                            <h6 class="border-bottom pb-2">
                                <i class="fas fa-hand-holding-usd text-warning me-2"></i>সর্বশেষ লোন পরিশোধ
                            </h6>
                            <div class="table-responsive">
                                <table class="table table-sm">
                                    <thead>
                                        <tr>
                                            <th>সদস্য</th>
                                            <th>লোন নম্বর</th>
                                            <th>পরিমাণ</th>
                                            <th>তারিখ</th>
                                            <th>স্ট্যাটাস</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($recent_transactions['loan_installments'] as $installment)
                                            <tr>
                                                <td>{{ $installment->loan->member->name ?? 'অজানা' }}</td>
                                                <td>{{ $installment->loan->loan_number ?? 'N/A' }}</td>
                                                <td class="text-success">{{ number_format($installment->paid_amount, 2) }} টাকা</td>
                                                <td>
                                                    @if($installment->payment_date)
                                                        {{ \Carbon\Carbon::parse($installment->payment_date)->format('d/m/Y') }}
                                                    @else
                                                        তারিখ নেই
                                                    @endif
                                                </td>
                                                <td>
                                                    @if($installment->status == 'paid')
                                                        <span class="badge bg-success">পরিশোধিত</span>
                                                    @else
                                                        <span class="badge bg-danger">{{ $installment->status }}</span>
                                                    @endif
                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <!-- সারাংশ তথ্য -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-info-circle me-2"></i>অন্যান্য তথ্য</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 text-center">
                            <div class="summary-box">
                                <i class="fas fa-calendar-alt fa-2x text-primary mb-3"></i>
                                <h5>বর্তমান মাস</h5>
                                <h3 class="text-primary">{{ now()->format('F Y') }}</h3>
                            </div>
                        </div>
                        <div class="col-md-4 text-center">
                            <div class="summary-box">
                                <i class="fas fa-chart-bar fa-2x text-success mb-3"></i>
                                <h5>আয়-ব্যয় অনুপাত</h5>
                                @if($total_income > 0)
                                    <h3 class="text-success">
                                        {{ number_format(($total_expense / $total_income) * 100, 1) }}%
                                    </h3>
                                    <small class="text-muted">ব্যয়/আয় অনুপাত</small>
                                @else
                                    <h3 class="text-muted">0%</h3>
                                @endif
                            </div>
                        </div>
                        <div class="col-md-4 text-center">
                            <div class="summary-box">
                                <i class="fas fa-percentage fa-2x text-warning mb-3"></i>
                                <h5>লোন পরিশোধ হার</h5>
                                @if($total_loan_amount > 0)
                                    <h3 class="text-warning">
                                        {{ number_format(($total_paid / $total_loan_amount) * 100, 1) }}%
                                    </h3>
                                    <small class="text-muted">মোট পরিশোধিত</small>
                                @else
                                    <h3 class="text-muted">0%</h3>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- রিপোর্ট নেভিগেশন -->
    <div class="row mt-4 no-print">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-file-alt me-2"></i>অন্যান্য রিপোর্ট</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <div class="d-grid">
                                <a href="{{ route('reports.monthly-loan-saving') }}" class="btn btn-outline-primary btn-lg">
                                    <i class="fas fa-calendar-alt me-2"></i>মাসিক ঋণ ও সঞ্চয়
                                </a>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="d-grid">
                                <a href="{{ route('reports.monthly') }}" class="btn btn-outline-success btn-lg">
                                    <i class="fas fa-chart-bar me-2"></i>মাসিক রিপোর্ট
                                </a>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="d-grid">
                                <a href="{{ route('reports.loan') }}" class="btn btn-outline-warning btn-lg">
                                    <i class="fas fa-handshake me-2"></i>ঋণ রিপোর্ট
                                </a>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="d-grid">
                                <a href="{{ route('reports.member') }}" class="btn btn-outline-info btn-lg">
                                    <i class="fas fa-users me-2"></i>সদস্য রিপোর্ট
                                </a>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="d-grid">
                                <a href="{{ route('reports.balance-sheet') }}" class="btn btn-outline-dark btn-lg">
                                    <i class="fas fa-balance-scale me-2"></i>ব্যালেন্স শীট
                                </a>
                            </div>
                        </div>
                        <div class="col-md-4 mb-3">
                            <div class="d-grid">
                                <a href="{{ route('reports.income-expense') }}" class="btn btn-outline-secondary btn-lg">
                                    <i class="fas fa-exchange-alt me-2"></i>আয়-ব্যয় রিপোর্ট
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <div class="row mt-4 no-print">
        <div class="col-12 text-center">
            <hr>
            <p class="text-muted">
                <i class="fas fa-clock me-1"></i>
                রিপোর্ট তৈরি হয়েছে: {{ now()->format('d F Y, h:i A') }}
                | 
                <i class="fas fa-user me-1"></i>
                ব্যবহারকারী: {{ Auth::user()->name ?? 'অজানা' }}
            </p>
        </div>
    </div>
</div>

<style>
    .stat-card {
        transition: transform 0.3s;
        border-left: 4px solid;
    }
    .stat-card:hover {
        transform: translateY(-5px);
    }
    .stat-icon {
        font-size: 2.5rem;
        opacity: 0.8;
    }
    .bg-primary-light {
        background-color: rgba(102, 126, 234, 0.1) !important;
        border-left-color: #667eea !important;
    }
    .bg-success-light {
        background-color: rgba(40, 167, 69, 0.1) !important;
        border-left-color: #28a745 !important;
    }
    .bg-info-light {
        background-color: rgba(23, 162, 184, 0.1) !important;
        border-left-color: #17a2b8 !important;
    }
    .bg-warning-light {
        background-color: rgba(255, 193, 7, 0.1) !important;
        border-left-color: #ffc107 !important;
    }
    .bg-danger-light {
        background-color: rgba(220, 53, 69, 0.1) !important;
        border-left-color: #dc3545 !important;
    }
    .table th {
        background-color: #f8f9fa;
        font-weight: 600;
    }
    .btn-print {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border: none;
        color: white;
    }
    .btn-print:hover {
        color: white;
        opacity: 0.9;
    }
    .summary-box {
        background: white;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    }
    .total-box {
        font-size: 1.5rem;
        font-weight: bold;
        color: #2c3e50;
    }
    .currency {
        font-size: 0.9rem;
        color: #7f8c8d;
    }
    .transaction-item {
        border-left: 3px solid;
        padding-left: 15px;
        margin-bottom: 10px;
    }
    @media print {
        .no-print {
            display: none !important;
        }
        .card {
            box-shadow: none !important;
            border: 1px solid #dee2e6 !important;
        }
    }
</style>
@endsection