<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>{{ $payment->payment_number ?? 'Receipt' }}</title>
    @include('receipts.partials.pdf-styles')
    <style>
        .hero { background: #059669; color: #fff; padding: 16px 12px; }
        .hero-side { background: rgba(255,255,255,0.15); text-align: center; padding: 12px 8px; }
        .highlight { background: #ecfdf5; border: 2px solid #10b981; text-align: center; padding: 16px 10px; }
        .amt { font-size: 24px; font-weight: bold; color: #047857; padding: 10px 0 6px; }
    </style>
</head>
<body>
@php
    $companySettings = \App\Models\CompanySetting::getSettings();
@endphp
<div class="container pdf-root">
    <table class="layout section hero" cellspacing="0" cellpadding="0" width="100%">
        <tr>
            <td width="58%" style="vertical-align:middle;">
                @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:110px;max-height:56px;margin-bottom:6px;"><br>
                @endif
                <div class="font-bold" style="font-size:16px;">{{ $companySettings->company_name ?? config('app.name') }}</div>
                <div style="font-size:10px;opacity:.95;margin-top:4px;">
                    @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>
            <td width="42%" class="hero-side" style="vertical-align:middle;">
                <div class="label-muted" style="color:rgba(255,255,255,.9);">Payment receipt</div>
                <div class="font-bold" style="font-size:14px;">{{ $payment->payment_number ?? '—' }}</div>
                <div style="font-size:11px;margin-top:6px;opacity:.95;">{{ ($payment->payment_date ?? now())->format('M d, Y') }}</div>
            </td>
        </tr>
    </table>

    <table class="layout section highlight" cellspacing="0" cellpadding="0" width="100%">
        <tr><td class="label-muted" style="color:#065f46;">Payment received</td></tr>
        <tr><td class="amt">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->amount ?? 0, 2) }}</td></tr>
        <tr><td style="font-size:11px;color:#065f46;">Method: {{ ucfirst(str_replace('_', ' ', (string) ($payment->payment_method ?? '—'))) }}</td></tr>
    </table>

    <table class="layout section" cellspacing="0" cellpadding="0" width="100%" style="background:#f9fafb;border:1px solid #e5e7eb;">
        <tr>
            @if($payment->customer ?? null)
            <td width="50%" style="padding:12px;border-right:1px solid #e5e7eb;">
                <div class="label-muted">Paid by</div>
                <div class="font-bold">{{ $payment->customer->name }}</div>
                <div style="font-size:10px;color:#4b5563;margin-top:4px;">
                    @if($payment->customer->email){{ $payment->customer->email }}@endif
                    @if($payment->customer->phone)@if($payment->customer->email)<br>@endif{{ $payment->customer->phone }}@endif
                </div>
            </td>
            @endif
            <td width="{{ ($payment->customer ?? null) ? '50' : '100' }}" style="padding:12px;">
                <div class="label-muted">Payment details</div>
                <table class="layout" cellspacing="0" cellpadding="0" width="100%">
                    <tr><td style="font-size:11px;">Date</td><td class="text-right" style="font-size:11px;">{{ ($payment->payment_date ?? now())->format('F d, Y') }}</td></tr>
                    @if($payment->transaction_id)
                    <tr><td style="font-size:11px;">Txn ID</td><td class="text-right" style="font-size:11px;">{{ $payment->transaction_id }}</td></tr>
                    @endif
                    @if($payment->reference)
                    <tr><td style="font-size:11px;">Reference</td><td class="text-right" style="font-size:11px;">{{ $payment->reference }}</td></tr>
                    @endif
                </table>
            </td>
        </tr>
    </table>

    @if($payment->invoice ?? null)
    <table class="layout section" cellspacing="0" cellpadding="0" width="100%" style="margin-top:8px;">
        <tr>
            <td width="25%" style="background:#f9fafb;border:1px solid #e5e7eb;padding:10px;text-align:center;">
                <div class="label-muted">Invoice</div>
                <div class="font-bold">{{ $payment->invoice->invoice_number }}</div>
            </td>
            <td width="25%" style="background:#f9fafb;border:1px solid #e5e7eb;padding:10px;text-align:center;">
                <div class="label-muted">Invoice total</div>
                <div class="font-bold">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->invoice->total ?? 0, 2) }}</div>
            </td>
            <td width="25%" style="background:#f9fafb;border:1px solid #e5e7eb;padding:10px;text-align:center;">
                <div class="label-muted">Paid</div>
                <div class="font-bold" style="color:#059669;">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->amount ?? 0, 2) }}</div>
            </td>
            <td width="25%" style="background:#f9fafb;border:1px solid #e5e7eb;padding:10px;text-align:center;">
                <div class="label-muted">Balance</div>
                @php $balColor = (($payment->invoice->balance ?? 0) > 0) ? '#b91c1c' : '#059669'; @endphp
                <div class="font-bold" style="color:{{ $balColor }};">{{ $payment->currency ?? 'USD' }} {{ number_format($payment->invoice->balance ?? 0, 2) }}</div>
            </td>
        </tr>
    </table>
    @endif

    @if($payment->notes ?? null)
    <table class="layout section" cellspacing="0" cellpadding="0" width="100%" style="background:#f9fafb;border:1px solid #e5e7eb;padding:10px;">
        <tr><td class="label-muted">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:16px;border-top:2px solid #e5e7eb;">
            <div class="font-bold" style="font-size:14px;color:#059669;">Thank you</div>
            <div style="font-size:10px;color:#6b7280;margin-top:6px;">Payment processed. Please keep this receipt.</div>
        </td></tr>
    </table>
</div>
</body>
</html>
