<!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; }
        .section { margin-bottom: 9px; }
        thead tr { page-break-inside: avoid; }
        .head-blue { background: #137fec; color: #fff; font-size: 10px; font-weight: bold; text-transform: uppercase; letter-spacing: 0.02em; }
        .right { text-align: right; }
        .muted { color: #4b5563; font-size: 10px; }
        .box { background: #f3f4f6; border: 1px solid #d1d5db; padding: 8px 10px; }
        .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(); @endphp

<table class="section">
    <tr>
        <td width="52%">
            @if(!empty($pdfLogoUri ?? null))
                <img src="{{ $pdfLogoUri }}" alt="" class="pdf-logo" width="220" height="58">
            @endif
            <div style="font-size:17px;font-weight:bold;margin-top:6px;">{{ $company->company_name ?? config('app.name') }}</div>
            <div class="muted" style="font-size:11px;line-height:1.55;">
                @if($company->company_address){{ $company->company_address }}<br>@endif
                @if($company->company_phone){{ $company->company_phone }}<br>@endif
                @if($company->company_email){{ $company->company_email }}@endif
            </div>
        </td>
        <td width="48%" class="right">
            <div style="font-size:26px;font-weight:bold;color:#137fec;letter-spacing:0.04em;">QUOTATION</div>
            <table class="box" style="margin-top:6px;text-align:left;">
                <tr><td class="muted">Quote #</td><td class="right" style="font-weight:bold;">{{ $quotation->quotation_number }}</td></tr>
                <tr><td class="muted">Date</td><td class="right">{{ $quotation->issue_date->format('M d, Y') }}</td></tr>
                <tr><td class="muted">Valid Until</td><td class="right">{{ $quotation->expiry_date ? $quotation->expiry_date->format('M d, Y') : '—' }}</td></tr>
                <tr><td class="muted">Status</td><td class="right">{{ ucfirst($quotation->status) }}</td></tr>
            </table>
        </td>
    </tr>
</table>

<table class="section" style="background:#fef3c7;border-left:4px solid #f59e0b;">
    <tr><td style="padding:10px 12px;font-size:12px;font-weight:bold;color:#92400e;">Valid until {{ $quotation->expiry_date ? $quotation->expiry_date->format('F d, Y') : '—' }}</td></tr>
</table>

@if($quotation->customer)
<table class="section">
    <tr><td style="font-size:10px;font-weight:bold;color:#137fec;text-transform:uppercase;">Quoted To</td></tr>
    <tr><td style="font-size:15px;font-weight:bold;">{{ $quotation->customer->name }}</td></tr>
    <tr><td class="muted" style="font-size:11px;">
        @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
    </td></tr>
</table>
@endif

<table class="section">
    <thead>
        <tr>
            <th class="head-blue" align="left">Description</th>
            <th class="head-blue right">Qty</th>
            <th class="head-blue right">Unit</th>
            <th class="head-blue right">Tax</th>
            <th class="head-blue 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 #e5e7eb;">
                <strong>{{ $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 #e5e7eb;">{{ number_format($item->quantity, 2) }}</td>
            <td class="right" style="border-bottom:1px solid #e5e7eb;">{{ $quotation->currency }} {{ number_format($item->unit_price, 2) }}</td>
            <td class="right" style="border-bottom:1px solid #e5e7eb;">{{ $item->tax_rate ?? 0 }}%</td>
            <td class="right" style="border-bottom:1px solid #e5e7eb;"><strong>{{ $quotation->currency }} {{ number_format($line, 2) }}</strong></td>
        </tr>
        @empty
        <tr><td colspan="5" class="muted" style="text-align:center;padding:16px;">No items</td></tr>
        @endforelse
    </tbody>
</table>

<table class="section" style="width:100%;">
    <tr>
        <td width="55%"></td>
        <td width="45%">
            <table>
                <tr><td class="muted">Subtotal</td><td class="right">{{ $quotation->currency }} {{ number_format($quotation->subtotal, 2) }}</td></tr>
                @if(($quotation->tax_amount ?? 0) > 0)
                <tr><td class="muted">Tax</td><td class="right">{{ $quotation->currency }} {{ number_format($quotation->tax_amount, 2) }}</td></tr>
                @endif
                @if(($quotation->discount_amount ?? 0) > 0)
                <tr><td class="muted">Discount</td><td class="right">-{{ $quotation->currency }} {{ number_format($quotation->discount_amount, 2) }}</td></tr>
                @endif
                <tr><td style="font-weight:bold;font-size:15px;color:#137fec;padding-top:10px;border-top:3px solid #137fec;">Total</td><td class="right" style="font-weight:bold;font-size:15px;color:#137fec;padding-top:10px;border-top:3px solid #137fec;">{{ $quotation->currency }} {{ number_format($quotation->total, 2) }}</td></tr>
            </table>
        </td>
    </tr>
</table>

@if($quotation->notes)
<table class="section"><tr><td style="font-size:12px;font-weight:bold;">Notes</td></tr><tr><td class="muted" style="font-size:11px;line-height:1.5;">{{ $quotation->notes }}</td></tr></table>
@endif

<table class="section box"><tr><td style="font-size:12px;font-weight:bold;">Terms &amp; Conditions</td></tr>
<tr><td class="muted" style="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>
