<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Payment Invoice — {{ $businessName }}</title>
    <style>
        @page { margin: 12mm 15mm; }
        body {
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            color: #1e293b;
            font-size: 13px;
            line-height: 1.5;
        }
        .invoice-box {
            max-width: 800px;
            margin: auto;
            padding: 15px;
            background-color: #fff;
        }
        .header {
            border-bottom: 2px solid #ea580c; /* Orange */
            padding-bottom: 20px;
            margin-bottom: 20px;
        }
        .header-logo {
            max-height: 60px;
            max-width: 240px;
            object-fit: contain;
        }
        .header-text {
            font-size: 26px;
            font-weight: bold;
            color: #ea580c;
        }
        .invoice-title {
            font-size: 18px;
            font-weight: bold;
            color: #ea580c;
            text-align: right;
        }
        .details-table {
            width: 100%;
            margin-bottom: 30px;
            border-collapse: collapse;
        }
        .details-table td {
            padding: 5px;
            vertical-align: top;
        }
        .receipt-items {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
        }
        .receipt-items th {
            background-color: #ea580c;
            color: #fff;
            border-bottom: 2px solid #c2410c;
            padding: 10px;
            text-align: left;
            font-size: 11px;
            text-transform: uppercase;
        }
        .receipt-items td {
            padding: 10px;
            border-bottom: 1px solid #e2e8f0;
        }
        .receipt-items tr:nth-child(even) td {
            background-color: #f8fafc;
        }
        .total-row td {
            border-top: 2px solid #cbd5e1;
            font-weight: bold;
            padding-top: 15px;
        }
        .footer {
            margin-top: 40px;
            font-size: 11px;
            color: #94a3b8;
            text-align: center;
            border-top: 1px solid #e2e8f0;
            padding-top: 15px;
        }
    </style>
</head>
<body>

    <div class="invoice-box">
        <table style="width: 100%;" class="header">
            <tr>
                <td style="width: 50%;">
                    @if(!empty($invoiceLogo) && file_exists(public_path($invoiceLogo)))
                        <img src="{{ public_path($invoiceLogo) }}" alt="Invoice Logo" class="header-logo">
                    @else
                        <span class="header-text">{{ $businessName }}</span>
                    @endif
                </td>
                <td style="width: 50%; text-align: right; vertical-align: bottom;">
                    <span class="invoice-title">Payment Invoice</span><br>
                    <span style="font-size: 11px; color: #64748b;">Invoice #: INV-IN-{{ $cashIn->id }}</span>
                </td>
            </tr>
        </table>

        <table class="details-table">
            <tr>
                <td style="width: 50%;">
                    <strong style="color: #475569; font-size: 12px;">{{ $businessName }}</strong><br>
                    @if(!empty($businessAddress))
                        {!! nl2br(e($businessAddress)) !!}<br>
                    @endif
                    @if(!empty($businessPhone))
                        Phone: {{ $businessPhone }}<br>
                    @endif
                    @if(!empty($businessEmail))
                        Email: {{ $businessEmail }}
                    @endif
                </td>
                <td style="text-align: right; width: 50%;">
                    <strong style="color: #475569;">Client Detail:</strong><br>
                    @if($cashIn->client)
                        {{ $cashIn->client->full_name }}<br>
                        @php
                            $addrParts = array_filter([
                                $cashIn->client->address_line1,
                                $cashIn->client->address_line2,
                                $cashIn->client->city,
                                $cashIn->client->state,
                                $cashIn->client->postcode
                            ]);
                            $addressStr = implode(', ', $addrParts);
                        @endphp
                        @if($addressStr)
                            Address: {{ $addressStr }}<br>
                        @endif
                        @if($cashIn->client->country)
                            Country: {{ $cashIn->client->country }}<br>
                        @endif
                        Email: {{ $cashIn->client->email ?? 'N/A' }}<br>
                        Phone: {{ $cashIn->client->phone ?? 'N/A' }}
                    @elseif($cashIn->customAccount)
                        {{ $cashIn->customAccount->name }} (Custom Account)
                    @else
                        Walk-in Client
                    @endif
                </td>
            </tr>
        </table>

        <div style="background-color: #f8fafc; border: 1px solid #e2e8f0; border-radius: 8px; padding: 12px; margin-bottom: 25px;">
            <table style="width: 100%; font-size: 12px;">
                <tr>
                    <td><strong>Transaction ID:</strong> {{ $cashIn->transaction_id ?? 'N/A' }}</td>
                    <td><strong>Payment Date:</strong> {{ $cashIn->date->format('d-m-Y') }}</td>
                </tr>
                <tr>
                    <td><strong>Payment Method:</strong> {{ $cashIn->paymentMethod?->name ?? 'N/A' }}</td>
                    <td><strong>Transaction Type:</strong> {{ $cashIn->paymentType?->name ?? 'N/A' }}</td>
                </tr>
                @if($cashIn->original_currency)
                    <tr>
                        <td><strong>Foreign Amount:</strong> {{ number_format($cashIn->original_amount, 2) }} {{ $cashIn->original_currency }}</td>
                        <td><strong>Exchange Rate:</strong> 1 {{ $cashIn->original_currency }} = {{ number_format($cashIn->exchange_rate, 6) }} {{ $baseCurrencyCode }}</td>
                    </tr>
                @endif
            </table>
        </div>

        <table class="receipt-items">
            <thead>
                <tr>
                    <th style="width: 70%;">Description</th>
                    <th style="width: 30%; text-align: right;">Amount</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        {{ $cashIn->paymentType ? $cashIn->paymentType->name : 'Payment' }}
                    </td>
                    <td style="text-align: right; font-family: monospace; font-size: 13px;">
                        {{ $currencySymbol }}{{ number_format($cashIn->amount, 2) }}
                    </td>
                </tr>
                @if($cashIn->vat_rate)
                <tr>
                    <td style="color: #64748b; font-size: 12px;">Net Amount</td>
                    <td style="text-align: right; font-family: monospace; font-size: 12px; color: #64748b;">
                        {{ $currencySymbol }}{{ number_format($cashIn->net_amount, 2) }}
                    </td>
                </tr>
                <tr>
                    <td style="color: #64748b; font-size: 12px;">VAT ({{ $cashIn->vat_rate }}%)</td>
                    <td style="text-align: right; font-family: monospace; font-size: 12px; color: #64748b;">
                        {{ $currencySymbol }}{{ number_format($cashIn->vat_amount, 2) }}
                    </td>
                </tr>
                @endif
                <tr class="total-row">
                    <td style="text-align: right;">Total Received:</td>
                    <td style="text-align: right; color: #ea580c; font-family: monospace; font-size: 14px;">
                        {{ $currencySymbol }}{{ number_format($cashIn->amount, 2) }}
                    </td>
                </tr>
            </tbody>
        </table>

        @if($cashIn->notes)
            <div style="margin-top: 20px; font-size: 12px; border-top: 1px solid #e2e8f0; padding-top: 10px;">
                <strong>Notes / Details:</strong>
                <p style="color: #64748b; margin-top: 5px; white-space: pre-wrap;">{{ $cashIn->notes }}</p>
            </div>
        @endif

        <div class="footer">
            Thank you for your business. This is a computer generated document.
        </div>
    </div>

</body>
</html>
