<!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; position: relative; background: #fff; }
        .premium-header { background: linear-gradient(135deg, rgba(52, 0, 128, 1) 0%, rgba(15, 23, 42, 1) 100%); color: #fff; padding-top: 50px; padding-bottom: 50px; padding-left: 80px; padding-right: 80px; position: relative; }
        /* Table layout: Dompdf does not reliably lay out flex/grid like browsers */
        .header-layout { width: 100%; border-collapse: collapse; }
        .header-brand-cell { vertical-align: middle; padding-right: 20px; }
        .header-badge-cell { vertical-align: middle; text-align: right; }
        .brand-section { }
        .brand-logo-wrap { margin-bottom: 14px; }
        .brand-logo { max-height: 64px; width: 140px; max-width: 140px; height: auto; object-fit: contain; display: block; }
        .brand-name { font-size: 25px; font-weight: 700; margin-bottom: 10px; }
        .brand-details { font-size: 14px; opacity: 0.88; line-height: 1.75; }
        .quote-badge { background: rgba(255,255,255,0.15); padding: 20px 30px; border-radius: 12px; text-align: center; }
        .quote-label { font-size: 12px; text-transform: uppercase; letter-spacing: 2px; opacity: 0.9; margin-bottom: 10px; }
        .quote-number { font-size: 28px; font-weight: 700; }
        .content { padding: 40px 24px; }
        .client-section { background: #f8fafc; padding: 30px; border-radius: 12px; margin-bottom: 40px; border-left: 4px solid #1e293b; }
        .client-label { font-size: 11px; text-transform: uppercase; color: #64748b; letter-spacing: 1px; margin-bottom: 12px; }
        .client-name { font-size: 22px; font-weight: 700; color: #1e293b; margin-bottom: 10px; }
        .client-details { font-size: 14px; color: #64748b; line-height: 2; }
        .pricing-table { width: 100%; border-collapse: collapse; margin: 40px 0; }
        .pricing-table thead { background-color: #340080; color: #fff; }
        .pricing-table th { background-color: #340080; padding: 18px 20px; text-align: left; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 1px; }
        .pricing-table td { padding: 20px; border-bottom: 1px solid #e2e8f0; font-size: 14px; }
        .pricing-table tbody tr:hover { background: #f8fafc; }
        .text-right { text-align: right; }
        .total-box { background-color: #340080; color: #fff; padding: 35px; border-radius: 12px; margin-top: 40px; max-width: 450px; margin-left: auto; }
        .total-table { width: 100%; border-collapse: collapse; }
        .total-row td { padding: 12px 0; font-size: 14px; border-bottom: 1px solid rgba(255,255,255,0.1); vertical-align: top; }
        .total-row.grand-total td { border-bottom: none; border-top: 2px solid rgba(255,255,255,0.2); padding-top: 20px; font-size: 24px; font-weight: 700; }
        .total-label { opacity: 0.9; font-size: 16px; }
        .total-value { font-weight: 600; }
        .acceptance-section { margin-top: 50px; padding-top: 40px; border-top: 2px solid #e2e8f0; }
        .acceptance-title { font-size: 16px; font-weight: 700; color: #1e293b; margin-bottom: 25px; }
        .signature-layout { width: 100%; border-collapse: collapse; margin-top: 30px; }
        .signature-col { width: 50%; vertical-align: top; padding: 0 20px 0 0; }
        .signature-col + .signature-col { padding: 0 0 0 20px; }
        .signature-box { border: 2px solid #e2e8f0; border-radius: 8px; padding: 25px; min-height: 100px; }
        .signature-label { font-size: 12px; color: #64748b; margin-top: 10px; }
        .terms-box { background: #f8fafc; padding: 25px; border-radius: 8px; margin-top: 30px; }
        .terms-title { font-size: 13px; font-weight: 700; color: #1e293b; margin-bottom: 12px; }
        .terms-text { font-size: 13px; color: #64748b; line-height: 1.8; }
        @media print {
            .quotation-container { max-width: 100%; padding: 0; }
            .premium-header { break-inside: avoid; padding-top: 32px; padding-bottom: 32px; padding-left: 80px; padding-right: 80px; }
            .content { padding: 32px 20px; }
        }
    </style>
</head>
<body>
    <div class="quotation-container">
        <div class="premium-header">
            <table class="header-layout" width="100%" cellspacing="0" cellpadding="0">
                <tr>
                    <td class="header-brand-cell" valign="middle" width="65%">
                <div class="brand-section">
                    @php
                        $companySettings = \App\Models\CompanySetting::getSettings();
                        $hasCompanyLogo = $companySettings && $companySettings->company_logo && \Illuminate\Support\Facades\Storage::disk('public')->exists($companySettings->company_logo);
                    @endphp
                    @if($hasCompanyLogo)
                    <div class="brand-logo-wrap">
                        <img src="{{ \Illuminate\Support\Facades\Storage::url($companySettings->company_logo) }}" alt="{{ $companySettings->company_name ?? config('app.name') }}" class="brand-logo">
                    </div>
                    @endif
                    <div class="brand-name">{{ $companySettings->company_name ?? config('app.name') }}</div>
                    @php
                        $cityLine = trim(implode(', ', array_filter([
                            $companySettings->company_city ?? null,
                            $companySettings->company_state ?? null,
                            $companySettings->company_zip ?? null,
                        ])));
                        $hasBrandDetails = $companySettings->company_address
                            || $cityLine !== ''
                            || !empty($companySettings->company_country)
                            || $companySettings->company_phone
                            || $companySettings->company_email
                            || $companySettings->company_tax_id;
                    @endphp
                    @if($hasBrandDetails)
                    <div class="brand-details">
                        @if($companySettings->company_address)<div>{{ $companySettings->company_address }}</div>@endif
                        @if($cityLine !== '')<div>{{ $cityLine }}</div>@endif
                        @if(!empty($companySettings->company_country))<div>{{ $companySettings->company_country }}</div>@endif
                        @if($companySettings->company_phone)<div>{{ $companySettings->company_phone }}</div>@endif
                        @if($companySettings->company_email)<div>{{ $companySettings->company_email }}</div>@endif
                        @if($companySettings->company_tax_id)<div>Tax ID: {{ $companySettings->company_tax_id }}</div>@endif
                    </div>
                    @endif
                </div>
                    </td>
                    <td class="header-badge-cell" valign="middle" align="right" width="35%">
                <div class="quote-badge">
                    <div class="quote-label">Quotation</div>
                    <div class="quote-number">{{ $quotation->quotation_number ?? 'QUO-001' }}</div>
                    <div style="margin-top: 15px; font-size: 12px; opacity: 0.8;">
                        {{ ($quotation->issue_date ?? now())->format('M d, Y') }} - 
                        {{ ($quotation->expiry_date ?? now()->addDays(30))->format('M d, Y') }}
                    </div>
                </div>
                    </td>
                </tr>
            </table>
        </div>

        <div class="content">
            <div class="client-section">
                <div class="client-label">Quoted To</div>
                @if($quotation->customer ?? null)
                <div class="client-name">{{ $quotation->customer->name }}</div>
                <div class="client-details">
                    @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>

            <table class="pricing-table">
                <thead>
                    <tr>
                        <th>Service / Item</th>
                        <th class="text-right">Quantity</th>
                        <th class="text-right">Unit Price</th>
                        <th class="text-right">Tax</th>
                        <th class="text-right">Total</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($quotation->items ?? [] as $item)
                    <tr>
                        <td>
                            <strong>{{ $item->description }}</strong>
                            @if($item->notes)
                                <br><small style="color: #64748b;">{{ $item->notes }}</small>
                            @endif
                        </td>
                        <td class="text-right">{{ number_format($item->quantity, 2) }}</td>
                        <td class="text-right">{{ $quotation->currency ?? 'USD' }} {{ number_format($item->unit_price, 2) }}</td>
                        <td class="text-right">{{ $item->tax_rate ?? 0 }}%</td>
                        <td class="text-right"><strong>{{ $quotation->currency ?? 'USD' }} {{ number_format(($item->quantity * $item->unit_price) * (1 + ($item->tax_rate ?? 0) / 100), 2) }}</strong></td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" style="text-align: center; padding: 40px; color: #94a3b8;">No items</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>

            <div class="total-box">
                <table class="total-table" width="100%" cellspacing="0" cellpadding="0">
                <tr class="total-row">
                    <td class="total-label">Subtotal</td>
                    <td class="total-value" align="right">{{ $quotation->currency ?? 'USD' }} {{ number_format($quotation->subtotal ?? 0, 2) }}</td>
                </tr>
                @if(($quotation->tax_amount ?? 0) > 0)
                <tr class="total-row">
                    <td class="total-label">Tax</td>
                    <td class="total-value" align="right">{{ $quotation->currency ?? 'USD' }} {{ number_format($quotation->tax_amount ?? 0, 2) }}</td>
                </tr>
                @endif
                @if(($quotation->discount_amount ?? 0) > 0)
                <tr class="total-row">
                    <td class="total-label">Discount</td>
                    <td class="total-value" align="right">-{{ $quotation->currency ?? 'USD' }} {{ number_format($quotation->discount_amount ?? 0, 2) }}</td>
                </tr>
                @endif
                <tr class="total-row grand-total">
                    <td>Total Estimate</td>
                    <td align="right">{{ $quotation->currency ?? 'USD' }} {{ number_format($quotation->total ?? 0, 2) }}</td>
                </tr>
                </table>
            </div>

            <div class="acceptance-section">
                <div class="acceptance-title">Acceptance & Authorization</div>
                <table class="signature-layout" width="100%" cellspacing="0" cellpadding="0">
                    <tr>
                        <td class="signature-col" valign="top" width="50%">
                        <div class="signature-box"></div>
                        <div class="signature-label">Client Signature</div>
                        </td>
                        <td class="signature-col" valign="top" width="50%">
                        <div class="signature-box"></div>
                        <div class="signature-label">Date</div>
                        </td>
                    </tr>
                </table>
                @if($quotation->notes)
                <div class="terms-box">
                    <div class="terms-title">Notes</div>
                    <div class="terms-text">{{ $quotation->notes }}</div>
                </div>
                @endif
                <div class="terms-box">
                    <div class="terms-title">Terms & Conditions</div>
                    <div class="terms-text">{{ $quotation->terms ?? 'This quotation is valid for 30 days from the date of issue. All prices are subject to change without notice. Payment terms: Net 30 days from invoice date.' }}</div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
