<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>{{ $payment->payment_number ?? 'Receipt' }}</title>
    @include('receipts.partials.pdf-styles')
    <style>
        .head-purple { background: #340080; color: #fff; padding: 14px 10px; }
        .box { border: 2px solid #e5e7eb; }
        .box-title { font-size: 11px; font-weight: bold; color: #1e3a5f; text-transform: uppercase; letter-spacing: 1px; padding-bottom: 8px; border-bottom: 1px solid #e5e7eb; margin-bottom: 8px; }
    </style>
</head>
<body>
@php
    $companySettings = \App\Models\CompanySetting::getSettings();
@endphp
<div class="container pdf-root">
    <table class="layout section" cellspacing="0" cellpadding="0" width="100%">
        <tr>
            <td class="head-purple" width="65%">
                <table class="layout" cellspacing="0" cellpadding="0" width="100%">
                    <tr>
                        <td width="30%">
                            @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:90px;max-height:48px;">
                            @endif
                        </td>
                        <td width="70%">
                            <div class="font-bold" style="font-size:15px;">{{ $companySettings->company_name ?? config('app.name') }}</div>
                        </td>
                    </tr>
                </table>
            </td>
            <td class="head-purple text-center" width="35%" style="border-left:1px solid rgba(255,255,255,.25);">
                <div class="label-muted" style="color:rgba(255,255,255,.85);">Receipt</div>
                <div class="font-bold" style="font-size:13px;">{{ $payment->payment_number ?? '—' }}</div>
            </td>
        </tr>
    </table>

    <table class="layout section" cellspacing="0" cellpadding="0" width="100%">
        <tr><td style="background:#f3f4f6;padding:10px;text-align:center;font-size:12px;font-weight:600;">PAYMENT RECEIPT</td></tr>
    </table>

    <table class="layout section box" cellspacing="0" cellpadding="0" width="100%" style="padding:12px;">
        <tr><td colspan="2" class="box-title">Payment information</td></tr>
        <tr>
            <td width="50%">
                <div class="label-muted">Date</div>
                <div class="font-bold">{{ ($payment->payment_date ?? now())->format('F d, Y') }}</div>
            </td>
            <td width="50%">
                <div class="label-muted">Method</div>
                <div class="font-bold">{{ ucfirst(str_replace('_', ' ', (string) ($payment->payment_method ?? '—'))) }}</div>
            </td>
        </tr>
        @if($payment->transaction_id)
        <tr><td colspan="2" style="padding-top:8px;">
            <div class="label-muted">Transaction ID</div>
            <div>{{ $payment->transaction_id }}</div>
        </td></tr>
        @endif
        @if($payment->reference)
        <tr><td colspan="2" style="padding-top:8px;">
            <div class="label-muted">Reference</div>
            <div>{{ $payment->reference }}</div>
        </td></tr>
        @endif
    </table>

    @if($payment->customer ?? null)
    <table class="layout section box" cellspacing="0" cellpadding="0" width="100%" style="padding:12px;">
        <tr><td class="box-title">Customer</td></tr>
        <tr><td class="font-bold">{{ $payment->customer->name }}</td></tr>
        <tr><td style="font-size:11px;color:#6b7280;">
            @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

    <table class="layout section" cellspacing="0" cellpadding="0" width="100%" style="background:#f9fafb;border:1px solid #e5e7eb;padding:12px;">
        <tr><td class="box-title" style="border-color:#d1d5db;">@if($payment->invoice ?? null)Payment breakdown @else Summary @endif</td></tr>
        @if($payment->invoice ?? null)
        <tr class="border-b"><td>Invoice #</td><td class="text-right font-bold">{{ $payment->invoice->invoice_number }}</td></tr>
        <tr class="border-b"><td>Invoice total</td><td class="text-right">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->invoice->total ?? 0, 2) }}</td></tr>
        @endif
        <tr>
            <td class="font-bold" style="padding-top:10px;">Amount paid</td>
            <td class="text-right font-bold amount-lg" style="font-size:18px;padding-top:10px;color:#1e3a5f;">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->amount ?? 0, 2) }}</td>
        </tr>
        @if(($payment->invoice ?? null) && ($payment->invoice->balance ?? 0) > 0)
        <tr><td style="color:#b91c1c;">Balance due</td><td class="text-right" style="color:#b91c1c;">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->invoice->balance, 2) }}</td></tr>
        @endif
    </table>

    @if($payment->notes ?? null)
    <table class="layout section box" cellspacing="0" cellpadding="0" width="100%" style="padding:12px;">
        <tr><td class="box-title">Notes</td></tr>
        <tr><td style="font-size:11px;">{{ $payment->notes }}</td></tr>
    </table>
    @endif

    <table class="layout section" cellspacing="0" cellpadding="0" width="100%">
        <tr><td class="text-center" style="padding-top:14px;font-size:11px;color:#1e3a5f;">
            <div class="font-bold">Thank you for your payment</div>
            <div style="font-size:10px;color:#6b7280;margin-top:4px;">Official proof of payment — retain for your records.</div>
        </td></tr>
    </table>
</div>
</body>
</html>
