@extends('layouts.app')
@section('title','Upload Contract Template')
@section('content')
<x-ui.form-shell title="Upload Contract Template" description="Upload the microfinance company's existing Word or PDF contract. DOCX templates are text-mapped; PDF templates keep the original pages and use positioned system-field overlays." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Contract Templates','url'=>route('admin.contract-templates.index')],['label'=>'Upload']]">
<form method="POST" action="{{ route('admin.contract-templates.store') }}" enctype="multipart/form-data" class="mf-form">@csrf
<x-ui.form-section title="Source contract" description="Use DOCX when you want automatic label detection and spacing control. Use PDF when the contract is already finalized visually and you want to place generated values directly on its pages.">
<x-ui.form-grid>
<div class="mf-col-5"><label class="mf-field-label">Template name *</label><input class="mf-input" name="name" value="{{ old('name') }}" placeholder="e.g. Standard Loan Contract" required>@error('name')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
<div class="mf-col-7"><label class="mf-field-label">DOCX or PDF contract *</label><input type="file" class="mf-input" name="template_file" accept=".docx,.pdf,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/pdf" required>@error('template_file')<p class="mf-field-error">{{ $message }}</p>@enderror<p class="mf-field-help">Maximum 15 MB. The source file is stored privately.</p></div>
<div class="mf-col-12 grid gap-2 md:grid-cols-2"><div class="rounded-md border p-3 text-xs"><strong>DOCX</strong><p class="mt-1 text-muted-foreground">The system extracts Word paragraphs/table cells, detects known labels, and lets you correct spaces before/after generated values.</p></div><div class="rounded-md border p-3 text-xs"><strong>PDF</strong><p class="mt-1 text-muted-foreground">The PDF remains the exact visual background. You choose system fields and position each value by page, X/Y coordinates, width, font size and alignment.</p></div></div>
<div class="mf-col-12"><label class="mf-option"><input type="checkbox" name="make_default" value="1" @checked(old('make_default'))><span><strong>Make this the default contract immediately</strong><small class="block text-muted-foreground">You can leave it as draft, review mappings/positions first, then activate it later.</small></span></label></div>
</x-ui.form-grid>
</x-ui.form-section>

<x-ui.form-section title="Required signatures" description="These do not get auto-filled as signatures. They are shown as completion requirements before the signed copy is uploaded.">
<div class="grid gap-2 sm:grid-cols-2 lg:grid-cols-3">@foreach($signatories as $value=>$label)<label class="mf-option"><input type="checkbox" name="required_signatories[]" value="{{ $value }}" @checked(in_array($value,old('required_signatories',['borrower','guarantor','loan_officer','office_stamp']),true))><span>{{ $label }}</span></label>@endforeach</div>
</x-ui.form-section>

<div class="rounded-md border p-4 text-sm"><strong>Supported system fields</strong><p class="mt-1 text-muted-foreground">Both formats can use the same {{ $fieldCount }} customer, loan, branch, guarantor, staff and collateral values. PDF fields are positioned manually because the original PDF page is treated as a fixed visual template.</p></div>
<x-ui.form-actions><a class="mf-btn" href="{{ route('admin.contract-templates.index') }}">Cancel</a><button class="mf-btn mf-btn-primary">Upload Contract</button></x-ui.form-actions>
</form>
</x-ui.form-shell>
@endsection
