<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quotation {{ $quotation->quotation_number ?? 'QUO-001' }}</title>
    <style>
        @page { size: A4 portrait; margin: 12mm; }
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
            color: #111827;
            line-height: 1.6;
            background: #fff !important;
            color-adjust: exact;
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }
        .quotation-container { max-width: 210mm; margin: 0 auto; padding: 25mm; background: #fff; }
        .proposal-header { background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%); color: #fff; padding: 30px; border-radius: 12px; margin-bottom: 40px; }
        .proposal-header-top { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
        .proposal-header-top td { vertical-align: middle; }
        .proposal-title { font-size: 28px; font-weight: 700; margin-bottom: 0; }
        .proposal-header-logo-wrap { text-align: right; }
        .proposal-header-logo { max-height: 56px; max-width: 220px; width: auto; height: auto; object-fit: contain; background: #fff; padding: 8px 12px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
        .proposal-subtitle { font-size: 14px; opacity: 0.9; }
        .quote-number { font-size: 18px; font-weight: 600; margin-top: 15px; }
        .client-section { background: #f9fafb; padding: 25px; border-radius: 8px; margin-bottom: 30px; }
        .client-label { font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 1px; margin-bottom: 10px; }
        .client-name { font-size: 20px; font-weight: 700; color: #111827; margin-bottom: 8px; }
        .client-info { font-size: 13px; color: #6b7280; line-height: 2; }
        .service-description { margin: 30px 0; }
        .service-title { font-size: 16px; font-weight: 600; color: #3b82f6; margin-bottom: 15px; }
        .items-table { width: 100%; border-collapse: collapse; }
        .items-table td { padding: 15px 0; border-bottom: 1px solid #e5e7eb; vertical-align: top; }
        .item-desc { }
        .item-price { font-weight: 600; color: #111827; white-space: nowrap; }
        .pricing-summary { background: #eff6ff; border: 2px solid #3b82f6; border-radius: 8px; padding: 25px; margin: 30px 0; }
        .summary-title { font-size: 16px; font-weight: 700; color: #3b82f6; margin-bottom: 15px; }
        .summary-table { width: 100%; border-collapse: collapse; }
        .summary-table td { padding: 10px 0; font-size: 14px; vertical-align: top; }
        .summary-total td { font-size: 20px; font-weight: 700; color: #111827; padding-top: 15px; border-top: 2px solid #3b82f6; }
        .acceptance-section { margin-top: 40px; padding-top: 30px; border-top: 2px solid #e5e7eb; }
        .signature-layout { width: 100%; border-collapse: collapse; margin-top: 30px; }
        .signature-layout td { width: 50%; vertical-align: top; padding-right: 15px; }
        .signature-layout td + td { padding-right: 0; padding-left: 15px; }
        .signature-field { border-bottom: 2px solid #111827; padding-bottom: 5px; min-height: 60px; }
        .signature-label { font-size: 12px; color: #6b7280; margin-top: 5px; }
        @media print {
            .quotation-container { max-width: 100%; padding: 0; }
        }
    </style>
</head>
<body>
    <div class="quotation-container">
        @php
            $companySettings = \App\Models\CompanySetting::getSettings();
            $hasCompanyLogo = $companySettings && $companySettings->company_logo && \Illuminate\Support\Facades\Storage::disk('public')->exists($companySettings->company_logo);
        @endphp
        <div class="proposal-header">
            <table class="proposal-header-top" width="100%" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="65%" valign="middle">
                        <div class="proposal-title">Sales Proposal</div>
                    </td>
                    <td width="35%" valign="middle" align="right">
                        @if($hasCompanyLogo)
                        <div class="proposal-header-logo-wrap">
                            <img src="{{ \Illuminate\Support\Facades\Storage::url($companySettings->company_logo) }}" alt="{{ $companySettings->company_name ?? 'Company' }} logo" class="proposal-header-logo">
                        </div>
                        @endif
                    </td>
                </tr>
            </table>
            <div class="proposal-subtitle">Professional Service Quotation</div>
            <div class="quote-number">Quote #{{ $quotation->quotation_number ?? 'QUO-001' }}</div>
            <div style="margin-top: 15px; font-size: 13px; opacity: 0.9;">
                Date: {{ ($quotation->issue_date ?? now())->format('F d, Y') }} | 
                Valid Until: {{ ($quotation->expiry_date ?? now()->addDays(30))->format('F d, Y') }}
            </div>
        </div>

        <div class="client-section">
            <div class="client-label">Prepared For</div>
            @if($quotation->customer ?? null)
            <div class="client-name">{{ $quotation->customer->name }}</div>
            <div class="client-info">
                @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>
            @endif
        </div>

        <div class="service-description">
            <div class="service-title">Service Description</div>
            @if($quotation->notes)
            <div style="font-size: 14px; color: #6b7280; line-height: 1.8;">{{ $quotation->notes }}</div>
            @endif
        </div>

        <table class="items-table" cellspacing="0" cellpadding="0">
            @forelse($quotation->items ?? [] as $item)
            <tr>
                <td class="item-desc" width="75%">
                    <strong>{{ $item->description }}</strong>
                    @if($item->notes)
                        <div style="font-size: 12px; color: #9ca3af; margin-top: 5px;">{{ $item->notes }}</div>
                    @endif
                    <div style="font-size: 12px; color: #6b7280; margin-top: 5px;">
                        Qty: {{ number_format($item->quantity, 2) }} × {{ $quotation->currency ?? 'USD' }} {{ number_format($item->unit_price, 2) }}
                        @if($item->tax_rate) + {{ $item->tax_rate }}% tax @endif
                    </div>
                </td>
                <td class="item-price" width="25%" align="right">{{ $quotation->currency ?? 'USD' }} {{ number_format(($item->quantity * $item->unit_price) * (1 + ($item->tax_rate ?? 0) / 100), 2) }}</td>
            </tr>
            @empty
            <tr>
                <td colspan="2" style="text-align: center; padding: 30px; color: #9ca3af;">No items</td>
            </tr>
            @endforelse
        </table>

        <div class="pricing-summary">
            <div class="summary-title">Pricing Summary</div>
            <table class="summary-table" cellspacing="0" cellpadding="0">
                <tr>
                    <td>Subtotal</td>
                    <td align="right">{{ $quotation->currency ?? 'USD' }} {{ number_format($quotation->subtotal ?? 0, 2) }}</td>
                </tr>
                @if(($quotation->tax_amount ?? 0) > 0)
                <tr>
                    <td>Tax</td>
                    <td align="right">{{ $quotation->currency ?? 'USD' }} {{ number_format($quotation->tax_amount ?? 0, 2) }}</td>
                </tr>
                @endif
                @if(($quotation->discount_amount ?? 0) > 0)
                <tr>
                    <td>Discount</td>
                    <td align="right">-{{ $quotation->currency ?? 'USD' }} {{ number_format($quotation->discount_amount ?? 0, 2) }}</td>
                </tr>
                @endif
                <tr class="summary-total">
                    <td>Total</td>
                    <td align="right">{{ $quotation->currency ?? 'USD' }} {{ number_format($quotation->total ?? 0, 2) }}</td>
                </tr>
            </table>
        </div>

        <div class="acceptance-section">
            <div style="font-size: 14px; font-weight: 600; margin-bottom: 20px;">Acceptance</div>
            <table class="signature-layout" cellspacing="0" cellpadding="0">
                <tr>
                    <td>
                        <div class="signature-field"></div>
                        <div class="signature-label">Client Signature</div>
                    </td>
                    <td>
                        <div class="signature-field"></div>
                        <div class="signature-label">Date</div>
                    </td>
                </tr>
            </table>
            @if($quotation->terms)
            <div style="margin-top: 30px; font-size: 12px; color: #6b7280; line-height: 1.8;">
                <strong>Terms:</strong> {{ $quotation->terms }}
            </div>
            @endif
        </div>
    </div>
</body>
</html>
