@php
// This partial returns only the thermal receipt HTML used by the print iframe.
@endphp

<style>
    /* Reduce margins for printing receipts and improve wrapping for long lines and non-latin text */
    @page {
        size: auto;
        /* top right bottom left */
        margin: 0.5cm 0.2cm 0.1cm 0.2cm;
    }

    /* Use a compact font and allow breaking inside words so long product names or Sinhala text don't get truncated */
    @media print {
        html, body {
            margin: 0; /* remove default page margins */
            padding: 0;
            font-family: 'Noto Sans Sinhala', 'Noto Sans', Arial, sans-serif;
            font-size: 13px; /* increased from 11px to 13px for better readability */
            line-height: 1.15;
            color: #000 !important;
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
            -webkit-text-size-adjust: none;
        }

        .thermal-print {
            margin: 0;
            padding: 0;
            width: 100%;
            box-sizing: border-box;
            color: #000 !important;
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }

        .thermal-table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed; /* allow cells to wrap within their width */
            font-size: 14px; /* slightly larger for better readability */
        }


        .thermal-table th,
        .thermal-table td {
            padding: 0 4px;
            white-space: normal !important;
            word-break: break-word !important;
            overflow-wrap: anywhere !important;
        }

        /* Add extra space between items (after each product's second row) */
        .thermal-table tbody tr + tr {
            /* Only apply margin after the second row of each item */
        }
        .thermal-table tbody tr:nth-child(2n) {
            /* Add bottom margin after every second row (item details row) */
            height: 18px;
        }

        /* Make product name rows able to wrap on small paper */
        .thermal-table tbody tr:first-child td {
            font-size: 14px; /* product name rows slightly larger */
        }

        /* Ensure footer doesn't add extra bottom space */
        .thermal-footer, .thermal-note, .thermal-thankyou {
            margin-bottom: 0 !important;
            padding-bottom: 0 !important;
        }

        /* Slightly larger thank-you line for readability */
        .thermal-thankyou {
            font-size: 15px; /* increased from 13px to 15px */
            font-weight: 600;
            margin-top: 2px;
        }
    }

    /* Also apply on-screen for better preview */
    html, body {
        font-family: 'Noto Sans Sinhala', 'Noto Sans', Arial, sans-serif;
    }

    /* Larger thank-you text on screen preview */
    .thermal-thankyou {
        font-size: 15px; /* increased from 13px to 15px */
        font-weight: 600;
    }
</style>

<div class="thermal-print">
    <div class="thermal-header">
    <div class="company-name" style="font-weight:700; font-size:18px; text-align:center;">Perera and Sons Enterprises</div>
    <div class="company-contact" style="font-size:14px; text-align:center; margin-bottom:2px;">Tel-074 1519 244 | 077 7138 071</div>
    <div class="company-address" style="font-size:14px; text-align:center; margin-bottom:4px; display:none;">89/A, Nattandiya Road,Dankotuwa.</div>
    <div class="thermal-title" style="font-size:15px; font-weight:700;">INVOICE RECEIPT</div>
        <div style="display:flex; justify-content:space-between; width:100%;">
            <div class="thermal-info" style="font-size:13px;">Invoice #: {{ $invoice->invoice_number }}</div>
            <div class="thermal-info" style="font-size:13px;">Date: {{ $invoice->date_time && $invoice->date_time instanceof \Carbon\Carbon ? $invoice->date_time->format('Y-m-d H:i') : ($invoice->date_time ?: '-') }}</div>
        </div>
        <div class="thermal-info" style="font-size:13px;">Customer: {{ $invoice->customer->customer_name ?? '-' }}</div>
        @if($invoice->customer)
        <div class="thermal-info" style="font-size:13px;">Customer Total Balance: Rs. {{ number_format($invoice->customer->balance_amount ?? 0, 2) }}</div>
        @endif
        <div class="thermal-info" style="font-size:13px;">Sales Rep: {{ optional(auth()->user())->name ?? optional(auth()->user())->username ?? '-' }}</div>
        <div class="thermal-separator">-----------------------------------------------------------------------------</div>
    </div>

    <table class="thermal-table">
        <thead>
            <tr>
                <th style="width: 18%;">Code</th>
                <th class="qty" style="width:10%">Qty</th>
                <th class="market-price" style="width:20%; display:none">Mkt Price</th>
                <th class="price" style="width:20%">Price</th>
                <th class="total" style="width:27%">Total</th>
            </tr>
        </thead>
        <tbody>
            @foreach($invoice->items as $it)
            {{-- First row: full product name (wraps if long) --}}
            <tr>
                @php $w = $it->warranty ?? optional($it->product)->warranty_days ?? 0; @endphp
                <td colspan="5" style="white-space:normal; word-break:break-word; font-size:13px;">
                        {{ $it->product->product_name ?? 'N/A' }}
                        @if($w && intval($w) > 0)
                            <div style="font-size:12px; color:#555; margin-top:4px;">Warranty: {{ intval($w) }} Months</div>
                        @endif
                </td>
            </tr>
            {{-- Second row: numeric columns aligned under their headers --}}
            <tr>
                {{-- Show product code in first column and qty in second column on the second line --}}
                <td style="text-align:left; font-size:13px; width:18%">{{ $it->product->product_code ?? '' }}</td>
                <td class="qty" style="text-align:center; width:10%; font-size:13px;">{{ $it->qty }}</td>
                <td class="market-price" style="text-align:right; width:20%; display:none; font-size:13px;">{{ number_format($it->market_price, 2) }}</td>
                <td class="price" style="text-align:right; width:20%; font-size:13px;">{{ number_format($it->selling_price, 2) }}</td>
                <td class="total" style="text-align:right; width:27%; font-size:13px;">{{ number_format($it->total_value, 2) }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>

    <div class="thermal-separator">-----------------------------------------------------------------------------</div>

    <div class="thermal-payments-summary">
        @php
            $subtotal = 0;
            $totalDiscount = 0;
            foreach($invoice->items as $item) {
                $marketPrice = $item->market_price ?? $item->selling_price ?? 0;
                $sellingPrice = $item->selling_price ?? 0;
                $quantity = $item->qty ?? 0;

                $subtotal += $marketPrice * $quantity;
                $totalDiscount += ($marketPrice - $sellingPrice) * $quantity;
            }
            $netTotal = $subtotal - $totalDiscount;
        @endphp
        <div class="thermal-payment-row" style="display:flex; justify-content:space-between; display:none">
            <span>Sub Total:</span>
            <span>Rs. {{ number_format($subtotal, 2) }}</span>
        </div>
        @if($totalDiscount > 0)
        <div class="thermal-payment-row" style="display:flex; justify-content:space-between; font-size:15px; font-weight:700;">
            <span>Discount:</span>
            <span>-Rs. {{ number_format($totalDiscount, 2) }}</span>
        </div>
         <div class="thermal-separator">-----------------------------------------------------------------------------</div>
        @endif
        <div class="thermal-total" style="display:flex; justify-content:space-between; font-weight:700; font-size:16px;">
            <span>NET TOTAL:</span>
            <span>Rs. {{ number_format($netTotal, 2) }}</span>
        </div>
        @php
            $paid = $invoice->down_payment ?? 0;
            $balance = $paid - $netTotal;
        @endphp
    </div>

    

    <div class="thermal-payments">
        @if($invoice->payment_type === 'cash' && $invoice->down_payment > 0)
        <div class="thermal-payment-row" style="font-size:14px;">
            <span>Cash Paid:</span>
            <span>Rs. {{ number_format($invoice->down_payment, 2) }}</span>
        </div>
        @endif

        @if($balance != 0)
        <div class="thermal-payment-row" style="display:flex; justify-content:space-between; display:none; font-size:14px;">
            <span>Balance {{ $balance < 0 ? 'Due' : 'Change' }}:</span>
            <span>Rs. {{ number_format(abs($balance), 2) }}</span>
        </div>
        @endif
    </div>

   

    <div class="thermal-footer" style="text-align:center;">
        <div class="thermal-note" style="font-size:12px; margin-bottom:4px; display:none">Open 9.000am-8.00pm</div>
        <div class="thermal-thankyou">Thank you for your business!</div>
        
        <!-- Stamp and Signature Section -->
        <div style="margin-top: 15px; margin-bottom: 10px; padding: 0 5px;">
            <!-- Stamp Space - Full Width -->
            <div style="text-align: center; margin-bottom: 3px;">
                <div style="height: 1in; border: 1px dashed #666; display: flex; align-items: center; justify-content: center; margin-bottom: 3px;">
                    <span style="font-size: 11px; color: #999;">STAMP</span>
                </div>
                <div style="font-size: 11px; font-weight: 600; border-top: 1px solid #000; padding-top: 2px; margin-bottom: 3px;">Company Stamp</div>
            </div>
            
            <!-- Customer Signature Space - Full Width -->
            <div style="text-align: center;">
                <div style="height: 1in; border-bottom: 1px solid #000; display: flex; align-items: flex-end; padding-bottom: 5px; margin-bottom: 3px;">
                    <span style="font-size: 10px; color: #ccc; width: 100%;"></span>
                </div>
                <div style="font-size: 11px; font-weight: 600;">Customer Signature</div>
            </div>
        </div>

        <div class="thermal-info" style="font-size:10px; display:none">Software by Harshana Systems-0775897772</div>
    </div>
</div>