@extends('layouts.app')

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

@section('styles')
<style>
    .expense-card {
        border-left: 4px solid #dc3545;
        border-radius: 8px;
        margin-bottom: 15px;
        transition: all 0.3s;
    }
    
    .expense-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    }
    
    .expense-card.approved {
        border-left-color: #28a745;
    }
    
    .expense-card.pending {
        border-left-color: #ffc107;
    }
    
    .expense-card.rejected {
        border-left-color: #6c757d;
    }
    
    .expense-amount {
        font-size: 1.2rem;
        font-weight: 600;
        color: #dc3545;
    }
    
    .expense-amount.approved {
        color: #28a745;
    }
    
    .expense-date {
        background-color: #f8f9fa;
        padding: 5px 10px;
        border-radius: 5px;
        font-size: 0.9rem;
    }
    
    .status-badge {
        padding: 5px 10px;
        border-radius: 20px;
        font-size: 0.8rem;
        font-weight: 600;
        display: inline-block;
    }
    
    .badge-pending {
        background-color: #fff3cd;
        color: #856404;
    }
    
    .badge-approved {
        background-color: #d4edda;
        color: #155724;
    }
    
    .badge-rejected {
        background-color: #f8d7da;
        color: #721c24;
    }
    
    .stat-card {
        border: none;
        border-radius: 10px;
        box-shadow: 0 2px 6px rgba(0,0,0,0.1);
        transition: all 0.3s;
    }
    
    .stat-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 5px 15px rgba(0,0,0,0.2);
    }
    
    .stat-card .card-title {
        font-size: 1.5rem;
        font-weight: 700;
        color: #1a5f7a;
        margin-bottom: 5px;
    }
    
    .stat-card .card-text {
        color: #6c757d;
        margin-bottom: 0;
    }
    
    .filter-section {
        background: #f8f9fa;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
    }
    
    .btn-group-custom {
        gap: 5px;
    }
    
    /* প্রিন্ট স্টাইল */
    @media print {
        .sidebar, .navbar, .footer, .btn-group, .btn, .filter-section, .pagination {
            display: none !important;
        }
        
        .main-content {
            margin-left: 0 !important;
            padding-top: 0 !important;
        }
        
        .card {
            border: none !important;
        }
    }
</style>
@endsection

@section('content')
<div class="container-fluid">
    <!-- ব্রেডক্রাম্ব -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('dashboard') }}" class="text-decoration-none">ড্যাশবোর্ড</a></li>
            <li class="breadcrumb-item active" aria-current="page">ব্যয় ব্যবস্থাপনা</li>
        </ol>
    </nav>
    
    <!-- স্ট্যাটাস মেসেজ -->
    @if(session('success'))
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <i class="fas fa-check-circle me-2"></i>{{ session('success') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif
    
    @if(session('error'))
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <i class="fas fa-exclamation-circle me-2"></i>{{ session('error') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif
    
    <!-- ফিল্টার সেকশন -->
    <div class="filter-section">
        <form method="GET" action="{{ route('expenses.index') }}" id="filterForm">
            <div class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">ক্যাটাগরি</label>
                    <select name="category_id" class="form-select">
                        <option value="">সব ক্যাটাগরি</option>
                        @foreach($categories as $category)
                            <option value="{{ $category->id }}" {{ request('category_id') == $category->id ? 'selected' : '' }}>
                                {{ $category->name_bn ?? $category->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                
                <div class="col-md-3">
                    <label class="form-label">স্ট্যাটাস</label>
                    <select name="status" class="form-select">
                        <option value="">সব স্ট্যাটাস</option>
                        <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>মুলতুবি</option>
                        <option value="approved" {{ request('status') == 'approved' ? 'selected' : '' }}>অনুমোদিত</option>
                        <option value="rejected" {{ request('status') == 'rejected' ? 'selected' : '' }}>বাতিল</option>
                    </select>
                </div>
                
                <div class="col-md-2">
                    <label class="form-label">শুরু তারিখ</label>
                    <input type="date" name="from_date" class="form-control" value="{{ request('from_date') }}">
                </div>
                
                <div class="col-md-2">
                    <label class="form-label">শেষ তারিখ</label>
                    <input type="date" name="to_date" class="form-control" value="{{ request('to_date') }}">
                </div>
                
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary me-2">
                        <i class="fas fa-search me-1"></i>খুঁজুন
                    </button>
                    <button type="button" class="btn btn-secondary" id="resetFilter">
                        <i class="fas fa-redo me-1"></i>রিসেট
                    </button>
                </div>
            </div>
        </form>
    </div>
    
    <!-- পরিসংখ্যান কার্ড -->
    <div class="row mb-4">
        <div class="col-md-3 mb-3">
            <div class="card stat-card">
                <div class="card-body text-center">
                    <h5 class="card-title">{{ number_format($totalExpense ?? 0, 2) }}</h5>
                    <p class="card-text">মোট ব্যয়</p>
                    <small class="text-success">{{ $expenses->total() ?? 0 }} টি রেকর্ড</small>
                </div>
            </div>
        </div>
        
        <div class="col-md-3 mb-3">
            <div class="card stat-card">
                <div class="card-body text-center">
                    <h5 class="card-title">{{ number_format($monthlyExpense ?? 0, 2) }}</h5>
                    <p class="card-text">এই মাসের ব্যয়</p>
                    <small class="text-success">{{ $expenses->where('status', 'approved')->count() ?? 0 }} টি অনুমোদিত</small>
                </div>
            </div>
        </div>
        
        <div class="col-md-3 mb-3">
            <div class="card stat-card">
                <div class="card-body text-center">
                    <h5 class="card-title">{{ $expenses->where('status', 'pending')->count() ?? 0 }}</h5>
                    <p class="card-text">মুলতুবি ব্যয়</p>
                    <small class="text-warning">অনুমোদনের অপেক্ষায়</small>
                </div>
            </div>
        </div>
        
        <div class="col-md-3 mb-3">
            <div class="card stat-card">
                <div class="card-body text-center">
                    <h5 class="card-title">{{ $expenses->where('status', 'approved')->count() ?? 0 }}</h5>
                    <p class="card-text">অনুমোদিত ব্যয়</p>
                    <small class="text-success">মোট রেকর্ডের {{ round(($expenses->where('status', 'approved')->count() / max($expenses->total(), 1)) * 100) }}%</small>
                </div>
            </div>
        </div>
    </div>
    
    <!-- একশন বাটন -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="d-flex flex-wrap justify-content-between align-items-center gap-2">
                <div class="btn-group btn-group-custom" role="group">
                    <a href="{{ route('expenses.index') }}" class="btn btn-outline-primary {{ !request()->has('status') ? 'active' : '' }}">
                        <i class="fas fa-list me-2"></i>সকল ব্যয়
                    </a>
                    <a href="{{ route('expenses.report') }}" class="btn btn-outline-info">
                        <i class="fas fa-chart-bar me-2"></i>রিপোর্ট
                    </a>
                    <a href="{{ route('expenses.monthly-report') }}" class="btn btn-outline-success">
                        <i class="fas fa-calendar-alt me-2"></i>মাসিক রিপোর্ট
                    </a>
                </div>
                
                <div>
                    <a href="{{ route('expenses.create') }}" class="btn btn-primary">
                        <i class="fas fa-plus me-2"></i>নতুন ব্যয় যোগ করুন
                    </a>
                </div>
            </div>
        </div>
    </div>
    
    <!-- ব্যয় তালিকা -->
    <div class="card shadow">
        <div class="card-header bg-white py-3">
            <div class="d-flex justify-content-between align-items-center">
                <h5 class="card-title mb-0">
                    <i class="fas fa-money-bill-wave me-2 text-danger"></i>
                    ব্যয় তালিকা
                </h5>
                <span class="badge bg-secondary">মোট {{ $expenses->total() }} টি রেকর্ড</span>
            </div>
        </div>
        
        <div class="card-body">
            @if($expenses->isEmpty())
            <div class="text-center py-5">
                <i class="fas fa-receipt fa-4x text-muted mb-3"></i>
                <h4>কোনো ব্যয় রেকর্ড পাওয়া যায়নি</h4>
                <p class="text-muted mb-3">আপনার প্রথম ব্যয় রেকর্ড যোগ করুন।</p>
                <a href="{{ route('expenses.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>প্রথম ব্যয় যোগ করুন
                </a>
            </div>
            @else
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th width="8%">তারিখ</th>
                            <th width="20%">শিরোনাম</th>
                            <th width="15%">ক্যাটাগরি</th>
                            <th width="15%">পরিমাণ</th>
                            <th width="15%">পেমেন্ট পদ্ধতি</th>
                            <th width="10%">স্ট্যাটাস</th>
                            <th width="17%">কার্যক্রম</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($expenses as $expense)
                        <tr class="expense-card {{ $expense->status ?? 'pending' }}">
                            <td>
                                <div class="expense-date">
                                    <i class="far fa-calendar-alt me-1"></i>
                                    {{ $expense->expense_date ? $expense->expense_date->format('d/m/Y') : ($expense->date ? date('d/m/Y', strtotime($expense->date)) : 'N/A') }}
                                </div>
                            </td>
                            <td>
                                <strong>{{ $expense->title }}</strong>
                                @if($expense->description)
                                <small class="d-block text-muted text-truncate" style="max-width: 250px;">
                                    <i class="fas fa-align-left me-1"></i>{{ Str::limit($expense->description, 40) }}
                                </small>
                                @endif
                                @if($expense->voucher_number)
                                <small class="d-block text-info">
                                    <i class="fas fa-hashtag me-1"></i>{{ $expense->voucher_number }}
                                </small>
                                @endif
                            </td>
                            <td>
                                @if($expense->category)
                                <span class="badge bg-secondary">
                                    {{ $expense->category->name_bn ?? $expense->category->name }}
                                </span>
                                @else
                                <span class="badge bg-light text-dark">N/A</span>
                                @endif
                            </td>
                            <td>
                                <span class="expense-amount {{ $expense->status }}">
                                    ৳ {{ number_format($expense->amount, 2) }}
                                </span>
                            </td>
                            <td>
                                <span class="badge bg-light text-dark">
                                    <i class="fas fa-{{ $expense->payment_method == 'cash' ? 'money-bill' : ($expense->payment_method == 'bank' ? 'university' : 'mobile-alt') }} me-1"></i>
                                    {{ $expense->payment_method_in_bangla ?? $expense->payment_method }}
                                </span>
                            </td>
                            <td>
                                @php
                                    $statusClass = match($expense->status) {
                                        'approved' => 'approved',
                                        'pending' => 'pending',
                                        'rejected' => 'rejected',
                                        default => 'pending'
                                    };
                                @endphp
                                <span class="status-badge badge-{{ $statusClass }}">
                                    {{ $expense->status_in_bangla ?? $expense->status }}
                                </span>
                            </td>
                            <td>
                                <div class="btn-group btn-group-sm" role="group">
                                    <a href="{{ route('expenses.show', $expense) }}" class="btn btn-outline-info" title="দেখুন" data-bs-toggle="tooltip">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    <a href="{{ route('expenses.edit', $expense) }}" class="btn btn-outline-warning" title="সম্পাদনা" data-bs-toggle="tooltip">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    @if($expense->receipt_attachment)
                                    <a href="{{ route('expenses.download-attachment', $expense) }}" class="btn btn-outline-success" title="রশিদ ডাউনলোড" data-bs-toggle="tooltip">
                                        <i class="fas fa-download"></i>
                                    </a>
                                    @endif
                                    @if($expense->status == 'pending')
                                    <a href="{{ route('expenses.approve', $expense) }}" class="btn btn-outline-success" title="অনুমোদন" data-bs-toggle="tooltip" onclick="return confirm('এই ব্যয় অনুমোদন করতে চান?')">
                                        <i class="fas fa-check"></i>
                                    </a>
                                    @endif
                                    <button type="button" class="btn btn-outline-danger" title="ডিলিট" 
                                            onclick="deleteExpense({{ $expense->id }}, '{{ $expense->title }}')">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            
            <!-- পেজিনেশন -->
            @if($expenses->hasPages())
            <div class="d-flex justify-content-between align-items-center mt-4">
                <div class="text-muted">
                    দেখানো হচ্ছে {{ $expenses->firstItem() }} থেকে {{ $expenses->lastItem() }} পর্যন্ত (মোট {{ $expenses->total() }} টি)
                </div>
                <div>
                    {{ $expenses->withQueryString()->links() }}
                </div>
            </div>
            @endif
            @endif
        </div>
    </div>
</div>

<!-- ডিলিট কনফার্মেশন মডাল -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title">
                    <i class="fas fa-exclamation-triangle me-2"></i>
                    ব্যয় রেকর্ড ডিলিট করুন
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="text-center mb-3">
                    <i class="fas fa-trash-alt fa-4x text-danger mb-3"></i>
                    <h5 class="mb-3">আপনি কি নিশ্চিত?</h5>
                    <p class="mb-2">আপনি "<strong id="deleteExpenseTitle"></strong>" ব্যয় রেকর্ডটি ডিলিট করতে চাচ্ছেন।</p>
                    <p class="text-danger mt-3">
                        <i class="fas fa-info-circle me-1"></i>
                        এই কাজটি বাতিল করা যাবে না। ডিলিট করার পর ডাটা পুনরুদ্ধার করা সম্ভব হবে না।
                    </p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="fas fa-times me-1"></i>বাতিল
                </button>
                <form id="deleteForm" method="POST" style="display: inline;">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">
                        <i class="fas fa-trash me-1"></i>হ্যাঁ, ডিলিট করুন
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        // টুলটিপ ইনিশিয়ালাইজ
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function(tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl);
        });
        
        // ফিল্টার ফর্ম সাবমিট
        $('#filterForm').on('submit', function(e) {
            e.preventDefault();
            const formData = $(this).serialize();
            window.location.href = "{{ route('expenses.index') }}?" + formData;
        });
        
        // ফিল্টার রিসেট
        $('#resetFilter').click(function() {
            $('#filterForm')[0].reset();
            window.location.href = "{{ route('expenses.index') }}";
        });
        
        // এলার্ট অটো ডিসমিস
        setTimeout(function() {
            $('.alert').alert('close');
        }, 5000);
    });
    
    // ডিলিট ফাংশন
    function deleteExpense(id, title) {
        document.getElementById('deleteExpenseTitle').textContent = title;
        document.getElementById('deleteForm').action = '{{ url("expenses") }}/' + id;
        
        var deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
        deleteModal.show();
    }
    
    // স্ট্যাটাস ফিল্টার অ্যাকটিভ
    @if(request('status'))
        $('.btn-group a').removeClass('active');
        // কাস্টম স্ট্যাটাস হাইলাইট
    @endif
</script>
@endsection