@extends('layouts.app')
@section('title','Contract Templates')
@section('content')
<x-ui.page-toolbar title="Contract Templates" description="Upload DOCX or PDF loan contracts, map system fields into the original document, and choose the contract used for new loan agreements." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Administration'],['label'=>'Contract Templates']]">
<x-slot:actions><a class="mf-btn mf-btn-primary" href="{{ route('admin.contract-templates.create') }}">Upload Contract</a></x-slot:actions>
</x-ui.page-toolbar>

<div class="mf-card">
<div class="border-b p-4"><h2 class="font-semibold">Loan contract templates</h2><p class="mt-1 text-xs text-muted-foreground">DOCX templates preserve the original Word structure while filling mapped values. PDF templates preserve the original PDF pages and place generated values as positioned overlays. Unused non-default templates can be deleted; templates already used by loans are kept for permanent contract history.</p></div>
<div class="mf-table-frame border-0"><table class="mf-table"><thead><tr><th>Template</th><th>File</th><th>Format</th><th>Mappings</th><th>Status</th><th>Default</th><th class="text-right">Actions</th></tr></thead><tbody>
@forelse($templates as $template)
<tr>
<td><div class="font-medium">{{ $template->name }}</div><div class="text-xs text-muted-foreground">Version {{ $template->version }} · uploaded {{ $template->created_at->format('d M Y H:i') }}</div></td>
<td>{{ $template->original_name }}</td>
<td><span class="mf-badge mf-badge-muted">{{ strtoupper($template->isPdf()?'pdf':'docx') }}</span></td>
<td>{{ count($template->mappings ?? []) }} system fields</td>
<td><span class="mf-badge {{ $template->status==='active'?'mf-badge-success':'mf-badge-muted' }}">{{ ucfirst($template->status) }}</span></td>
<td>{{ $template->is_default?'Yes':'—' }}</td>
<td class="text-right"><div class="inline-flex flex-wrap justify-end gap-2">
<a class="mf-btn" href="{{ route('admin.contract-templates.download',$template) }}">Original</a>
<a class="mf-btn" href="{{ route('admin.contract-templates.edit',$template) }}">{{ $template->isPdf()?'Edit positions':'Review mapping' }}</a>
@if(!$template->is_default)<form method="POST" action="{{ route('admin.contract-templates.default',$template) }}">@csrf<button class="mf-btn">Use as default</button></form>@endif
@if($template->is_default)
<span class="mf-btn cursor-not-allowed opacity-60" title="Set another template as default before deleting this one.">Default protected</span>
@elseif($template->loans()->exists())
<span class="mf-btn cursor-not-allowed opacity-60" title="This template has already been used by one or more loans.">In use</span>
@else
<form method="POST" action="{{ route('admin.contract-templates.destroy',$template) }}" onsubmit="return confirm('Delete this contract template and its uploaded source file? This action cannot be undone.');">@csrf @method('DELETE')<button class="mf-btn text-destructive">Delete</button></form>
@endif
</div></td>
</tr>
@empty<tr><td colspan="7" class="text-center text-muted-foreground">No contract template has been uploaded yet.</td></tr>@endforelse
</tbody></table></div>
</div>
@endsection
