{{-- Expects: $invoice, $currency, $issueDate, $dueDate, $daysUntilDue, $paymentMethod, $selectedBankAccount, $paymentUrl --}}
@php
    $thBg = $thBg ?? '#137fec';
    $thText = $thText ?? '#ffffff';
    $thBorderBottom = $thBorderBottom ?? '#0d5aa7';
    $accent = $accent ?? '#137fec';
    $paymentBg = $paymentBg ?? '#eff6ff';
    $paymentBorder = $paymentBorder ?? '#bfdbfe';
    $paymentTitle = $paymentTitle ?? '#1d4ed8';
    $paymentText = $paymentText ?? '#1e3a8a';
@endphp
    <tr>
        <td style="padding:0 0 12px 0;">
            <table class="pdf-items" style="width:100%; border-collapse:collapse; border:1px solid #e5e7eb;">
                <thead>
                    <tr style="background-color:{{ $thBg }}; color:{{ $thText }};">
                        <th style="padding:10px 12px; text-align:left; font-size:14px; font-weight:bold; border-bottom:1px solid {{ $thBorderBottom }};">Description</th>
                        <th style="padding:10px 12px; text-align:right; font-size:14px; font-weight:bold; border-bottom:1px solid {{ $thBorderBottom }}; width:11%;">Qty</th>
                        <th style="padding:10px 12px; text-align:right; font-size:14px; font-weight:bold; border-bottom:1px solid {{ $thBorderBottom }}; width:15%;">Unit</th>
                        <th style="padding:10px 12px; text-align:right; font-size:14px; font-weight:bold; border-bottom:1px solid {{ $thBorderBottom }}; width:9%;">Tax</th>
                        <th style="padding:10px 12px; text-align:right; font-size:14px; font-weight:bold; border-bottom:1px solid {{ $thBorderBottom }}; width:16%;">Total</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($invoice->items ?? [] as $item)
                        @php
                            $lineTotal = $item->total ?? (($item->quantity * $item->unit_price) * (1 + ((float) ($item->tax_rate ?? 0)) / 100));
                        @endphp
                        <tr style="page-break-inside:auto;">
                            <td style="padding:10px 12px; border-bottom:1px solid #e5e7eb;">
                                <strong>{{ $item->description }}</strong>@if(!empty($item->notes))<span style="font-size:13px; color:#6b7280;"> — {{ $item->notes }}</span>@endif
                            </td>
                            <td style="padding:10px 12px; border-bottom:1px solid #e5e7eb; text-align:right;">{{ number_format((float) $item->quantity, 2) }}</td>
                            <td style="padding:10px 12px; border-bottom:1px solid #e5e7eb; text-align:right;">{{ $currency }} {{ number_format((float) $item->unit_price, 2) }}</td>
                            <td style="padding:10px 12px; border-bottom:1px solid #e5e7eb; text-align:right;">{{ number_format((float) ($item->tax_rate ?? 0), 2) }}%</td>
                            <td style="padding:10px 12px; border-bottom:1px solid #e5e7eb; text-align:right;"><strong>{{ $currency }} {{ number_format((float) $lineTotal, 2) }}</strong></td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" style="padding:14px 12px; text-align:center; color:#9ca3af; border-bottom:1px solid #e5e7eb;">No items</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </td>
    </tr>

    <tr>
        <td style="padding:0 0 12px 0;" class="pdf-section">
            <table style="width:100%; border-collapse:collapse;">
                <tr>
                    <td style="width:55%;"></td>
                    <td style="width:45%; vertical-align:top;">
                        @if(($totalsStyle ?? '') === 'bluebox')
                        <table style="width:100%; border-collapse:collapse; background-color:{{ $accent }}; color:#ffffff;">
                            <tr>
                                <td style="padding:10px 12px; font-size:14px;">Subtotal</td>
                                <td style="padding:10px 12px; font-size:14px; text-align:right; font-weight:600;">{{ $currency }} {{ number_format((float) ($invoice->subtotal ?? 0), 2) }}</td>
                            </tr>
                            @if((float) ($invoice->tax_amount ?? 0) > 0)
                            <tr>
                                <td style="padding:10px 12px; font-size:14px; border-top:1px solid rgba(255,255,255,0.25);">Tax</td>
                                <td style="padding:10px 12px; font-size:14px; text-align:right; font-weight:600; border-top:1px solid rgba(255,255,255,0.25);">{{ $currency }} {{ number_format((float) ($invoice->tax_amount ?? 0), 2) }}</td>
                            </tr>
                            @endif
                            @if((float) ($invoice->discount_amount ?? 0) > 0)
                            <tr>
                                <td style="padding:10px 12px; font-size:14px; border-top:1px solid rgba(255,255,255,0.25);">Discount</td>
                                <td style="padding:10px 12px; font-size:14px; text-align:right; font-weight:600; border-top:1px solid rgba(255,255,255,0.25);">-{{ $currency }} {{ number_format((float) ($invoice->discount_amount ?? 0), 2) }}</td>
                            </tr>
                            @endif
                            <tr>
                                <td style="padding:10px 12px; font-size:18px; font-weight:bold; border-top:2px solid rgba(255,255,255,0.35);">Total due</td>
                                <td style="padding:10px 12px; font-size:18px; font-weight:bold; text-align:right; border-top:2px solid rgba(255,255,255,0.35);">{{ $currency }} {{ number_format((float) ($invoice->total ?? 0), 2) }}</td>
                            </tr>
                            @if((float) ($invoice->paid_amount ?? 0) > 0)
                            <tr>
                                <td style="padding:10px 12px; font-size:14px; border-top:1px solid rgba(255,255,255,0.25);">Paid</td>
                                <td style="padding:10px 12px; font-size:14px; text-align:right; font-weight:600; border-top:1px solid rgba(255,255,255,0.25);">{{ $currency }} {{ number_format((float) ($invoice->paid_amount ?? 0), 2) }}</td>
                            </tr>
                            <tr>
                                <td style="padding:10px 12px; font-size:16px; font-weight:bold; border-top:1px solid rgba(255,255,255,0.25);">Balance due</td>
                                <td style="padding:10px 12px; font-size:16px; font-weight:bold; text-align:right; border-top:1px solid rgba(255,255,255,0.25);">{{ $currency }} {{ number_format((float) ($invoice->balance ?? 0), 2) }}</td>
                            </tr>
                            @endif
                        </table>
                        @else
                        <table style="width:100%; border-collapse:collapse;">
                            <tr>
                                <td style="padding:8px 8px; font-size:14px; color:#6b7280;">Subtotal</td>
                                <td style="padding:8px 8px; font-size:14px; text-align:right; font-weight:600;">{{ $currency }} {{ number_format((float) ($invoice->subtotal ?? 0), 2) }}</td>
                            </tr>
                            @if((float) ($invoice->tax_amount ?? 0) > 0)
                            <tr>
                                <td style="padding:8px 8px; font-size:14px; color:#6b7280; border-top:1px solid #e5e7eb;">Tax</td>
                                <td style="padding:8px 8px; font-size:14px; text-align:right; font-weight:600; border-top:1px solid #e5e7eb;">{{ $currency }} {{ number_format((float) ($invoice->tax_amount ?? 0), 2) }}</td>
                            </tr>
                            @endif
                            @if((float) ($invoice->discount_amount ?? 0) > 0)
                            <tr>
                                <td style="padding:8px 8px; font-size:14px; color:#6b7280; border-top:1px solid #e5e7eb;">Discount</td>
                                <td style="padding:8px 8px; font-size:14px; text-align:right; font-weight:600; border-top:1px solid #e5e7eb;">-{{ $currency }} {{ number_format((float) ($invoice->discount_amount ?? 0), 2) }}</td>
                            </tr>
                            @endif
                            <tr>
                                <td style="padding:8px 8px; font-size:17px; font-weight:bold; color:{{ $accent }}; border-top:2px solid {{ $accent }};">Total due</td>
                                <td style="padding:8px 8px; font-size:17px; font-weight:bold; text-align:right; color:{{ $accent }}; border-top:2px solid {{ $accent }};">{{ $currency }} {{ number_format((float) ($invoice->total ?? 0), 2) }}</td>
                            </tr>
                            @if((float) ($invoice->paid_amount ?? 0) > 0)
                            <tr>
                                <td style="padding:8px 8px; font-size:14px; color:#6b7280; border-top:1px solid #e5e7eb;">Paid</td>
                                <td style="padding:8px 8px; font-size:14px; text-align:right; font-weight:600; border-top:1px solid #e5e7eb;">{{ $currency }} {{ number_format((float) ($invoice->paid_amount ?? 0), 2) }}</td>
                            </tr>
                            <tr>
                                <td style="padding:8px 8px; font-size:16px; font-weight:bold; border-top:1px solid #e5e7eb;">Balance due</td>
                                <td style="padding:8px 8px; font-size:16px; font-weight:bold; text-align:right; border-top:1px solid #e5e7eb;">{{ $currency }} {{ number_format((float) ($invoice->balance ?? 0), 2) }}</td>
                            </tr>
                            @endif
                        </table>
                        @endif
                    </td>
                </tr>
            </table>
        </td>
    </tr>

    <tr>
        <td style="padding:0; border-top:1px solid #e5e7eb;" class="pdf-section">
            <table style="width:100%; border-collapse:collapse;">
                @if(!empty($invoice->notes))
                <tr>
                    <td style="padding:0 0 12px 0;">
                        <div style="font-size:14px; font-weight:bold;">Notes</div>
                        <div style="font-size:13px; color:#4b5563; line-height:1.45;">{{ $invoice->notes }}</div>
                    </td>
                </tr>
                @endif
                @if(!empty($invoice->terms))
                <tr>
                    <td style="padding:0 0 12px 0;">
                        <div style="font-size:14px; font-weight:bold;">Terms &amp; conditions</div>
                        <div style="font-size:13px; color:#4b5563; line-height:1.45;">{{ $invoice->terms }}</div>
                    </td>
                </tr>
                @endif
                <tr>
                    <td style="padding:12px 14px; background-color:{{ $paymentBg }}; border:1px solid {{ $paymentBorder }};">
                        <table style="width:100%; border-collapse:collapse;">
                            <tr>
                                <td style="font-size:14px; font-weight:bold; color:{{ $paymentTitle }}; padding:0 0 6px 0;">Payment instructions</td>
                            </tr>
                            <tr>
                                <td style="font-size:13px; color:{{ $paymentText }}; line-height:1.45; padding:0;">
                                    Please make payment within <strong>{{ $daysUntilDue }}</strong> days of the invoice date.
                                </td>
                            </tr>
                            @if($paymentMethod === 'online')
                            <tr>
                                <td style="font-size:13px; color:{{ $paymentText }}; padding:10px 0 0 0; border-top:1px solid {{ $paymentBorder }}; line-height:1.45;">
                                    <strong>Payment method:</strong> Online payment. Payments via payment gateway.@if($paymentUrl) <span style="word-break:break-all;">{{ $paymentUrl }}</span>@endif
                                </td>
                            </tr>
                            @elseif($selectedBankAccount)
                            <tr>
                                <td style="font-size:13px; color:{{ $paymentText }}; padding:10px 0 0 0; border-top:1px solid {{ $paymentBorder }}; line-height:1.45;">
                                    <strong>Bank transfer:</strong> {{ $selectedBankAccount->bank_name }}
                                    @if($selectedBankAccount->account_name) — {{ $selectedBankAccount->account_name }}@endif
                                    @if($selectedBankAccount->account_number) — {{ $selectedBankAccount->account_number }}@endif
                                    @if($selectedBankAccount->swift_code) — SWIFT {{ $selectedBankAccount->swift_code }}@endif
                                    @if($selectedBankAccount->iban) — IBAN {{ $selectedBankAccount->iban }}@endif
                                    @if($selectedBankAccount->branch_name) — {{ $selectedBankAccount->branch_name }}@endif
                                    @if($selectedBankAccount->currency) ({{ $selectedBankAccount->currency }})@endif
                                </td>
                            </tr>
                            @else
                            <tr>
                                <td style="font-size:13px; color:{{ $paymentText }}; padding:10px 0 0 0; border-top:1px solid {{ $paymentBorder }}; line-height:1.45;">
                                    Payment via bank transfer or agreed methods.
                                </td>
                            </tr>
                            @endif
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
