<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Receipt - {{ $order->invoice_number }}</title>
    <style>
        body { font-family: monospace; font-size: 12px; margin: 20px; }
        .text-center { text-align: center; }
        .text-right { text-align: right; }
        .border-bottom { border-bottom: 1px dashed #000; padding-bottom: 10px; margin-bottom: 10px; }
        table { width: 100%; }
        .total { font-weight: bold; font-size: 14px; }
    </style>
</head>
<body>
    <div class="text-center">
        <h2>POS SYSTEM</h2>
        <p>123 Business Street<br>City, State 12345<br>Tel: (555) 123-4567</p>
    </div>
    
    <div class="border-bottom">
        <p><strong>Invoice #:</strong> {{ $order->invoice_number }}<br>
        <strong>Date:</strong> {{ $order->client_time ? \Carbon\Carbon::parse($order->client_time)->format('M d, Y H:i') : $order->created_at->format('M d, Y H:i') }}<br>
        <strong>Cashier:</strong> {{ $order->user->name }}</p>
        @if($order->customer)
        <p><strong>Customer:</strong> {{ $order->customer->name }}</p>
        @endif
    </div>

    <table>
        <thead>
            <tr>
                <th>Item</th>
                <th class="text-right">Qty</th>
                <th class="text-right">Price</th>
                <th class="text-right">Total</th>
            </tr>
        </thead>
        <tbody>
            @foreach($order->items as $item)
            <tr>
                <td>{{ $item->product_name }}</td>
                <td class="text-right">{{ $item->quantity }}</td>
                <td class="text-right">{{ number_format($item->price, 2) }}</td>
                <td class="text-right">{{ number_format($item->subtotal, 2) }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>

    <div class="border-bottom">
        <table>
            <tr>
                <td>Subtotal:</td>
                <td class="text-right">{{ number_format($order->subtotal, 2) }}</td>
            </tr>
            @if($order->discount > 0)
            <tr>
                <td>Discount:</td>
                <td class="text-right">-{{ number_format($order->discount, 2) }}</td>
            </tr>
            @endif
            @if($order->tax > 0)
            <tr>
                <td>Tax ({{ $order->tax_rate }}%):</td>
                <td class="text-right">{{ number_format($order->tax, 2) }}</td>
            </tr>
            @endif
            <tr class="total">
                <td>TOTAL:</td>
                <td class="text-right">{{ number_format($order->total, 2) }}</td>
            </tr>
        </table>
    </div>

    <div>
        <p><strong>Payment:</strong> 
            @if($order->payment_methods)
                @foreach(json_decode($order->payment_methods, true) as $pm)
                    {{ ucfirst($pm['method']) }}: Kes {{ number_format($pm['amount'], 2) }}{{ !$loop->last ? ', ' : '' }}
                @endforeach
            @else
                {{ ucfirst($order->payment_method) }}
            @endif
        </p>
        <p><strong>Paid:</strong> {{ number_format($order->paid_amount, 2) }}</p>
        <p><strong>Change:</strong> {{ number_format($order->change_amount, 2) }}</p>
    </div>

    <div class="text-center" style="margin-top: 20px;">
        <p>Thank you for your purchase!</p>
        <p>Please come again</p>
    </div>

    <script>
        window.print();
    </script>
</body>
</html>
