<!doctype html>
<html lang="id">
<head>
    @php
        $businessName = \App\Support\AppSettings::businessName($order->branch_id);
        $businessLogo = \App\Support\AppSettings::logoUrl($order->branch_id);
        $businessAddress = \App\Support\AppSettings::get('business_address', $order->branch?->address, $order->branch_id);
        $receiptFooter = \App\Support\AppSettings::get('receipt_footer', 'Terima kasih.', $order->branch_id);
    @endphp
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Nota {{ $order->invoice_no }}</title>
    <style>
        * { box-sizing: border-box; }
        body { margin: 0; background: #e5e7eb; color: #111827; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace; }
        .toolbar { max-width: 420px; margin: 20px auto 12px; display: flex; gap: 8px; font-family: system-ui, sans-serif; }
        .toolbar a, .toolbar button { flex: 1; border: 1px solid #cbd5e1; border-radius: 6px; background: white; padding: 10px 12px; color: #0f172a; text-align: center; text-decoration: none; cursor: pointer; }
        .receipt { width: 80mm; max-width: calc(100vw - 24px); margin: 0 auto 24px; background: white; padding: 14px; box-shadow: 0 10px 30px rgba(15, 23, 42, .14); }
        .center { text-align: center; }
        .muted { color: #475569; }
        .line { border-top: 1px dashed #64748b; margin: 10px 0; }
        .row { display: flex; justify-content: space-between; gap: 12px; }
        .item { margin: 8px 0; }
        .item-name { font-weight: 700; }
        .small { font-size: 11px; }
        .total { font-size: 15px; font-weight: 800; }
        @media print {
            body { background: white; }
            .toolbar { display: none; }
            .receipt { width: 80mm; max-width: none; margin: 0; padding: 0; box-shadow: none; }
            @page { size: 80mm auto; margin: 5mm; }
        }
    </style>
</head>
<body>
<div class="toolbar">
    <a href="{{ route('orders.show', $order) }}">Detail</a>
    <button onclick="window.print()">Print</button>
    <a href="{{ route('pos.create') }}">POS Baru</a>
</div>

<main class="receipt">
    <div class="center">
        @if ($businessLogo)
            <img src="{{ $businessLogo }}" alt="{{ $businessName }}" style="max-width:120px;max-height:60px;object-fit:contain;margin:0 auto 6px;">
        @endif
        <div style="font-size:16px;font-weight:800;">{{ $businessName }}</div>
        <div class="small muted">{{ $order->branch?->name ?? 'Cabang' }}</div>
        <div class="small muted">{{ $businessAddress }}</div>
        <div class="small muted">{{ $order->branch?->phone }}</div>
    </div>

    <div class="line"></div>
    <div class="small">
        <div class="row"><span>Nota</span><strong>{{ $order->invoice_no }}</strong></div>
        <div class="row"><span>Tanggal</span><span>{{ $order->created_at->format('d/m/Y H:i') }}</span></div>
        <div class="row"><span>Kasir</span><span>{{ $order->cashier?->name ?? '-' }}</span></div>
        <div class="row"><span>Pelanggan</span><span>{{ $order->customer?->name ?? 'Walk-in' }}</span></div>
        <div class="row"><span>Estimasi</span><span>{{ $order->estimated_done_at?->format('d/m/Y H:i') ?? '-' }}</span></div>
    </div>

    <div class="line"></div>
    @foreach ($order->details as $detail)
        <div class="item small">
            <div class="item-name">{{ $detail->product_name }} {{ $detail->is_reward_free_item ? '(Reward)' : '' }}</div>
            <div class="row">
                <span>{{ rtrim(rtrim(number_format((float) $detail->quantity, 3, ',', '.'), '0'), ',') }} x Rp {{ number_format($detail->unit_price, 0, ',', '.') }}</span>
                <span>Rp {{ number_format($detail->line_total, 0, ',', '.') }}</span>
            </div>
            @if ((float) $detail->discount > 0)
                <div class="row muted"><span>Diskon item</span><span>- Rp {{ number_format($detail->discount, 0, ',', '.') }}</span></div>
            @endif
        </div>
    @endforeach

    <div class="line"></div>
    <div class="small">
        <div class="row"><span>Subtotal</span><span>Rp {{ number_format($order->subtotal, 0, ',', '.') }}</span></div>
        <div class="row"><span>Diskon</span><span>- Rp {{ number_format($order->discount_total, 0, ',', '.') }}</span></div>
        <div class="row"><span>Ongkir</span><span>Rp {{ number_format($order->delivery_fee, 0, ',', '.') }}</span></div>
        <div class="row total"><span>Total</span><span>Rp {{ number_format($order->grand_total, 0, ',', '.') }}</span></div>
        <div class="row"><span>Dibayar</span><span>Rp {{ number_format($order->paid_total, 0, ',', '.') }}</span></div>
        <div class="row"><span>Sisa</span><span>Rp {{ number_format($order->balance_due, 0, ',', '.') }}</span></div>
        <div class="row"><span>Status bayar</span><strong>{{ strtoupper($order->payment_status) }}</strong></div>
    </div>

    <div class="line"></div>
    <div class="center small">
        <div>Simpan nota ini untuk tracking.</div>
        <div>{{ route('tracking.index') }}</div>
        <div style="margin-top:8px;">{{ $receiptFooter }}</div>
    </div>
</main>

@if (request()->boolean('auto_print'))
    <script>window.addEventListener('load', () => window.print());</script>
@endif
</body>
</html>
