@extends('layouts.admin')

@section('title', 'Loans')

@section('content')
<div class="fundi-page-header">
    <div>
        <h1>Loans</h1>
        <p class="fundi-page-header__desc">Loan applications, approvals, and portfolio.</p>
    </div>
    <a href="{{ route('admin.loans.create') }}" class="fundi-btn fundi-btn--primary">New Application</a>
</div>

<form method="GET" class="fundi-filters">
    <div class="fundi-form-group">
        <label for="status">Status</label>
        <select id="status" name="status" class="fundi-select">
            <option value="">All statuses</option>
            @foreach(['submitted','underwriting','approved','rejected','disbursed','active','in_arrears','closed'] as $s)
                <option value="{{ $s }}" @selected(request('status') === $s)>{{ ucwords(str_replace('_',' ',$s)) }}</option>
            @endforeach
        </select>
    </div>
    <div class="fundi-form-group">
        <label for="product_id">Product</label>
        <select id="product_id" name="product_id" class="fundi-select">
            <option value="">All products</option>
            @foreach($products as $product)
                <option value="{{ $product->id }}" @selected(request('product_id') == $product->id)>{{ $product->name }}</option>
            @endforeach
        </select>
    </div>
    <button type="submit" class="fundi-btn fundi-btn--secondary">Filter</button>
</form>

<div class="fundi-card">
    <div class="fundi-card__body">
        <div class="fundi-table-wrap">
            <table class="fundi-table">
                <thead>
                    <tr>
                        <th>Loan No.</th>
                        <th>Customer</th>
                        <th>Product</th>
                        <th class="num">Principal</th>
                        <th class="num">Outstanding</th>
                        <th>Duration</th>
                        <th>Status</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($loans as $loan)
                        @php
                            $progress = $loan->durationProgressPercent();
                            $daysLeft = $loan->daysRemaining();
                        @endphp
                        <tr>
                            <td>{{ $loan->loan_no }}</td>
                            <td>{{ $loan->customer->display_name ?? '—' }}</td>
                            <td>{{ $loan->loanProduct->name ?? '—' }}</td>
                            <td class="num">{{ money($loan->principal) }}</td>
                            <td class="num">{{ money($loan->outstanding_principal) }}</td>
                            <td>
                                <div class="vfh-mini-progress" title="{{ number_format($progress, 0) }}% of tenor">
                                    <span style="width:{{ min(100, $progress) }}%"></span>
                                </div>
                                <span class="text-muted" style="font-size:0.75rem;">
                                    {{ $loan->tenor_months }} mo
                                    @if($daysLeft !== null)
                                        · {{ $daysLeft < 0 ? abs($daysLeft).'d overdue' : $daysLeft.'d left' }}
                                    @endif
                                </span>
                            </td>
                            <td><x-status-badge :status="$loan->status" /></td>
                            <td class="actions"><a href="{{ route('admin.loans.show', $loan) }}" class="fundi-btn fundi-btn--link fundi-btn--sm">View</a></td>
                        </tr>
                    @empty
                        <tr><td colspan="8" class="fundi-empty">No loans found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($loans->hasPages())
            <div class="fundi-pagination">{{ $loans->links() }}</div>
        @endif
    </div>
</div>
@endsection
