<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{ $doc['document_number'] ?? 'Draft' }}</title>
    <style>
        body { font-family: DejaVu Sans, sans-serif; font-size: 11px; color: #111827; }
        h1 { font-size: 18px; margin: 0 0 8px 0; }
        .muted { color: #6b7280; font-size: 10px; }
        table { width: 100%; border-collapse: collapse; margin-top: 16px; }
        th, td { border: 1px solid #e5e7eb; padding: 6px 8px; text-align: left; }
        th { background: #f9fafb; font-weight: 600; }
        .num { text-align: right; }
        .totals { margin-top: 12px; width: 40%; margin-left: auto; }
        .totals td { border: none; padding: 4px 0; }
        .banner { background: #fef3c7; padding: 8px; margin-bottom: 12px; font-weight: bold; }
    </style>
</head>
<body>

@if (!empty($doc['is_draft']))
    <div class="banner">CONCEPT</div>
@endif

<h1>{{ $doc['type_label'] ?? 'Invoice' }}</h1>
@if (!empty($doc['document_number']))
    <p class="muted">{{ $doc['document_number'] }}</p>
@endif

<table style="border: none; margin-top: 0;">
    <tr>
        <td style="border: none; width: 50%; vertical-align: top;">
            <strong>{{ $doc['organization']['name'] ?? '' }}</strong><br>
            {!! nl2br(e($doc['organization']['address'] ?? '')) !!}<br>
            @if (!empty($doc['organization']['vat_number']))
                VAT: {{ $doc['organization']['vat_number'] }}<br>
            @endif
        </td>
        <td style="border: none; vertical-align: top;">
            <strong>Bill to</strong><br>
            {{ $doc['customer']['name'] ?? '' }}<br>
            @if (!empty($doc['customer']['email']))
                {{ $doc['customer']['email'] }}<br>
            @endif
        </td>
    </tr>
</table>

<p>
    <strong>Date:</strong> {{ $doc['invoice_date'] ?? '' }}
    &nbsp; | &nbsp;
    <strong>Due:</strong> {{ $doc['expiration_date'] ?? '' }}
</p>

@if (!empty($doc['description']))
    <p>{{ $doc['description'] }}</p>
@endif

<table>
    <thead>
    <tr>
        <th>Description</th>
        <th class="num">Qty</th>
        <th class="num">Price ex VAT</th>
        <th class="num">VAT %</th>
        <th class="num">Total ex VAT</th>
        <th class="num">VAT</th>
        <th class="num">Total inc VAT</th>
    </tr>
    </thead>
    <tbody>
    @foreach ($doc['lines'] ?? [] as $row)
        <tr>
            <td>{{ $row['description'] }}</td>
            <td class="num">{{ number_format($row['quantity'], 2, ',', '.') }}</td>
            <td class="num">{{ number_format($row['unit_price_ex_vat'], 2, ',', '.') }}</td>
            <td class="num">{{ number_format($row['vat_percent'], 2, ',', '.') }}</td>
            <td class="num">{{ number_format($row['line_total_ex_vat'], 2, ',', '.') }}</td>
            <td class="num">{{ number_format($row['line_vat_amount'], 2, ',', '.') }}</td>
            <td class="num">{{ number_format($row['line_total_inc_vat'], 2, ',', '.') }}</td>
        </tr>
    @endforeach
    </tbody>
</table>

<table class="totals">
    <tr>
        <td>Subtotal ex VAT</td>
        <td class="num">€ {{ number_format($doc['subtotal_ex_vat'] ?? 0, 2, ',', '.') }}</td>
    </tr>
    <tr>
        <td>VAT</td>
        <td class="num">€ {{ number_format($doc['total_vat'] ?? 0, 2, ',', '.') }}</td>
    </tr>
    <tr>
        <td><strong>Total inc VAT</strong></td>
        <td class="num"><strong>€ {{ number_format($doc['total_inc_vat'] ?? 0, 2, ',', '.') }}</strong></td>
    </tr>
</table>

<p class="muted" style="margin-top: 32px;">EUR — Prices excluding VAT per line unless stated.</p>

</body>
</html>
