<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Receipt {{ $sale->receipt_number }}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f3f4f6;
            color: #111827;
            margin: 0;
            padding: 24px;
        }
        .receipt {
            background: #fff;
            width: 320px;
            margin: 0 auto;
            padding: 18px;
            border: 1px solid #e5e7eb;
        }
        .center { text-align: center; }
        .muted { color: #6b7280; font-size: 12px; }
        .row {
            display: flex;
            justify-content: space-between;
            gap: 12px;
            font-size: 13px;
            margin: 8px 0;
        }
        .total {
            border-top: 1px dashed #9ca3af;
            border-bottom: 1px dashed #9ca3af;
            padding: 10px 0;
            margin: 12px 0;
            font-weight: 700;
            font-size: 16px;
        }
        .barcode {
            display: flex;
            justify-content: center;
            align-items: stretch;
            height: 64px;
            margin: 14px 0 6px;
            overflow: hidden;
        }
        .bar {
            background: #111827;
            height: 64px;
            margin-right: 1px;
        }
        .actions {
            margin: 18px auto;
            width: 320px;
            text-align: center;
        }
        button {
            border: 0;
            background: #111827;
            color: #fff;
            padding: 10px 14px;
            cursor: pointer;
        }
        @media print {
            body { background: #fff; padding: 0; }
            .receipt { border: 0; margin: 0; width: 72mm; }
            .actions { display: none; }
        }
    </style>
</head>
<body>
    <div class="receipt">
        <div class="center">
            <strong>{{ $siteName }}</strong>
            <div class="muted">POS Receipt</div>
        </div>

        <div class="row"><span>Receipt</span><span>{{ $sale->receipt_number }}</span></div>
        <div class="row"><span>Date</span><span>{{ $sale->sold_at?->format('Y-m-d H:i') }}</span></div>
        <div class="row"><span>POS</span><span>{{ $sale->terminal?->code }}</span></div>
        <div class="row"><span>Agent</span><span>{{ $sale->agent?->name ?? 'System' }}</span></div>
        <div class="row"><span>Customer</span><span>{{ $sale->customer?->username }}</span></div>

        <div class="row total"><span>Amount</span><span>${{ number_format((float) $sale->amount, 2) }}</span></div>

        <div class="row"><span>Tendered</span><span>${{ number_format((float) ($sale->tendered_amount ?? $sale->amount), 2) }}</span></div>
        <div class="row"><span>Change</span><span>${{ number_format((float) $sale->change_amount, 2) }}</span></div>
        <div class="row"><span>Transaction</span><span>{{ $sale->transaction?->reference }}</span></div>

        <div class="barcode" aria-label="Barcode {{ $sale->barcode_value }}">
            @foreach ($sale->barcodeSegments() as $segment)
                <span
                    class="bar"
                    style="width: {{ $segment['width'] }}px; background: {{ $segment['bar'] ? '#111827' : 'transparent' }}"
                ></span>
            @endforeach
        </div>
        <div class="center muted">{{ $sale->barcode_value }}</div>

        <p class="center muted">{{ $footer }}</p>
    </div>

    <div class="actions">
        <button onclick="window.print()">Print Receipt</button>
    </div>
</body>
</html>
