@extends('layouts.app')
@section('title','Review '.$document->original_name)
@section('content')
<x-ui.page-toolbar
    :title="'Review document'"
    :description="$document->original_name.' · '.$loan->loan_number.' · '.$loan->customer->full_name"
    :breadcrumbs="[
        ['label'=>'Dashboard','url'=>route('dashboard')],
        ['label'=>'Loan Applications','url'=>route('loans.index')],
        ['label'=>$loan->loan_number,'url'=>route('loans.show',$loan)],
        ['label'=>'Document review'],
    ]"
>
    <x-slot:actions>
        <a class="mf-btn" href="{{ route('loans.show',$loan) }}">Back to loan</a>
        <a class="mf-btn mf-btn-primary" href="{{ route('loans.documents.download',[$loan,$document]) }}">Download original</a>
    </x-slot:actions>
</x-ui.page-toolbar>

<div class="grid gap-4 lg:grid-cols-[minmax(0,1fr)_280px]">
    <div class="mf-card overflow-hidden">
        <div class="border-b p-4">
            <h2 class="font-semibold">Document contents</h2>
            <p class="mt-1 text-xs text-muted-foreground">Use this review before recording an approval, rejection, correction request or other loan decision.</p>
        </div>

        @if($previewAvailable)
            <div class="bg-muted/30 p-4 sm:p-6">
                <article class="mx-auto min-h-[70vh] max-w-4xl border bg-white px-6 py-8 text-sm leading-7 text-black shadow-sm sm:px-10">
                    @forelse($blocks as $block)
                        <p class="min-h-5 whitespace-pre-wrap">{{ $block['text'] ?? '' }}</p>
                    @empty
                        <p class="text-gray-500">No readable text was found in this Word document.</p>
                    @endforelse
                </article>
            </div>
        @else
            <div class="p-6">
                <div class="rounded-md border p-4 text-sm">
                    <div class="font-medium">Browser preview is not available for this file format.</div>
                    <p class="mt-2 text-muted-foreground">{{ $previewError ?: 'Download the original document to open it with a compatible application and complete your review.' }}</p>
                    <a class="mf-btn mt-4" href="{{ route('loans.documents.download',[$loan,$document]) }}">Download original</a>
                </div>
            </div>
        @endif
    </div>

    <aside class="space-y-4">
        <div class="mf-card">
            <div class="border-b p-4"><h2 class="font-semibold">Document information</h2></div>
            <div class="space-y-3 p-4 text-sm">
                <div><div class="text-xs text-muted-foreground">Type</div><div class="font-medium">{{ $document->kind==='signed_contract'?'Signed Contract':($document->document_type ?: 'Supporting Document') }}</div></div>
                <div><div class="text-xs text-muted-foreground">File</div><div class="break-all font-medium">{{ $document->original_name }}</div></div>
                <div><div class="text-xs text-muted-foreground">Uploaded</div><div>{{ $document->created_at->format('d M Y H:i') }}</div></div>
                <div><div class="text-xs text-muted-foreground">Uploaded by</div><div>{{ $document->uploader?->name ?? 'System' }}</div></div>
                @if($document->notes)<div><div class="text-xs text-muted-foreground">Notes</div><div>{{ $document->notes }}</div></div>@endif
            </div>
        </div>

        <div class="mf-card p-4 text-sm">
            <div class="font-medium">Review reminder</div>
            <p class="mt-2 text-xs leading-5 text-muted-foreground">Confirm the names, loan values, signatures, dates, collateral/guarantor details and any required supporting evidence before making a decision.</p>
        </div>
    </aside>
</div>
@endsection
