@extends('layouts.app')
@section('title','Loan Adjustments')
@section('content')
<x-ui.page-toolbar title="Loan Adjustments" description="Early settlement and controlled repayment-schedule restructuring with permanent history." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Adjustments']]" />

<form method="GET" class="mf-card mf-card-body mb-4">
    <div class="mf-form-inline">
        <input class="mf-input" name="search" value="{{ request('search') }}" placeholder="Loan, settlement, restructuring, customer or phone">
        <button class="mf-btn mf-btn-primary">Search</button>
    </div>
</form>

<div class="mf-card mb-4">
    <div class="border-b p-4"><h2 class="font-semibold">Active loans</h2><p class="mt-1 text-xs text-muted-foreground">Only active loans can be settled early or restructured.</p></div>
    <div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>Loan</th><th>Customer</th><th>Branch</th><th>Principal</th><th>Interest</th><th>Fees</th><th>Penalties</th><th>Total</th><th></th></tr></thead><tbody>
    @forelse($active as $loan)
        <tr>
            <td><a class="font-medium text-primary hover:underline" href="{{ route('loans.show',$loan) }}">{{ $loan->loan_number }}</a><div class="text-xs text-muted-foreground">{{ $loan->product->name }}</div></td>
            <td>{{ $loan->customer->full_name }}<div class="text-xs text-muted-foreground">{{ $loan->customer->phone }}</div></td>
            <td>{{ $loan->branch->name }}</td>
            <td>{{ number_format((float)$loan->principal_outstanding,2) }}</td>
            <td>{{ number_format((float)$loan->interest_outstanding,2) }}</td>
            <td>{{ number_format((float)$loan->fees_outstanding,2) }}</td>
            <td>{{ number_format((float)$loan->penalties_outstanding,2) }}</td>
            <td class="font-semibold">{{ number_format((float)$loan->total_outstanding,2) }}</td>
            <td class="text-right whitespace-nowrap">
                @if(auth()->user()->can('loans.settlement.execute'))<a class="mf-btn" href="{{ route('loan-early-settlements.create',$loan) }}">Settle</a>@endif
                @if(auth()->user()->can('loans.restructure.create'))<a class="mf-btn mf-btn-primary" href="{{ route('loan-restructurings.create',$loan) }}">Restructure</a>@endif
            </td>
        </tr>
    @empty<tr><td colspan="9" class="text-center text-muted-foreground">No active loans match this search.</td></tr>@endforelse
    </tbody></table></div>
</div>
<div class="mb-4">{{ $active->links() }}</div>

<div class="mf-card mb-4">
    <div class="border-b p-4"><h2 class="font-semibold">Restructuring requests</h2><p class="mt-1 text-xs text-muted-foreground">Previous and proposed schedules are preserved permanently.</p></div>
    <div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>Reference</th><th>Loan</th><th>Customer</th><th>Version</th><th>Status</th><th>Requested</th><th>Approved / Rejected by</th><th></th></tr></thead><tbody>
    @forelse($restructurings as $item)
        <tr>
            <td class="font-medium">{{ $item->restructuring_number }}</td>
            <td>{{ $item->loan->loan_number }}</td>
            <td>{{ $item->loan->customer->full_name }}</td>
            <td>v{{ $item->previous_schedule_version }} → v{{ $item->new_schedule_version }}</td>
            <td><span class="mf-badge {{ $item->status==='approved'?'mf-badge-success':($item->status==='rejected'?'mf-badge-danger':'mf-badge-warning') }}">{{ ucfirst($item->status) }}</span></td>
            <td>{{ $item->requested_at?->format('d M Y H:i') }}<div class="text-xs text-muted-foreground">{{ $item->requester?->name }}</div></td>
            <td>{{ $item->approver?->name ?? $item->rejecter?->name ?? '—' }}</td>
            <td class="text-right"><a class="mf-btn" href="{{ route('loan-restructurings.show',$item) }}">Open</a></td>
        </tr>
    @empty<tr><td colspan="8" class="text-center text-muted-foreground">No restructuring requests yet.</td></tr>@endforelse
    </tbody></table></div>
</div>
<div class="mb-4">{{ $restructurings->links() }}</div>

<div class="mf-card">
    <div class="border-b p-4"><h2 class="font-semibold">Completed early settlements</h2><p class="mt-1 text-xs text-muted-foreground">Permanent settlement and financial posting history.</p></div>
    <div class="mf-table-frame border-0"><table class="mf-table" data-standard-table><thead><tr><th>Settlement</th><th>Loan</th><th>Customer</th><th>Final amount</th><th>Adjustment</th><th>Account</th><th>Date</th><th></th></tr></thead><tbody>
    @forelse($settlements as $item)
        <tr>
            <td class="font-medium">{{ $item->settlement_number }}</td>
            <td>{{ $item->loan->loan_number }}</td>
            <td>{{ $item->loan->customer->full_name }}</td>
            <td class="font-semibold">{{ number_format((float)$item->final_amount,2) }}</td>
            <td>{{ number_format((float)$item->settlement_adjustment,2) }}</td>
            <td>{{ $item->financialAccount->name }}</td>
            <td>{{ $item->settlement_date->format('d M Y') }}</td>
            <td class="text-right"><a class="mf-btn" href="{{ route('loan-early-settlements.show',$item) }}">Open</a></td>
        </tr>
    @empty<tr><td colspan="8" class="text-center text-muted-foreground">No early settlements yet.</td></tr>@endforelse
    </tbody></table></div>
</div>
<div class="mt-4">{{ $settlements->links() }}</div>
@endsection
