<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>{{ $payment->payment_number ?? 'Receipt' }}</title>
    @include('receipts.partials.pdf-styles')
    <style>
        .title-bar { background: #059669; color: #fff; text-align: center; font-size: 14px; letter-spacing: 2px; padding: 12px 8px; }
        .sum-box { background: #ecfdf5; border: 2px solid #10b981; }
        .sum-row td { border-bottom: 1px solid #d1fae5; }
        .sum-total td { border-top: 2px solid #10b981; font-weight: bold; font-size: 13px; color: #047857; }
    </style>
</head>
<body>
@php
    $companySettings = \App\Models\CompanySetting::getSettings();
@endphp
<div class="container pdf-root">
    {{-- Header: logo + company --}}
    <table class="layout section" cellspacing="0" cellpadding="0">
        <tr>
            <td width="28%" class="text-center" style="padding-bottom:12px;">
                @if($companySettings && $companySettings->company_logo && \Illuminate\Support\Facades\Storage::disk('public')->exists($companySettings->company_logo))
                    <img src="{{ \Illuminate\Support\Facades\Storage::url($companySettings->company_logo) }}" alt="" style="max-width:120px;max-height:64px;">
                @endif
            </td>
            <td width="72%" class="text-left">
                <div class="font-bold" style="font-size:16px;">{{ $companySettings->company_name ?? config('app.name') }}</div>
                <div style="font-size:11px;color:#6b7280;">
                    @if($companySettings->company_address ?? null){{ $companySettings->company_address }}<br>@endif
                    @if($companySettings->company_phone ?? null){{ $companySettings->company_phone }}@endif
                    @if($companySettings->company_email ?? null) @if($companySettings->company_phone ?? null) · @endif {{ $companySettings->company_email }}@endif
                </div>
            </td>
        </tr>
    </table>
    <table class="layout section" cellspacing="0" cellpadding="0">
        <tr><td class="title-bar">PAYMENT RECEIPT</td></tr>
    </table>

    {{-- Receipt meta --}}
    <table class="layout section" cellspacing="0" cellpadding="0" style="background:#f9fafb;border:1px solid #e5e7eb;">
        <tr>
            <td width="33%">
                <div class="label-muted">Receipt #</div>
                <div class="font-bold">{{ $payment->payment_number ?? '—' }}</div>
            </td>
            <td width="34%" class="text-center">
                <div class="label-muted">Date</div>
                <div class="font-bold">{{ ($payment->payment_date ?? now())->format('M d, Y') }}</div>
            </td>
            <td width="33%" class="text-right">
                <div class="label-muted">Method</div>
                <div class="font-bold">{{ ucfirst(str_replace('_', ' ', (string) ($payment->payment_method ?? '—'))) }}</div>
            </td>
        </tr>
    </table>

    @if($payment->customer ?? null)
    <table class="layout section" cellspacing="0" cellpadding="0">
        <tr><td class="label-muted border-b" style="padding-bottom:4px;">Paid by</td></tr>
        <tr><td class="font-bold">{{ $payment->customer->name }}</td></tr>
        <tr><td style="font-size:11px;color:#4b5563;">
            @if($payment->customer->email){{ $payment->customer->email }}@endif
            @if($payment->customer->phone)@if($payment->customer->email)<br>@endif{{ $payment->customer->phone }}@endif
        </td></tr>
    </table>
    @endif

    @if($payment->invoice ?? null)
    <table class="layout section" cellspacing="0" cellpadding="0">
        <tr><td class="label-muted border-b">Invoice reference</td></tr>
        <tr>
            <td width="70%" class="font-bold">#{{ $payment->invoice->invoice_number }}</td>
            <td width="30%" class="text-right font-bold">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->amount ?? 0, 2) }}</td>
        </tr>
        <tr><td colspan="2" style="font-size:10px;color:#6b7280;">Due {{ $payment->invoice->due_date->format('M d, Y') }}</td></tr>
    </table>
    @endif

    <table class="layout section sum-box" cellspacing="0" cellpadding="0">
        <tr><td colspan="2" class="font-bold" style="color:#047857;padding-bottom:8px;">Payment summary</td></tr>
        <tr class="sum-row">
            <td width="55%">Amount paid</td>
            <td width="45%" class="text-right font-bold">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->amount ?? 0, 2) }}</td>
        </tr>
        @if($payment->transaction_id)
        <tr class="sum-row">
            <td>Transaction ID</td>
            <td class="text-right">{{ $payment->transaction_id }}</td>
        </tr>
        @endif
        @if($payment->reference)
        <tr class="sum-row">
            <td>Reference</td>
            <td class="text-right">{{ $payment->reference }}</td>
        </tr>
        @endif
        <tr class="sum-total">
            <td>Total received</td>
            <td class="text-right amount-lg" style="font-size:16px;padding-top:10px;">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->amount ?? 0, 2) }}</td>
        </tr>
    </table>

    @if($payment->notes ?? null)
    <table class="layout section" cellspacing="0" cellpadding="0" style="background:#f9fafb;border:1px solid #e5e7eb;">
        <tr><td class="label-muted">Notes</td></tr>
        <tr><td style="font-size:11px;color:#4b5563;">{{ $payment->notes }}</td></tr>
    </table>
    @endif

    <table class="layout section" cellspacing="0" cellpadding="0">
        <tr><td class="text-center border-b" style="padding-top:16px;padding-bottom:8px;">
            <div class="font-bold" style="color:#059669;">Thank you for your payment</div>
            <div style="font-size:10px;color:#6b7280;margin-top:6px;">This receipt is proof of payment. Please retain for your records.</div>
        </td></tr>
    </table>
</div>
</body>
</html>
