<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=80mm, initial-scale=1.0">
    <title>Nota Gabungan - {{ $orders->count() }} Order</title>
    <style>
        @media print {
            @page {
                size: 80mm auto;
                margin: 0;
            }
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Courier New', monospace;
            font-size: 12px;
            width: 80mm;
            padding: 5mm;
            background: white;
            color: #000;
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }

        .receipt-shell {
            position: relative;
            min-height: 140mm;
        }

        .watermark-demo {
            position: absolute;
            inset: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            pointer-events: none;
            user-select: none;
            z-index: 0;
            overflow: hidden;
        }

        .watermark-demo span {
            display: block;
            transform: rotate(-32deg);
            font-size: 28px;
            line-height: 1;
            font-weight: 900;
            letter-spacing: 4px;
            text-transform: uppercase;
            white-space: nowrap;
            color: rgba(220, 38, 38, 0.10);
        }

        .receipt-content {
            position: relative;
            z-index: 1;
        }

        .header {
            text-align: center;
            margin-bottom: 10px;
        }

        .header h1 {
            font-size: 16px;
            font-weight: bold;
        }

        .header .address {
            font-size: 10px;
            margin-top: 4px;
            color: #666;
        }

        .demo-badge {
            display: inline-block;
            margin-top: 8px;
            padding: 3px 8px;
            border: 1px solid #dc2626;
            background: rgba(220, 38, 38, 0.10);
            color: #b91c1c;
            font-size: 10px;
            font-weight: 800;
            letter-spacing: 1px;
            text-transform: uppercase;
            border-radius: 3px;
        }

        .divider {
            border-bottom: 1px dashed #000;
            margin: 8px 0;
        }

        .divider-double {
            border-bottom: 2px dashed #000;
            margin: 8px 0;
        }

        .demo-warning-box {
            margin: 10px 0 8px 0;
            padding: 8px 7px;
            border: 2px solid #dc2626;
            background: rgba(220, 38, 38, 0.06);
            text-align: center;
        }

        .demo-warning-title {
            font-size: 13px;
            font-weight: 900;
            color: #b91c1c;
            letter-spacing: 1px;
            text-transform: uppercase;
        }

        .demo-warning-subtitle {
            font-size: 10px;
            font-weight: 900;
            color: #b91c1c;
            margin-top: 4px;
            line-height: 1.35;
            text-transform: uppercase;
        }

        .demo-warning-subtitle strong {
            color: #991b1b;
        }

        .summary-info {
            font-size: 11px;
            margin-bottom: 8px;
        }

        .summary-info .row {
            display: flex;
            justify-content: space-between;
            gap: 10px;
            margin-bottom: 2px;
        }

        .summary-info .row span:last-child {
            text-align: right;
        }

        .order-section {
            margin-bottom: 6px;
        }

        .order-header {
            font-weight: bold;
            font-size: 11px;
            background: #eee;
            padding: 2px 4px;
            margin-bottom: 4px;
        }

        .item {
            margin-bottom: 6px;
            padding-bottom: 4px;
            border-bottom: 1px dotted #ccc;
        }

        .item:last-child {
            border-bottom: none;
        }

        .item-name {
            font-weight: bold;
            font-size: 11px;
            word-break: break-word;
        }

        .item-detail {
            font-size: 9px;
            color: #666;
            margin-top: 1px;
            word-break: break-word;
        }

        .item-price {
            display: flex;
            justify-content: space-between;
            font-size: 10px;
            margin-top: 2px;
            gap: 8px;
        }

        .item-price span:last-child {
            text-align: right;
            white-space: nowrap;
        }

        .totals {
            margin-top: 8px;
        }

        .total-row {
            display: flex;
            justify-content: space-between;
            gap: 8px;
            font-size: 11px;
            margin-bottom: 2px;
        }

        .total-row.grand-total {
            font-size: 14px;
            font-weight: bold;
            border-top: 1px solid #000;
            border-bottom: 1px solid #000;
            padding: 4px 0;
            margin-top: 4px;
        }

        .payment-info {
            margin-top: 8px;
        }

        .payment-row {
            display: flex;
            justify-content: space-between;
            gap: 8px;
            font-size: 11px;
            margin-bottom: 2px;
        }

        .payment-row.bold {
            font-weight: bold;
        }

        .payment-row.change {
            font-size: 12px;
            font-weight: bold;
            margin-top: 4px;
            padding-top: 4px;
            border-top: 1px dashed #000;
        }

        .split-box {
            border: 1px dashed #000;
            padding: 6px 5px;
            margin-top: 8px;
            margin-bottom: 8px;
        }

        .split-title {
            font-size: 11px;
            font-weight: bold;
            margin-bottom: 4px;
            text-transform: uppercase;
        }

        .split-row {
            display: flex;
            justify-content: space-between;
            gap: 8px;
            font-size: 10px;
            margin-bottom: 2px;
        }

        .split-row .left {
            flex: 1;
            padding-right: 6px;
        }

        .split-row .right {
            white-space: nowrap;
            text-align: right;
        }

        .status-paid {
            text-align: center;
            font-size: 18px;
            font-weight: bold;
            margin: 10px 0;
            padding: 8px;
            border: 2px solid #000;
        }

        .status-demo {
            text-align: center;
            font-size: 16px;
            font-weight: 900;
            margin: 10px 0;
            padding: 8px;
            border: 2px solid #dc2626;
            color: #b91c1c;
            background: rgba(220, 38, 38, 0.05);
        }

        .demo-legal-note {
            margin-top: 8px;
            padding: 6px 5px;
            border: 1px dashed #dc2626;
            text-align: center;
            font-size: 10px;
            font-weight: 900;
            color: #b91c1c;
            line-height: 1.35;
            text-transform: uppercase;
            background: rgba(220, 38, 38, 0.04);
        }

        .footer {
            text-align: center;
            margin-top: 10px;
            padding-top: 10px;
            border-top: 1px dashed #000;
        }

        .footer-text {
            font-size: 11px;
        }

        .footer-time {
            font-size: 9px;
            color: #666;
            margin-top: 5px;
        }

        @media screen {
            body {
                margin: 20px auto;
                border: 1px solid #ccc;
                box-shadow: 0 0 10px rgba(0,0,0,0.1);
            }
        }
    </style>
</head>
<body onload="window.print()">
    @php
        $totalSemua = 0;

        $paymentMethod = $batchPaymentInfo['payment_method'] ?? 'cash';
        $paymentMethodLabel = $batchPaymentInfo['payment_method_label'] ?? match($paymentMethod) {
            'cash' => 'Tunai',
            'transfer' => 'Transfer Bank',
            'ewallet' => 'E-Wallet',
            'split' => 'Split Payment',
            default => ucfirst($paymentMethod),
        };

        $isSplitPayment = (bool) ($batchPaymentInfo['is_split_payment'] ?? false);
        $splitSummary = collect($batchPaymentInfo['split_summary'] ?? []);
        $splitTotal = (float) ($batchPaymentInfo['split_total'] ?? $splitSummary->sum('amount'));

        $paidAmount = (float) ($batchPaymentInfo['paid_amount'] ?? 0);
        $cashReceived = (float) ($batchPaymentInfo['cash_received'] ?? 0);
        $changeAmount = (float) ($batchPaymentInfo['change_amount'] ?? 0);
    @endphp

    <div class="receipt-shell">
        @if(env('APP_DEMO', false))
            <div class="watermark-demo">
                <span>DEMO MODE</span>
            </div>
        @endif

        <div class="receipt-content">
            <div class="header">
                @if($shopLogo ?? false)
                    <div style="text-align:center; margin-bottom:5px;">
                        <img src="{{ $shopLogo }}" alt="{{ $shopName }}" style="max-height: 35px;">
                    </div>
                @endif

                <h1>{{ $shopName }}</h1>

                @if($shopAddress)
                    <div class="address">{{ $shopAddress }}</div>
                @endif

                @if($shopPhone)
                    <div class="address">{{ $shopPhone }}</div>
                @endif

                @if(env('APP_DEMO', false))
                    <div class="demo-badge">Mode Demo</div>
                @endif
            </div>

            <div class="divider"></div>

            @if(env('APP_DEMO', false))
                <div class="demo-warning-box">
                    <div class="demo-warning-title">Demo Mode</div>
                    <div class="demo-warning-subtitle">
                        Nota gabungan ini adalah <strong>simulasi / percobaan</strong><br>
                        termasuk <strong>transaksi palsu</strong> dan <strong>bukan bukti pembayaran sah</strong>
                    </div>
                </div>
            @endif

            <div class="summary-info">
                <div class="row">
                    <span>Tgl Cetak:</span>
                    <span>{{ now()->format('d/m/Y H:i') }}</span>
                </div>
                <div class="row">
                    <span>Jumlah Order:</span>
                    <span>{{ $orders->count() }}</span>
                </div>
                <div class="row">
                    <span>Pelanggan:</span>
                    <span>{{ $orders->first()->customer_name ?? '-' }}</span>
                </div>
                <div class="row">
                    <span>Metode:</span>
                    <span>{{ $paymentMethodLabel }}</span>
                </div>
            </div>

            <div class="divider-double"></div>

            @foreach($orders as $order)
                @php
                    $totalSemua += (float) $order->total_price;
                @endphp

                <div class="order-section">
                    <div class="order-header">Invoice: {{ $order->invoice_number }}</div>

                    @foreach($order->items as $item)
                        <div class="item">
                            <div class="item-name">{{ $item->product_name }} - {{ $item->file_name }}</div>
                            <div class="item-detail">
                                {{ $item->size_description ?: '-' }} |
                                {{ ($item->sisi ?? 1) }} sisi |
                                {{ $item->material_name ?? '-' }}
                            </div>
                            <div class="item-price">
                                <span>{{ $item->qty }} pcs x Rp {{ number_format($item->calculated_price, 0, ',', '.') }}</span>
                                <span>Rp {{ number_format($item->subtotal, 0, ',', '.') }}</span>
                            </div>
                        </div>
                    @endforeach
                </div>
            @endforeach

            <div class="divider"></div>

            <div class="totals">
                <div class="total-row grand-total">
                    <span>TOTAL ORDER</span>
                    <span>Rp {{ number_format($totalSemua, 0, ',', '.') }}</span>
                </div>
            </div>

            <div class="payment-info">
                @if($isSplitPayment)
                    <div class="split-box">
                        <div class="split-title">Detail Split Payment</div>
                        @foreach($splitSummary as $split)
                            <div class="split-row">
                                <div class="left">
                                    {{ $split['method_label'] ?? '-' }}
                                    @if(!empty($split['account_label']))
                                        <br>{{ $split['account_label'] }}
                                    @endif
                                </div>
                                <div class="right">Rp {{ number_format((float) ($split['amount'] ?? 0), 0, ',', '.') }}</div>
                            </div>
                        @endforeach
                        <div class="split-row" style="margin-top:4px; padding-top:4px; border-top:1px dashed #000; font-weight:bold;">
                            <div class="left">Total Split</div>
                            <div class="right">Rp {{ number_format($splitTotal, 0, ',', '.') }}</div>
                        </div>
                    </div>
                @endif

                <div class="payment-row">
                    <span>Metode Bayar</span>
                    <span>{{ $paymentMethodLabel }}</span>
                </div>

                <div class="payment-row bold">
                    <span>Jumlah Dibayar</span>
                    <span>
                        Rp {{ number_format($paidAmount > 0 ? $paidAmount : ($isSplitPayment ? $splitTotal : $totalSemua), 0, ',', '.') }}
                    </span>
                </div>

                @if(!$isSplitPayment && $paymentMethod === 'cash')
                    <div class="payment-row">
                        <span>Uang Diterima</span>
                        <span>
                            Rp {{ number_format($cashReceived > 0 ? $cashReceived : ($paidAmount > 0 ? $paidAmount : $totalSemua), 0, ',', '.') }}
                        </span>
                    </div>

                    <div class="payment-row change">
                        <span>Kembalian</span>
                        <span>Rp {{ number_format($changeAmount, 0, ',', '.') }}</span>
                    </div>
                @elseif($changeAmount > 0)
                    <div class="payment-row change">
                        <span>Kembalian</span>
                        <span>Rp {{ number_format($changeAmount, 0, ',', '.') }}</span>
                    </div>
                @endif
            </div>

            @if(env('APP_DEMO', false))
                <div class="status-demo">
                    *** DEMO / TIDAK SAH ***
                </div>

                <div class="demo-legal-note">
                    Demo mode aktif · nota gabungan ini tidak valid untuk pembayaran, penagihan,
                    audit, pengambilan barang, klaim transaksi, atau bukti transaksi resmi
                </div>
            @else
                <div class="status-paid">
                    @if($orders->every(fn($o) => $o->isPaid()))
                        *** LUNAS ***
                    @else
                        *** TAGIHAN ***
                    @endif
                </div>
            @endif

            <div class="footer">
                <div class="footer-text">{{ $shopFooter }}</div>
                <div class="footer-time">{{ now()->format('d/m/Y H:i:s') }}</div>
            </div>
        </div>
    </div>
</body>
</html>
