@php
    $party = $party ?? ($payment->isReceived() ? $payment->customer : $payment->vendor);
    $docOpts = $docSettings['options'] ?? [];
    $docLbl = $docSettings['labels'] ?? [];
    $kindMeta = $kindMeta ?? $payment->kindMeta();
    $paymentTitle = $paymentTitle ?? $kindMeta['title'] ?? 'PAYMENT';
    $amountLabel = $amountLabel ?? $kindMeta['amount_label'] ?? 'Amount';
    $forPdf = $forPdf ?? false;
    if ($forPdf) {
        $logoImage = $logoPath ?? \App\Helpers\CompanyMedia::pdfImagePath($company->logo_path ?? null);
        $signatureImage = $signaturePath ?? \App\Helpers\CompanyMedia::pdfImagePath($company->signature_image_path ?? null);
    } else {
        $logoImage = $logoPath ?? $logoSrc ?? \App\Helpers\CompanyMedia::imageSource($company->logo_path ?? null);
        $signatureImage = $signaturePath ?? $signatureSrc ?? \App\Helpers\CompanyMedia::imageSource($company->signature_image_path ?? null);
    }
@endphp

<div class="billing-invoice-doc{{ ($forPdf ?? false) ? ' billing-invoice-doc--pdf' : '' }}" id="payment-doc">
    <table class="billing-invoice-doc__layout-table billing-invoice-doc__header" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td width="58%" valign="top" class="billing-invoice-doc__company">
                @if($logoImage)
                    <img src="{{ $logoImage }}" alt="" class="billing-invoice-doc__logo" width="{{ $forPdf ? 120 : 70 }}">
                @endif
                <h1 class="billing-invoice-doc__company-name">{{ $company->name ?? 'Company' }}</h1>
                @foreach($company->fullAddressLines() as $line)
                    <p class="billing-invoice-doc__meta">{{ $line }}</p>
                @endforeach
                @if($company->phone)<p class="billing-invoice-doc__meta">Phone: {{ $company->phone }}</p>@endif
                @if($company->email)<p class="billing-invoice-doc__meta">Email: {{ $company->email }}</p>@endif
                @if($company->gstin)<p class="billing-invoice-doc__meta"><strong>GSTIN:</strong> {{ $company->gstin }}</p>@endif
            </td>
            <td width="42%" valign="top" align="right" class="billing-invoice-doc__title-block">
                <h2 class="billing-invoice-doc__doc-title">{{ $paymentTitle }}</h2>
                <p class="billing-invoice-doc__doc-number">#{{ $payment->payment_number }}</p>
                <p class="billing-invoice-doc__copy-label">{{ $documentCopyLabel ?? 'Original Copy' }}</p>
                <div class="billing-invoice-doc__amount-due mt-2">
                    {{ $amountLabel }}: ₹ {{ number_format((float) $payment->amount, 2) }}
                </div>
            </td>
        </tr>
    </table>

    <table class="billing-invoice-doc__layout-table mt-3" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td width="55%" valign="top">
                <p class="billing-invoice-doc__section-label">{{ $docLbl['receipt_to'] ?? 'Receipt To' }}</p>
                @if($party)
                    <p class="fw-semibold mb-1">{{ $party->name }}</p>
                    @foreach($party->billingAddressLines() as $line)
                        <p class="billing-invoice-doc__meta mb-0">{{ $line }}</p>
                    @endforeach
                    @if($party->gstin ?? null)
                        <p class="billing-invoice-doc__meta mt-1"><strong>GSTIN:</strong> {{ $party->gstin }}</p>
                    @endif
                @endif
            </td>
            <td width="45%" valign="top" align="right">
                <table class="billing-invoice-doc__dates" cellpadding="4" cellspacing="0" align="right">
                    <tr>
                        <td class="billing-invoice-doc__dates-label">{{ $docLbl['payment_date'] ?? 'Payment Date' }}</td>
                        <td class="billing-invoice-doc__dates-value">{{ $payment->payment_date->format('d - M - Y') }}</td>
                    </tr>
                    <tr>
                        <td class="billing-invoice-doc__dates-label">{{ $docLbl['payment_method'] ?? 'Payment Method' }}</td>
                        <td class="billing-invoice-doc__dates-value">{{ $payment->paymentMethodLabel() }}</td>
                    </tr>
                    @if($payment->transaction_ref)
                    <tr>
                        <td class="billing-invoice-doc__dates-label">Reference #</td>
                        <td class="billing-invoice-doc__dates-value">{{ $payment->transaction_ref }}</td>
                    </tr>
                    @endif
                    @if((float) $payment->available_credit > 0)
                    <tr>
                        <td class="billing-invoice-doc__dates-label">{{ $docLbl['available_credit'] ?? 'Available Credit' }}</td>
                        <td class="billing-invoice-doc__dates-value">₹ {{ number_format((float) $payment->available_credit, 2) }}</td>
                    </tr>
                    @endif
                </table>
            </td>
        </tr>
    </table>

    @if($payment->allocations->isNotEmpty())
    <table class="table table-bordered billing-invoice-doc__items-table mt-4" width="100%">
        <thead>
            <tr class="table-light">
                <th>S.No</th>
                <th>{{ $docLbl['col_document_number'] ?? 'Document Number' }}</th>
                <th>{{ $docLbl['col_document_date'] ?? 'Document Date' }}</th>
                <th class="text-end">{{ $docLbl['col_document_amount'] ?? 'Document Amount (₹)' }}</th>
                <th class="text-end">{{ $docLbl['col_payment_amount'] ?? 'Payment Amount (₹)' }}</th>
            </tr>
        </thead>
        <tbody>
            @foreach($payment->allocations as $idx => $alloc)
            <tr>
                <td>{{ $idx + 1 }}</td>
                <td>{{ $alloc->document_number }}</td>
                <td>{{ $alloc->document_date?->format('d - M - Y') ?? '—' }}</td>
                <td class="text-end">{{ number_format((float) $alloc->document_amount, 2) }}</td>
                <td class="text-end">{{ number_format((float) $alloc->allocated_amount, 2) }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
    @endif

    <table class="billing-invoice-doc__layout-table mt-3" width="100%">
        <tr>
            <td width="50%" valign="bottom">
                @if(($docOpts['show_primary_signature'] ?? true) && $signatureImage)
                    <p class="small text-muted mb-1">{{ $settings['primary_signature_label'] ?? 'Provider Signature' }}</p>
                    <img src="{{ $signatureImage }}" alt="" height="{{ $forPdf ? 50 : 40 }}">
                @endif
            </td>
            <td width="50%" valign="bottom" align="right">
                <table align="right" cellpadding="4">
                    <tr>
                        <td class="text-end text-muted">{{ $paymentTitle }}</td>
                        <td class="text-end fw-semibold">₹ {{ number_format((float) $payment->amount, 2) }}</td>
                    </tr>
                    @if((float) $payment->available_credit > 0)
                    <tr>
                        <td class="text-end text-muted">{{ $docLbl['available_credit'] ?? 'Available Credit' }}</td>
                        <td class="text-end fw-semibold">₹ {{ number_format((float) $payment->available_credit, 2) }}</td>
                    </tr>
                    @endif
                </table>
                @if($docOpts['show_secondary_signature'] ?? false)
                    <p class="small text-muted mt-4 mb-0">{{ $settings['secondary_signature_label'] ?? 'Receiver Signature' }}</p>
                    <div style="border-top:1px solid #ccc; width:180px; margin-left:auto; margin-top:40px;"></div>
                @endif
            </td>
        </tr>
    </table>

    @if(($docOpts['show_total_in_words'] ?? true) && !empty($amountInWords))
        <p class="billing-invoice-doc__meta mt-3"><strong>Amount in words:</strong> {{ $amountInWords }}</p>
    @endif

    @if($payment->notes)
        <p class="billing-invoice-doc__meta mt-2"><strong>Notes:</strong> {{ $payment->notes }}</p>
    @endif
</div>
