@extends('layouts.portal')

@section('title', 'Loan '.$loan->loan_no)

@section('content')
<div class="fundi-page-header">
    <div>
        <h1>{{ $loan->loan_no }}</h1>
        <p class="fundi-page-header__desc">{{ $loan->loanProduct->name ?? 'Loan' }}</p>
    </div>
    <a href="{{ route('portal.loans.index') }}" class="fundi-btn fundi-btn--secondary">Back to loans</a>
</div>

<div class="fundi-detail-grid">
    <div class="fundi-detail-item"><label>Status</label><span><x-status-badge :status="$loan->status" /></span></div>
    <div class="fundi-detail-item"><label>Principal</label><span>{{ money($loan->principal) }}</span></div>
    <div class="fundi-detail-item"><label>Outstanding</label><span>{{ money($loan->outstanding_principal) }}</span></div>
    <div class="fundi-detail-item"><label>Interest Rate</label><span>{{ $loan->interest_rate }}%</span></div>
    <div class="fundi-detail-item"><label>Tenor</label><span>{{ $loan->tenor_months }} months</span></div>
    <div class="fundi-detail-item"><label>Next Due</label><span>{{ $loan->next_due_date?->format('d M Y') ?? '—' }}</span></div>
    <div class="fundi-detail-item"><label>Disbursement</label><span>{{ $loan->disbursement_date?->format('d M Y') ?? '—' }}</span></div>
    <div class="fundi-detail-item"><label>Maturity</label><span>{{ $loan->maturity_date?->format('d M Y') ?? '—' }}</span></div>
</div>


<div class="fundi-card" style="margin-bottom:1.5rem;">
    <div class="fundi-card__header"><h3>Your loan timeline &amp; interest</h3></div>
    <div class="fundi-card__body fundi-card__body--padded">
        <x-loan-timeline :loan="$loan" />
    </div>
</div>

@if($loan->schedules->isNotEmpty())
<div class="fundi-card">
    <div class="fundi-card__header"><h3>Repayment Schedule</h3></div>
    <div class="fundi-card__body">
        <div class="fundi-table-wrap">
            <table class="fundi-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Due Date</th>
                        <th class="num">Principal</th>
                        <th class="num">Interest</th>
                        <th class="num">Total Due</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($loan->schedules as $schedule)
                        <tr>
                            <td>{{ $schedule->installment_no }}</td>
                            <td>{{ $schedule->due_date?->format('d M Y') ?? '—' }}</td>
                            <td class="num">{{ money($schedule->principal_due) }}</td>
                            <td class="num">{{ money($schedule->interest_due) }}</td>
                            <td class="num">{{ money($schedule->total_due) }}</td>
                            <td><x-status-badge :status="$schedule->status ?? 'pending'" /></td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
@endif

@if($loan->repayments->isNotEmpty())
<div class="fundi-card">
    <div class="fundi-card__header"><h3>Repayment History</h3></div>
    <div class="fundi-card__body">
        <div class="fundi-table-wrap">
            <table class="fundi-table">
                <thead><tr><th>Date</th><th class="num">Amount</th><th>Reference</th></tr></thead>
                <tbody>
                    @foreach($loan->repayments as $rep)
                        <tr>
                            <td>{{ $rep->payment_date?->format('d M Y') ?? $rep->created_at->format('d M Y') }}</td>
                            <td class="num">{{ money($rep->amount) }}</td>
                            <td>{{ $rep->reference ?? '—' }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
@endif
@endsection
