<div class="content-wrapper">
    <div class="container-xxl flex-grow-1 container-p-y">
        <div class="row">
            <div class="col">

                {{-- Page Header Card --}}
                <div class="card shadow mb-3">
                    <div class="card-header bg-primary text-white">
                        <div class="d-flex flex-wrap justify-content-between align-items-center w-100">

                            {{-- Title --}}
                            <h5 class="mb-2 mb-md-0 text-uppercase fw-bold d-flex align-items-center text-white">
                                <i class="menu-icon ti ti-filter me-2"></i>
                                Loan Arrears Report
                                @if(request('arrearsDate') || isset($arrearsDate))
                                    ({{ \Carbon\Carbon::parse(request('arrearsDate', $arrearsDate ?? now()))->format('d M Y') }})
                                @endif
                            </h5>

                            {{-- Date Filter --}}
                            <form method="GET" id="dateForm" class="d-flex flex-column flex-md-row align-items-stretch gap-2" action="{{ url('/loan_arrears_report') }}">
                                <div class="input-group input-group-sm">
                                    <span class="input-group-text bg-primary text-white border-light">
                                        <i class="ti ti-calendar"></i> Select date
                                    </span>
                                    <input type="date" 
                                           name="arrearsDate" 
                                           class="form-control form-control-sm bg-primary text-white border-light"
                                           value="{{ request('arrearsDate', $arrearsDate ?? date('Y-m-d')) }}">
                                </div>
                                <button type="submit" class="btn btn-light btn-sm">
                                    Apply Date
                                </button>
                            </form>
                        </div>
                    </div>
                </div>

                {{-- More Filters + Export Buttons --}}
                <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2">
                    <button class="btn btn-primary btn-sm fw-bold" type="button" 
                            data-bs-toggle="collapse" data-bs-target="#collapseFilters" 
                            aria-expanded="false">
                        <i class="ti ti-filter-pause"></i> More Filters
                    </button>

                    <div class="d-flex gap-2 flex-wrap">
                        <button id="btnExportPDF" class="btn btn-danger btn-sm">📄 PDF</button>
                        <button id="btnExportExcel" class="btn btn-success btn-sm">📊 Excel</button>
                    </div>
                </div>

                {{-- Collapse Filter Card --}}
                <div class="collapse mt-3" id="collapseFilters">
                    <div class="card card-body">
                        <div class="row mt-3">
                            <form method="GET" action="{{url('/loan_arrears_report') }}" class="d-flex justify-content-between w-100">
                                <input type="hidden" name="arrearsDate" value="{{ request('arrearsDate', $arrearsDate ?? date('Y-m-d')) }}">

                                {{-- Division --}}
                                <div class="col-md-3 me-2">
                                    <div class="form-group">
                                        <label for="divisionId" class="form-label fw-bold">Select Division</label>
                                        <select name="divisionId" id="divisionId" class="form-control" onchange="this.form.submit()">
                                            <option value="">All Division</option>
                                            @foreach ($getDivision as $division)
                                                <option value="{{ $division->id }}" {{ request('divisionId') == $division->id ? 'selected' : '' }}>
                                                    {{ $division->divisionName }}
                                                </option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>

                                {{-- Village --}}
                                <div class="col-md-3 me-2">
                                    <div class="form-group">
                                        <label for="villageId" class="form-label fw-bold">Select Village</label>
                                        <select name="villageId" id="villageId" class="form-control" onchange="this.form.submit()">
                                            <option value="">Select Village</option>
                                            @if(isset($villages))
                                                @foreach ($villages as $village)
                                                    <option value="{{ $village->id }}" {{ request('villageId') == $village->id ? 'selected' : '' }}>
                                                        {{ $village->villageName }}
                                                    </option>
                                                @endforeach
                                            @endif
                                        </select>
                                    </div>
                                </div>

                                {{-- Small Group --}}
                                <div class="col-md-3 me-2" id="selectSmallGroupDiv" style="display: {{ isset($smallGroups) && count($smallGroups) ? 'block' : 'none' }};">
                                    <div class="form-group">
                                        <label for="smallGroupId" class="form-label fw-bold">Select Small Group</label>
                                        <select name="smallGroupId" id="smallGroupId" class="form-control" onchange="this.form.submit()">
                                            <option value="">Select Small Group</option>
                                            @if(isset($smallGroups))
                                                @foreach ($smallGroups as $smallGroup)
                                                    <option value="{{ $smallGroup->id }}" {{ request('smallGroupId') == $smallGroup->id ? 'selected' : '' }}>
                                                        {{ $smallGroup->smallGroupName }}
                                                    </option>
                                                @endforeach
                                            @endif
                                        </select>
                                    </div>
                                </div>

                                {{-- Loan Amount --}}
                                <div class="col-md-3 me-2">
                                    <div class="form-group">
                                        <label class="form-label fw-bold">Loan Amount</label>
                                        <div class="d-flex">
                                            <input type="number" name="loanMin" id="loanMin" class="form-control me-1" placeholder="Min" value="{{ request('loanMin') }}">
                                            <input type="number" name="loanMax" id="loanMax" class="form-control" placeholder="Max" value="{{ request('loanMax') }}">
                                        </div>
                                    </div>
                                </div>

                            </form>
                        </div>
                    </div>
                </div>
                {{-- Loan Arrears Table --}}
<div class="table-responsive mt-4">
    <table id="loanReportTable" class="table table-sm table-striped table-bordered align-middle" style="min-width:1300px;">
        <thead class="table-light text-center sticky-top bg-light" style="top:0; z-index:2;">
            <tr>
                <th>#</th>
                <th>Division</th>
                <th>Village</th>
                <th>GN Division</th>
                <th>Small Group</th>
                <th>Member Name</th>
                <th>NIC Number</th>
                <th>Old Account Number</th>
                <th>Loan Officer</th>
                <th>Loan ID</th>
                <th>Loan Date</th>
                <th>Loan Amount</th>
                <th>Loan Outstanding</th>
                <th>Last Payment Date</th>
                <th>Due Terms</th>
                <th class="text-danger">Principal Arrears</th>
                <th>Due Days</th>
                <th class="text-warning">Interest Arrears</th>
                <th class="text-primary">Total Arrears</th>
            </tr>
        </thead>
        <tbody>
            @forelse ($getLoanData as $index => $loan)
                <tr 
                    @if($loan->interestDays > 90) class="table-danger" 
                    @elseif(($loan->totalArrears + $loan->interestArrears) > 10000) class="table-warning" 
                    @endif
                >
                    <td>{{ $index + 1 }}</td>
                    <td>
                        <a href="{{ route('division_details', ['id' => encrypt($loan->divisionId)]) }}">
                            {{ $loan->divisionName ?? '-' }}
                        </a>
                    </td>
                    <td>
                        <a href="{{ route('village_details', ['id' => encrypt($loan->villageId)]) }}">
                            {{ $loan->villageName ?? '-' }}
                        </a>
                    </td>
                    <td>{{ $loan->gnDivisionName ?? '' }}</td>
                    <td>
                        <a href="{{ route('smallgroup_details', ['id' => encrypt($loan->smallGroupId)]) }}">
                            {{ $loan->smallGroupName ?? '-' }}
                        </a>
                    </td>
                    <td>
                        @php $encryptedId = Crypt::encrypt($loan->memberId); @endphp
                        <a href="{{ route('view_member', $encryptedId) }}">
                            {{ $loan->firstName }} {{ $loan->lastName }}
                        </a>
                    </td>

                    <td>{{ $loan->nicNumber }}</td>
                    <td>{{ $loan->oldAccountNumber }}</td>
                    <td>{{ $loan->loanOfficerName ?? '-' }}</td>
                    <td>
                        <a href="{{ route('view_loan_details', Crypt::encrypt($loan->id)) }}">
                            {{ $loan->loanId }}
                        </a>
                    </td>
                    <td>@if($loan->loanDate){{ \Carbon\Carbon::parse($loan->loanDate)->format('d M Y') }}@else - @endif</td>
                    <td class="text-end">{{ number_format($loan->principal,2) }}</td>
                    <td class="text-end">{{ number_format($loan->loanOutstanding,2) }}</td>
                    <td>{{ $loan->lastRepaymentDate }}</td>
                    <td class="text-center">{{ $loan->dueTerms ?? '-' }}</td>
                    <td class="text-danger fw-bold text-end">{{ number_format($loan->totalArrears,2) }}</td>
                    <td class="text-center">{{ $loan->interestDays }}</td>
                    <td class="text-warning fw-bold text-end">{{ number_format($loan->interestArrears,2) }}</td>
                    <td class="text-primary fw-bold text-end">{{ number_format($loan->totalArrears + $loan->interestArrears,2) }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="17" class="text-center text-muted">No loans in arrears</td>
                </tr>
            @endforelse
        </tbody>
        <tfoot class="table-light fw-bold text-end sticky-bottom bg-light" style="z-index:1;">
            <tr>
                <td colspan="11">Totals</td>
                <td>{{ number_format($getLoanData->sum('principal'),2) }}</td>
                <td>{{ number_format($getLoanData->sum('loanOutstanding'),2) }}</td>
                <td></td>
                <td></td>
                <td class="text-danger">{{ number_format($getLoanData->sum('totalArrears'),2) }}</td>
                <td></td>
                <td class="text-warning">{{ number_format($getLoanData->sum('interestArrears'),2) }}</td>
                <td class="text-primary fw-bold text-end">
                    {{ number_format($getLoanData->sum(fn($loan) => $loan->totalArrears + $loan->interestArrears),2) }}
                </td>
            </tr>
        </tfoot>
    </table>
</div>

                {{-- Summary Cards --}}
                <div class="row mt-4 g-2">
                    <div class="col-6 col-md-2">
                        <div class="card shadow p-3">
                            <label class="fw-bold">Total Members</label>
                            <h5 class="mb-0 total-member-count">{{ $totalMembers }}</h5>
                        </div>
                    </div>
                    <div class="col-6 col-md-2">
                        <div class="card shadow p-3">
                            <label class="fw-bold">Total Divisions</label>
                            <h5 class="mb-0 total-division-count">{{ $totalDivisions }}</h5>
                        </div>
                    </div>
                    <div class="col-6 col-md-2">
                        <div class="card shadow p-3">
                            <label class="fw-bold">Total Villages</label>
                            <h5 class="mb-0 total-village-count">{{ $totalVillages }}</h5>
                        </div>
                    </div>
                    <div class="col-6 col-md-2">
                        <div class="card shadow p-3">
                            <label class="fw-bold">Total Small Groups</label>
                            <h5 class="mb-0 total-smallgroup-count">{{ $totalSmallGroups }}</h5>
                        </div>
                    </div>
                    <div class="col-6 col-md-2">
                        <div class="card shadow p-3">
                            <label class="fw-bold">Total Loan Amount</label>
                            <h5 class="mb-0 total-loan-amount">{{ number_format($totalLoanAmount,2) }}</h5>
                        </div>
                    </div>
                    <div class="col-6 col-md-2">
                        <div class="card shadow p-3">
                            <label class="fw-bold">Total Loan Outstanding</label>
                            <h5 class="mb-0 total-loan-outstanding">{{ number_format($getLoanData->sum('loanOutstanding'),2) }}</h5>
                        </div>
                    </div>
                    <div class="col-6 col-md-2 mt-2">
                        <div class="card shadow p-3 bg-danger text-white">
                            <label class="fw-bold">Total Principal Arrears</label>
                            <h5 class="mb-0 total-principal-arrears">{{ number_format($totalTotalArrears,2) }}</h5>
                        </div>
                    </div>
                    <div class="col-6 col-md-2 mt-2">
                        <div class="card shadow p-3 bg-warning text-white">
                            <label class="fw-bold">Total Interest Arrears</label>
                            <h5 class="mb-0 total-interest-arrears">{{ number_format($totalInterestArrears,2) }}</h5>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
