<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ $quotation->quotation_number }}</title>
    <style>
        @page { margin: 10mm 11mm; size: A4 portrait; }
        * { box-sizing: border-box; }
        html, body { margin: 0; padding: 0; width: 100%; max-width: 100%; }
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 11px;
            line-height: 1.4;
            color: #111827;
        }
        table { width: 100%; border-collapse: collapse; table-layout: auto; }
        td, th { padding: 6px 8px; vertical-align: top; word-wrap: break-word; }
        thead tr { page-break-inside: avoid; }
        .right { text-align: right; }
        .muted { color: #475569; font-size: 10px; }
        .banner { background: #340080; color: #fff; }
        .head-p { background: #340080; color: #fff; font-size: 10px; font-weight: bold; text-transform: uppercase; letter-spacing: 0.03em; }
        .center { text-align: center; }
        .pdf-logo { max-height: 58px; max-width: 220px; width: auto; height: auto; display: block; }
        strong { font-weight: bold; }
    </style>
</head>
<body>
@php
    $company = \App\Models\CompanySetting::getSettings();
    $cityLine = trim(implode(', ', array_filter([$company->company_city ?? null, $company->company_state ?? null, $company->company_zip ?? null])));
@endphp

<table class="banner" style="margin-bottom:14px;">
    <tr>
        <td style="padding:18px 20px;" width="62%" valign="middle">
            @if(!empty($pdfLogoUri ?? null))
                <img src="{{ $pdfLogoUri }}" alt="" class="pdf-logo" width="220" height="58" style="margin-bottom:8px;">
            @endif
            <div style="font-size:17px;font-weight:bold;">{{ $company->company_name ?? config('app.name') }}</div>
            <div style="font-size:11px;opacity:0.92;line-height:1.55;margin-top:6px;">
                @if($company->company_address){{ $company->company_address }}<br>@endif
                @if($cityLine !== ''){{ $cityLine }}<br>@endif
                @if(!empty($company->company_country)){{ $company->company_country }}<br>@endif
                @if($company->company_phone){{ $company->company_phone }}<br>@endif
                @if($company->company_email){{ $company->company_email }}<br>@endif
                @if($company->company_tax_id)Tax ID: {{ $company->company_tax_id }}@endif
            </div>
        </td>
        <td style="padding:18px 20px;" width="38%" valign="middle" class="right">
            <table style="width:100%;background:rgba(255,255,255,0.15);">
                <tr><td class="center" style="padding:12px 14px;color:#fff;">
                    <div style="font-size:10px;font-weight:bold;text-transform:uppercase;letter-spacing:2px;opacity:0.95;">Quotation</div>
                    <div style="font-size:18px;font-weight:bold;margin-top:6px;">{{ $quotation->quotation_number }}</div>
                    <div style="font-size:10px;margin-top:8px;opacity:0.9;">{{ $quotation->issue_date->format('M d, Y') }} – {{ $quotation->expiry_date ? $quotation->expiry_date->format('M d, Y') : '—' }}</div>
                </td></tr>
            </table>
        </td>
    </tr>
</table>

@if($quotation->customer)
<table style="background:#f1f5f9;border-left:4px solid #1e293b;margin-bottom:14px;">
    <tr><td style="padding:14px 16px;">
        <div style="font-size:10px;font-weight:bold;text-transform:uppercase;color:#475569;">Quoted To</div>
        <div style="font-size:16px;font-weight:bold;color:#0f172a;margin-top:6px;">{{ $quotation->customer->name }}</div>
        <div class="muted" style="font-size:11px;line-height:1.55;">
            @if($quotation->customer->email){{ $quotation->customer->email }}<br>@endif
            @if($quotation->customer->phone){{ $quotation->customer->phone }}<br>@endif
            @if($quotation->customer->address){{ $quotation->customer->address }}@endif
        </div>
    </td></tr>
</table>
@endif

<table style="margin-bottom:14px;">
    <thead>
        <tr>
            <th class="head-p" align="left">Item</th>
            <th class="head-p right">Qty</th>
            <th class="head-p right">Unit</th>
            <th class="head-p right">Tax</th>
            <th class="head-p right">Total</th>
        </tr>
    </thead>
    <tbody>
        @forelse($quotation->items as $item)
        @php $line = ($item->quantity * $item->unit_price) * (1 + (($item->tax_rate ?? 0) / 100)); @endphp
        <tr>
            <td style="border-bottom:1px solid #e2e8f0;">
                <strong style="font-size:12px;">{{ $item->description }}</strong>
                @if($item->notes)
                    <br><span class="muted" style="font-size:10px;">{{ $item->notes }}</span>
                @endif
            </td>
            <td class="right" style="border-bottom:1px solid #e2e8f0;">{{ number_format($item->quantity, 2) }}</td>
            <td class="right" style="border-bottom:1px solid #e2e8f0;">{{ $quotation->currency }} {{ number_format($item->unit_price, 2) }}</td>
            <td class="right" style="border-bottom:1px solid #e2e8f0;">{{ $item->tax_rate ?? 0 }}%</td>
            <td class="right" style="border-bottom:1px solid #e2e8f0;font-weight:bold;font-size:12px;">{{ $quotation->currency }} {{ number_format($line, 2) }}</td>
        </tr>
        @empty
        <tr><td colspan="5" class="muted center" style="padding:14px;text-align:center;">No items</td></tr>
        @endforelse
    </tbody>
</table>

<table style="background:#340080;color:#fff;margin-bottom:14px;">
    <tr><td colspan="2" style="padding:14px 16px;">
        <table style="width:100%;color:#fff;">
            <tr><td style="opacity:0.95;font-weight:bold;font-size:12px;">Subtotal</td><td class="right" style="font-weight:bold;font-size:12px;">{{ $quotation->currency }} {{ number_format($quotation->subtotal, 2) }}</td></tr>
            @if(($quotation->tax_amount ?? 0) > 0)
            <tr><td style="opacity:0.95;padding-top:6px;font-weight:bold;">Tax</td><td class="right" style="font-weight:bold;">{{ $quotation->currency }} {{ number_format($quotation->tax_amount, 2) }}</td></tr>
            @endif
            @if(($quotation->discount_amount ?? 0) > 0)
            <tr><td style="opacity:0.95;padding-top:6px;font-weight:bold;">Discount</td><td class="right" style="font-weight:bold;">-{{ $quotation->currency }} {{ number_format($quotation->discount_amount, 2) }}</td></tr>
            @endif
            <tr><td style="font-size:17px;font-weight:bold;padding-top:12px;border-top:2px solid rgba(255,255,255,0.35);">Total</td><td class="right" style="font-size:17px;font-weight:bold;padding-top:12px;border-top:2px solid rgba(255,255,255,0.35);">{{ $quotation->currency }} {{ number_format($quotation->total, 2) }}</td></tr>
        </table>
    </td></tr>
</table>

<table style="margin-bottom:12px;">
    <tr><td style="font-size:12px;font-weight:bold;padding-bottom:8px;">Acceptance</td></tr>
    <tr>
        <td width="50%" style="padding-right:10px;"><div style="border:1px solid #cbd5e1;min-height:52px;"></div><div class="muted" style="font-size:10px;margin-top:4px;">Client Signature</div></td>
        <td width="50%" style="padding-left:10px;"><div style="border:1px solid #cbd5e1;min-height:52px;"></div><div class="muted" style="font-size:10px;margin-top:4px;">Date</div></td>
    </tr>
</table>

@if($quotation->notes)
<table style="background:#f1f5f9;margin-bottom:12px;border:1px solid #e2e8f0;"><tr><td style="padding:12px 14px;font-size:12px;font-weight:bold;">Notes</td></tr>
<tr><td class="muted" style="padding:0 14px 12px;font-size:11px;line-height:1.5;">{{ $quotation->notes }}</td></tr></table>
@endif

<table style="background:#f1f5f9;border:1px solid #e2e8f0;"><tr><td style="padding:12px 14px;font-size:12px;font-weight:bold;">Terms &amp; Conditions</td></tr>
<tr><td class="muted" style="padding:0 14px 12px;font-size:11px;line-height:1.55;">{{ $quotation->terms ?? 'This quotation is valid for 30 days from the date of issue.' }}</td></tr></table>
</body>
</html>
