@extends('layouts.app')

@section('title', 'ঋণ তালিকা')

@section('content')
<div class="container-fluid">
    <!-- ব্রেডক্রাম্ব -->
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">ড্যাশবোর্ড</a></li>
            <li class="breadcrumb-item active" aria-current="page">ঋণ তালিকা</li>
        </ol>
    </nav>
    
    <div class="card shadow">
        <div class="card-header py-3 d-flex justify-content-between align-items-center">
            <h6 class="m-0 font-weight-bold">ঋণ তালিকা</h6>
            <div>
                <a href="{{ route('loans.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-1"></i>নতুন ঋণ
                </a>
            </div>
        </div>
        <div class="card-body">
            <!-- ফিল্টার ফর্ম -->
            <form method="GET" action="{{ route('loans.index') }}" class="row g-3 mb-4">
                <div class="col-md-3">
                    <input type="text" name="search" class="form-control" placeholder="ঋণ নং বা সদস্য নাম" 
                           value="{{ request('search') }}">
                </div>
                <div class="col-md-2">
                    <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="disbursed" {{ request('status') == 'disbursed' ? 'selected' : '' }}>বিতরণকৃত</option>
                        <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>চলমান</option>
                        <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>সম্পন্ন</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <select name="member_id" class="form-select">
                        <option value="">সব সদস্য</option>
                        @foreach($members ?? [] as $member)
                        <option value="{{ $member->member_id }}" {{ request('member_id') == $member->member_id ? 'selected' : '' }}>
                            {{ $member->member_id }} - {{ $member->name }}
                        </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-search me-1"></i>খুঁজুন
                    </button>
                </div>
                <div class="col-md-2">
                    <a href="{{ route('loans.index') }}" class="btn btn-secondary w-100">
                        <i class="fas fa-redo me-1"></i>রিসেট
                    </a>
                </div>
            </form>
            
            <!-- ঋণ টেবিল -->
            @if(isset($loans) && $loans->count() > 0)
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="table-dark">
                        <tr>
                            <th width="50">#</th>
                            <th>ঋণ নং</th>
                            <th>সদস্য</th>
                            <th>ঋণের পরিমাণ</th>
                            <th>মেয়াদ</th>
                            <th>স্ট্যাটাস</th>
                            <th>তারিখ</th>
                            <th>কার্যক্রম</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($loans as $loan)
                        <tr>
                            <td>{{ $loop->iteration }}</td>
                            <td><strong>{{ $loan->loan_number ?? 'N/A' }}</strong></td>
                            <td>
                                @if($loan->member)
                                {{ $loan->member->name }} ({{ $loan->member->member_id }})
                                @else
                                <span class="text-danger">সদস্য পাওয়া যায়নি</span>
                                @endif
                            </td>
                            <td>{{ number_format($loan->loan_amount ?? 0, 2) }} ৳</td>
                            <td>{{ $loan->duration_months ?? 0 }} মাস</td>
                            <td>
                                @php
                                    $status = $loan->status ?? 'pending';
                                    $statusColors = [
                                        'pending' => 'warning',
                                        'approved' => 'info',
                                        'disbursed' => 'primary',
                                        'active' => 'success',
                                        'completed' => 'secondary',
                                        'defaulted' => 'danger'
                                    ];
                                    $color = $statusColors[$status] ?? 'secondary';
                                @endphp
                                <span class="badge bg-{{ $color }}">
                                    {{ $loan->getStatusInBanglaAttribute() ?? $status }}
                                </span>
                            </td>
                            <td>{{ isset($loan->loan_date) ? \Carbon\Carbon::parse($loan->loan_date)->format('d/m/Y') : 'N/A' }}</td>
                            <td>
                                <div class="btn-group btn-group-sm" role="group">
                                    <a href="{{ route('loans.show', $loan->id) }}" class="btn btn-info" title="দেখুন">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    @if(auth()->user()->user_type != 'member')
                                    <a href="{{ route('loans.edit', $loan->id) }}" class="btn btn-warning" title="সম্পাদনা">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            
            <!-- পেজিনেশন -->
            @if($loans->hasPages())
            <div class="d-flex justify-content-center mt-3">
                {{ $loans->withQueryString()->links() }}
            </div>
            @endif
            @else
            <div class="text-center py-4">
                <i class="fas fa-money-bill-wave fa-3x text-muted mb-3"></i>
                <h5 class="text-muted">কোন ঋণ পাওয়া যায়নি</h5>
                <p class="text-muted">নতুন ঋণ তৈরি করুন</p>
                <a href="{{ route('loans.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-1"></i>নতুন ঋণ তৈরি করুন
                </a>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // DataTable ইনিশিয়ালাইজ করুন (যদি jQuery DataTable ব্যবহার করেন)
        @if(isset($loans) && $loans->count() > 0)
        if (typeof $ !== 'undefined') {
            $('table').DataTable({
                language: {
                    url: '//cdn.datatables.net/plug-ins/1.13.6/i18n/bn.json'
                },
                pageLength: 25,
                order: [[1, 'desc']],
                responsive: true
            });
        }
        @endif
    });
</script>
@endsection