<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>{{ $quotation->quotation_number }}</title>
    <style>
        body { font-family: DejaVu Sans, sans-serif; font-size: 11px; color: #222; margin: 24px; }
        h1 { font-size: 20px; margin: 0 0 4px; color: #0f4c5c; }
        h2 { font-size: 13px; margin: 18px 0 6px; color: #0f4c5c; }
        .meta { color: #555; margin-bottom: 12px; }
        .box { border: 1px solid #ddd; padding: 10px; margin-top: 10px; }
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        th, td { border: 1px solid #ccc; padding: 6px; text-align: left; vertical-align: top; }
        th { background: #f0f4f5; }
        .text-right { text-align: right; }
        .totals td { border: none; padding: 3px 6px; }
        .totals tr.grand td { font-size: 13px; font-weight: bold; padding-top: 8px; }
        .footer { margin-top: 24px; font-size: 10px; color: #666; border-top: 1px solid #ddd; padding-top: 8px; }
    </style>
</head>
<body>
    @php
        $customer = is_array($quotation->customer_snapshot) ? $quotation->customer_snapshot : (json_decode((string) $quotation->customer_snapshot, true) ?: []);
        $event = is_array($quotation->event_snapshot) ? $quotation->event_snapshot : (json_decode((string) $quotation->event_snapshot, true) ?: []);
        $spaces = is_array($quotation->spaces_snapshot) ? $quotation->spaces_snapshot : (json_decode((string) $quotation->spaces_snapshot, true) ?: []);
    @endphp

    <h1>Quotation</h1>
    <div class="meta">
        <strong>{{ $settings['property_name'] ?? 'Venue Events Manager' }}</strong><br>
        {{ $settings['address'] ?? '' }}
        @if (!empty($settings['phone'])) | Tel: {{ $settings['phone'] }} @endif
        @if (!empty($settings['email'])) | {{ $settings['email'] }} @endif
        @if (!empty($settings['tax_id'])) | TIN: {{ $settings['tax_id'] }} @endif
    </div>

    <table>
        <tr>
            <td width="50%">
                <strong>Quote No:</strong> {{ $quotation->quotation_number }}<br>
                <strong>Version:</strong> {{ $quotation->version }}<br>
                <strong>Date:</strong> {{ $quotation->quotation_date?->format('d/m/Y') }}<br>
                <strong>Valid until:</strong> {{ $quotation->expires_at?->format('d/m/Y') ?: '—' }}<br>
                <strong>Status:</strong> {{ $quotation->status?->label() ?? $quotation->status }}
            </td>
            <td width="50%">
                <strong>Bill To</strong><br>
                {{ $customer['name'] ?? $quotation->customer?->name }}<br>
                {{ $customer['contact_person'] ?? $quotation->customer?->contact_person }}<br>
                {{ $customer['phone'] ?? $quotation->customer?->phone }}<br>
                {{ $customer['email'] ?? $quotation->customer?->email }}<br>
                {{ $customer['billing_address'] ?? $quotation->customer?->billing_address }}
            </td>
        </tr>
    </table>

    @if (!empty($event) || $quotation->booking)
        <h2>Event</h2>
        <div class="box">
            <strong>{{ $event['event_name'] ?? $quotation->booking?->event_name ?? 'Event' }}</strong><br>
            Guests: {{ $event['guest_count'] ?? $quotation->guest_count }} |
            Start: {{ $event['event_start'] ?? ($quotation->booking?->event_start?->format('d/m/Y H:i') ?? '—') }} |
            End: {{ $event['event_end'] ?? ($quotation->booking?->event_end?->format('d/m/Y H:i') ?? '—') }}
        </div>
    @endif

    @if (!empty($spaces))
        <h2>Spaces</h2>
        <ul>
            @foreach ($spaces as $space)
                <li>{{ is_array($space) ? ($space['name'] ?? $space['space_name'] ?? json_encode($space)) : $space }}</li>
            @endforeach
        </ul>
    @endif

    <h2>Charges</h2>
    <table>
        <thead>
            <tr>
                <th>Description</th>
                <th class="text-right">Qty</th>
                <th>Basis</th>
                <th class="text-right">Unit Price</th>
                <th class="text-right">Tax</th>
                <th class="text-right">Line Total</th>
            </tr>
        </thead>
        <tbody>
            @forelse ($quotation->items as $item)
                <tr>
                    <td>{{ $item->description }}</td>
                    <td class="text-right">{{ number_format((float) $item->quantity, 2) }}</td>
                    <td>{{ $item->charge_basis?->label() ?? $item->charge_basis }}</td>
                    <td class="text-right">{{ number_format((float) $item->unit_price, 2) }}</td>
                    <td class="text-right">{{ number_format((float) $item->tax_amount, 2) }}</td>
                    <td class="text-right">{{ number_format((float) $item->line_total, 2) }}</td>
                </tr>
            @empty
                <tr><td colspan="6">No line items</td></tr>
            @endforelse
        </tbody>
    </table>

    <table class="totals" style="width: 45%; margin-left: auto;">
        <tr><td class="text-right">Subtotal</td><td class="text-right">{{ number_format((float) $quotation->subtotal, 2) }}</td></tr>
        <tr><td class="text-right">Discount</td><td class="text-right">{{ number_format((float) $quotation->discount_amount, 2) }}</td></tr>
        <tr><td class="text-right">Service charge</td><td class="text-right">{{ number_format((float) $quotation->service_charge_amount, 2) }}</td></tr>
        <tr><td class="text-right">Tax</td><td class="text-right">{{ number_format((float) $quotation->tax_amount, 2) }}</td></tr>
        <tr class="grand"><td class="text-right">Grand Total</td><td class="text-right">{{ number_format((float) $quotation->grand_total, 2) }} {{ $quotation->currency }}</td></tr>
        <tr><td class="text-right">Deposit required</td><td class="text-right">{{ number_format((float) $quotation->deposit_required, 2) }}</td></tr>
    </table>

    @if ($quotation->payment_terms || $quotation->notes || $quotation->terms_and_conditions)
        <h2>Terms &amp; Notes</h2>
        @if ($quotation->payment_terms)<p><strong>Payment terms:</strong> {{ $quotation->payment_terms }}</p>@endif
        @if ($quotation->notes)<p><strong>Notes:</strong> {{ $quotation->notes }}</p>@endif
        @if ($quotation->terms_and_conditions)<p>{{ $quotation->terms_and_conditions }}</p>@endif
    @endif

    <div class="footer">
        Generated {{ now()->format('d/m/Y H:i') }} — {{ $quotation->quotation_number }}
    </div>
</body>
</html>
