@extends('layouts.app')
@section('title',$restructuring->restructuring_number)
@section('content')
<x-ui.page-toolbar :title="$restructuring->restructuring_number" description="Permanent restructuring proposal, evidence, decision and schedule versions." :breadcrumbs="[['label'=>'Loan Adjustments','url'=>route('loan-adjustments.index')],['label'=>$restructuring->restructuring_number]]" />

<div class="grid gap-4 lg:grid-cols-3">
    <div class="mf-card mf-card-body lg:col-span-2">
        <div class="flex flex-wrap items-center justify-between gap-2"><h2 class="font-semibold">Request details</h2><span class="mf-badge {{ $restructuring->status==='approved'?'mf-badge-success':($restructuring->status==='rejected'?'mf-badge-danger':'mf-badge-warning') }}">{{ ucfirst($restructuring->status) }}</span></div>
        <div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
            <div><div class="text-xs text-muted-foreground">Loan</div><a class="font-medium text-primary hover:underline" href="{{ route('loans.show',$restructuring->loan) }}">{{ $restructuring->loan->loan_number }}</a></div>
            <div><div class="text-xs text-muted-foreground">Customer</div><div class="font-medium">{{ $restructuring->loan->customer->full_name }}</div></div>
            <div><div class="text-xs text-muted-foreground">Schedule version</div><div class="font-medium">v{{ $restructuring->previous_schedule_version }} → v{{ $restructuring->new_schedule_version }}</div></div>
            <div><div class="text-xs text-muted-foreground">Total outstanding snapshot</div><div class="font-semibold">{{ number_format((float)$restructuring->total_outstanding,2) }}</div></div>
            <div><div class="text-xs text-muted-foreground">Requested by</div><div>{{ $restructuring->requester?->name ?? '—' }}</div><div class="text-xs text-muted-foreground">{{ $restructuring->requested_at?->format('d M Y H:i') }}</div></div>
            <div><div class="text-xs text-muted-foreground">Decision</div><div>{{ $restructuring->approver?->name ?? $restructuring->rejecter?->name ?? 'Pending' }}</div><div class="text-xs text-muted-foreground">{{ $restructuring->approved_at?->format('d M Y H:i') ?? $restructuring->rejected_at?->format('d M Y H:i') ?? '' }}</div></div>
        </div>
        <div class="mt-4 rounded-md border p-3"><div class="text-xs text-muted-foreground">Reason</div><div class="mt-1 text-sm whitespace-pre-line">{{ $restructuring->reason }}</div></div>
        @if($restructuring->decision_comments)<div class="mt-3 rounded-md border p-3"><div class="text-xs text-muted-foreground">Decision comments</div><div class="mt-1 text-sm">{{ $restructuring->decision_comments }}</div></div>@endif
    </div>

    <div class="mf-card mf-card-body">
        <h2 class="font-semibold">Supporting documents</h2>
        <div class="mt-3 space-y-2">
            @forelse($restructuring->documents as $document)
                <a class="mf-btn w-full justify-between" href="{{ route('loan-restructurings.documents.download',[$restructuring,$document]) }}"><span class="truncate">{{ $document->original_name }}</span><span>Download</span></a>
            @empty<p class="text-sm text-muted-foreground">No supporting documents uploaded.</p>@endforelse
        </div>
    </div>
</div>

@if($restructuring->status==='pending' && auth()->user()->can('loans.restructure.approve'))
<div class="mf-card mf-card-body mt-4">
    <h2 class="font-semibold">Approval decision</h2>
    <p class="mt-1 text-xs text-muted-foreground">Approval will fail if the loan balance or active schedule changed after this request.</p>
    <form method="POST" action="{{ route('loan-restructurings.decision',$restructuring) }}" class="mt-3 grid gap-3 md:grid-cols-[1fr_auto_auto]">
        @csrf
        <input class="mf-input" name="comments" placeholder="Comments; required for rejection" maxlength="2000">
        <button class="mf-btn mf-btn-primary" name="decision" value="approve">Approve</button>
        <button class="mf-btn" name="decision" value="reject">Reject</button>
    </form>
    @error('comments')<p class="mf-error mt-2">{{ $message }}</p>@enderror
</div>
@endif

<div class="grid gap-4 mt-4 xl:grid-cols-2">
    <div class="mf-card">
        <div class="border-b p-4"><h2 class="font-semibold">Previous schedule — v{{ $restructuring->previous_schedule_version }}</h2><p class="mt-1 text-xs text-muted-foreground">Permanent snapshot before restructuring.</p></div>
        <div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>#</th><th>Due</th><th>Principal</th><th>Interest</th><th>Fees</th><th>Penalty</th><th>Remaining</th></tr></thead><tbody>
        @foreach($restructuring->previous_schedule as $row)<tr><td>{{ $row['installment_number'] }}</td><td>{{ $row['due_date'] }}</td><td>{{ $row['principal'] }}</td><td>{{ $row['interest'] }}</td><td>{{ $row['fees'] }}</td><td>{{ $row['penalty_due'] }}</td><td>{{ $row['remaining'] }}</td></tr>@endforeach
        </tbody></table></div>
    </div>
    <div class="mf-card">
        <div class="border-b p-4"><h2 class="font-semibold">Proposed schedule — v{{ $restructuring->new_schedule_version }}</h2><p class="mt-1 text-xs text-muted-foreground">{{ $restructuring->status==='approved'?'This version is active.':'This version is not active unless approved.' }}</p></div>
        <div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>#</th><th>Due</th><th>Principal</th><th>Interest</th><th>Fees</th><th>Penalty</th><th>Remaining</th></tr></thead><tbody>
        @foreach($restructuring->new_schedule as $row)<tr><td>{{ $row['installment_number'] }}</td><td>{{ $row['due_date'] }}</td><td>{{ $row['principal'] }}</td><td>{{ $row['interest'] }}</td><td>{{ $row['fees'] }}</td><td>{{ $row['penalty_due'] }}</td><td>{{ $row['remaining'] }}</td></tr>@endforeach
        </tbody></table></div>
    </div>
</div>
@endsection
