<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Loan Agreement {{ $loan->loan_number }}</title>
<style>
body{font-family:Arial,sans-serif;color:#111827;margin:0;background:#f3f4f6;font-size:13px}.actions{max-width:900px;margin:16px auto;display:flex;gap:8px}.btn{padding:8px 12px;border:1px solid #d1d5db;background:#fff;color:#111827;text-decoration:none;cursor:pointer}.sheet{max-width:900px;margin:0 auto 24px;background:#fff;padding:30px;border:1px solid #d1d5db}.head{display:flex;justify-content:space-between;gap:24px;border-bottom:2px solid #111827;padding-bottom:16px}.brand{display:flex;gap:14px;align-items:flex-start}.logo{max-width:90px;max-height:70px;object-fit:contain}.name{font-size:21px;font-weight:700}.muted{font-size:11px;color:#6b7280;line-height:1.5}.doc{text-align:right}.doc h1{font-size:18px;margin:0 0 6px}.section{margin-top:20px}.section h2{font-size:12px;letter-spacing:.06em;text-transform:uppercase;border-bottom:1px solid #d1d5db;padding-bottom:6px}.grid{display:grid;grid-template-columns:1fr 1fr;gap:10px 24px}.label{font-size:10px;color:#6b7280;text-transform:uppercase}.value{font-weight:600;margin-top:2px}.table{width:100%;border-collapse:collapse;font-size:11px}.table th,.table td{border-bottom:1px solid #e5e7eb;padding:7px;text-align:left}.table th{text-transform:uppercase;font-size:9px;color:#6b7280}.money{text-align:right!important}.signatures{display:grid;grid-template-columns:1fr 1fr;gap:40px 60px;margin-top:50px}.sig{border-top:1px solid #111827;padding-top:6px;font-size:11px}.stamp{max-width:110px;max-height:80px;object-fit:contain;margin-top:10px}@media print{body{background:#fff}.actions{display:none}.sheet{border:0;margin:0;max-width:none;padding:12mm}}
</style>
</head>
<body>
@php($business=$loan->branch->business)
<div class="actions"><button class="btn" onclick="window.print()">Print Agreement</button><a class="btn" href="{{ route('loans.show',$loan) }}">Back to Loan</a></div>
<main class="sheet">
<div class="head"><div class="brand">@if($business->logo_path)<img class="logo" src="{{ route('document-branding.asset',[$business,'logo']) }}" alt="Company logo">@endif<div><div class="name">{{ $business->name }}</div><div class="muted">{{ collect([$business->address,$business->district,$business->region,$business->country])->filter()->join(', ') }}<br>{{ collect([$business->phone,$business->email,$business->website])->filter()->join(' · ') }}<br>Reg: {{ $business->registration_number ?: 'N/A' }} · TIN: {{ $business->tin ?: 'N/A' }}</div></div></div><div class="doc"><h1>LOAN AGREEMENT</h1><strong>{{ $loan->loan_number }}</strong><div class="muted">{{ $loan->branch->name }}<br>Generated {{ $loan->contract_generated_at?->format('d M Y H:i') }}</div></div></div>

<section class="section"><h2>Borrower</h2><div class="grid"><div><div class="label">Customer</div><div class="value">{{ $loan->customer->full_name }}</div><div class="muted">{{ $loan->customer->customer_number }}</div></div><div><div class="label">Phone / NIN</div><div class="value">{{ $loan->customer->phone }} · {{ $loan->customer->nin ?: 'NIN not provided' }}</div></div></div></section>

<section class="section"><h2>Loan Terms</h2><div class="grid"><div><div class="label">Product</div><div class="value">{{ $loan->product->name }}</div></div><div><div class="label">Purpose</div><div class="value">{{ $loan->purpose }}</div></div><div><div class="label">Principal</div><div class="value">{{ $business->currency_symbol ?: $business->currency }} {{ number_format((float)$loan->principal,2) }}</div></div><div><div class="label">Interest</div><div class="value">{{ $business->currency_symbol ?: $business->currency }} {{ number_format((float)$loan->interest,2) }}</div></div><div><div class="label">Fees</div><div class="value">{{ $business->currency_symbol ?: $business->currency }} {{ number_format((float)$loan->fees,2) }}</div></div><div><div class="label">Total Repayment</div><div class="value">{{ $business->currency_symbol ?: $business->currency }} {{ number_format((float)$loan->total_repayment,2) }}</div></div><div><div class="label">Interest Method</div><div class="value">{{ str($loan->product->interest_calculation_method)->replace('_',' ')->headline() }}</div></div><div><div class="label">Repayment Frequency</div><div class="value">{{ str($loan->product->repayment_frequency)->replace('_',' ')->headline() }}</div></div><div><div class="label">Late Payment Rule</div><div class="value">{{ str($loan->late_payment_penalty_type)->replace('_',' ')->headline() }} {{ $loan->late_payment_penalty_value }}</div></div></div></section>

<section class="section"><h2>Repayment Schedule</h2><table class="table"><thead><tr><th>#</th><th>Due Date</th><th class="money">Principal</th><th class="money">Interest</th><th class="money">Fees</th><th class="money">Amount Due</th></tr></thead><tbody>@foreach($loan->installments as $item)<tr><td>{{ $item->installment_number }}</td><td>{{ $item->due_date->format('d M Y') }}</td><td class="money">{{ number_format((float)$item->principal,2) }}</td><td class="money">{{ number_format((float)$item->interest,2) }}</td><td class="money">{{ number_format((float)$item->fees,2) }}</td><td class="money">{{ number_format((float)$item->amount_due,2) }}</td></tr>@endforeach</tbody></table></section>

<section class="section"><h2>Collateral & Guarantor</h2><div class="grid"><div><div class="label">Collateral</div>@forelse($loan->collaterals as $item)<div class="value">{{ $item->code }} · {{ $item->item }} · {{ $business->currency_symbol ?: $business->currency }} {{ number_format((float)($item->accepted_value ?? 0),2) }}</div>@empty<div class="value">No collateral linked.</div>@endforelse</div><div><div class="label">Guarantor</div><div class="value">{{ $loan->guarantor ? $loan->guarantor->full_name.' · '.$loan->guarantor->phone.' · NIN '.($loan->guarantor->nin ?: 'N/A') : 'No guarantor linked.' }}</div></div></div></section>

<section class="section"><h2>Borrower Obligations</h2><ol><li>The borrower agrees to repay the loan according to the schedule above.</li><li>Payments must be made through channels authorized by the lender and properly receipted.</li><li>Late payments may attract the penalty stated in this agreement.</li><li>The borrower must keep contact and address information current during the loan term.</li><li>Pledged collateral must not be sold, transferred or encumbered without written approval.</li><li>Collateral release is subject to full settlement and the lender custody-release process.</li></ol><p>The borrower confirms that the loan terms, charges, repayment schedule, collateral obligations and late-payment rules have been explained and accepted.</p></section>

<div class="signatures"><div class="sig">Borrower: {{ $loan->customer->full_name }} / Signature / Date</div><div class="sig">Guarantor Signature / Date</div><div class="sig">Authorized Lender Representative / Date</div><div>@if($business->stamp_path)<img class="stamp" src="{{ route('document-branding.asset',[$business,'stamp']) }}" alt="Company stamp">@else<div class="sig">Company Stamp</div>@endif</div></div>
</main>
</body></html>
