<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Faktur {{ $order->invoice_number }}</title>
    <style>
        @media print {
            @page {
                size: A5 portrait;
                margin: 10mm;
            }
        }

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

        body {
            font-family: Arial, sans-serif;
            font-size: 11px;
            line-height: 1.4;
            background: white;
            padding: 20px;
            color: #222;
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }

        .invoice-shell {
            position: relative;
            min-height: 180mm;
        }

        .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(-30deg);
            font-size: 58px;
            line-height: 1;
            font-weight: 900;
            letter-spacing: 8px;
            text-transform: uppercase;
            white-space: nowrap;
            color: rgba(220, 38, 38, 0.08);
        }

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

        .header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #333;
        }

        .company-info h1 {
            font-size: 20px;
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
        }

        .company-info p {
            font-size: 10px;
            color: #666;
            margin-bottom: 2px;
        }

        .invoice-title {
            text-align: right;
        }

        .invoice-title h2 {
            font-size: 24px;
            font-weight: bold;
            color: #333;
        }

        .invoice-title .invoice-number {
            font-size: 14px;
            font-weight: bold;
            margin-top: 5px;
        }

        .demo-badge {
            display: inline-block;
            margin-top: 8px;
            padding: 4px 10px;
            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;
        }

        .demo-warning-box {
            margin-bottom: 18px;
            padding: 12px 14px;
            border: 2px solid #dc2626;
            background: rgba(220, 38, 38, 0.06);
        }

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

        .demo-warning-text {
            font-size: 11px;
            font-weight: 700;
            color: #991b1b;
            line-height: 1.45;
        }

        .info-section {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            gap: 16px;
        }

        .info-block {
            width: 48%;
        }

        .info-block h3 {
            font-size: 11px;
            font-weight: bold;
            text-transform: uppercase;
            color: #666;
            margin-bottom: 5px;
            padding-bottom: 3px;
            border-bottom: 1px solid #ddd;
        }

        .info-block p {
            margin-bottom: 3px;
        }

        .info-block .label {
            color: #666;
            display: inline-block;
            width: 70px;
        }

        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            background: transparent;
        }

        .items-table th {
            background: #f5f5f5;
            border: 1px solid #ddd;
            padding: 6px;
            text-align: left;
            font-size: 9px;
            text-transform: uppercase;
        }

        .items-table td {
            border: 1px solid #ddd;
            padding: 6px;
            font-size: 10px;
            vertical-align: top;
        }

        .items-table .text-right {
            text-align: right;
        }

        .items-table .text-center {
            text-align: center;
        }

        .total-section {
            display: flex;
            justify-content: flex-end;
            margin-bottom: 20px;
        }

        .total-table {
            width: 220px;
        }

        .total-table tr td {
            padding: 5px 0;
        }

        .total-table .label {
            text-align: left;
        }

        .total-table .value {
            text-align: right;
            font-weight: bold;
        }

        .total-table .grand-total td {
            border-top: 2px solid #333;
            font-size: 14px;
            padding-top: 8px;
        }

        .payment-info {
            background: #f9f9f9;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
        }

        .payment-info h3 {
            font-size: 11px;
            font-weight: bold;
            margin-bottom: 8px;
        }

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

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

        .status-paid {
            display: inline-block;
            background: #28a745;
            color: white;
            padding: 5px 15px;
            border-radius: 3px;
            font-weight: bold;
            margin-top: 10px;
        }

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

        .signature-section {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
        }

        .signature-block {
            width: 45%;
            text-align: center;
        }

        .signature-block .title {
            font-size: 10px;
            color: #666;
            margin-bottom: 40px;
        }

        .signature-block .line {
            border-top: 1px solid #333;
            padding-top: 5px;
        }

        .footer {
            margin-top: 20px;
            padding-top: 10px;
            border-top: 1px solid #ddd;
            text-align: center;
            font-size: 9px;
            color: #666;
        }

        @media screen {
            body {
                max-width: 148mm;
                margin: 20px auto;
                border: 1px solid #ccc;
                box-shadow: 0 0 10px rgba(0,0,0,0.1);
            }
        }
    </style>
</head>
<body onload="window.print()">
    <div class="invoice-shell">
        @if(env('APP_DEMO', false))
            <div class="watermark-demo">
                <span>DEMO MODE</span>
            </div>
        @endif

        <div class="invoice-content">
            <div class="header">
                <div class="company-info">
                    @if($shopLogo ?? false)
                        <img src="{{ $shopLogo }}" alt="{{ $shopName }}" style="max-height: 40px; margin-bottom: 8px;">
                    @endif
                    <h1>{{ $shopName }}</h1>
                    @if($shopAddress)
                        <p>{{ $shopAddress }}</p>
                    @endif
                    @if($shopPhone)
                        <p>Telp: {{ $shopPhone }}</p>
                    @endif
                </div>

                <div class="invoice-title">
                    <h2>FAKTUR</h2>
                    <div class="invoice-number">{{ $order->invoice_number }}</div>

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

            @if(env('APP_DEMO', false))
                <div class="demo-warning-box">
                    <div class="demo-warning-title">Dokumen Simulasi / Percobaan</div>
                    <div class="demo-warning-text">
                        Faktur ini dibuat dalam <strong>DEMO MODE</strong>. Seluruh isi dokumen ini termasuk
                        <strong>transaksi palsu, simulasi, atau percobaan sistem</strong> dan
                        <strong>bukan bukti pembayaran sah</strong>, bukan bukti penagihan resmi,
                        bukan dokumen audit, dan tidak boleh digunakan untuk pengambilan barang atau klaim transaksi.
                    </div>
                </div>
            @endif

            <div class="info-section">
                <div class="info-block">
                    <h3>Tagihan Kepada</h3>
                    <p><strong>{{ $order->customer_name }}</strong></p>
                    @if($order->customer_phone)
                        <p>Telp: {{ $order->customer_phone }}</p>
                    @endif
                </div>

                <div class="info-block">
                    <h3>Detail Faktur</h3>
                    <p><span class="label">Tanggal:</span> {{ $order->paid_at->format('d M Y') }}</p>
                    <p><span class="label">Designer:</span> {{ $order->designer->name ?? '-' }}</p>
                    <p><span class="label">Kasir:</span> {{ $order->cashier->name ?? '-' }}</p>
                </div>
            </div>

            <table class="items-table">
                <thead>
                    <tr>
                        <th style="width: 5%">No</th>
                        <th style="width: 30%">Deskripsi</th>
                        <th class="text-center" style="width: 13%">Ukuran</th>
                        <th class="text-center" style="width: 7%">Sisi</th>
                        <th class="text-center" style="width: 7%">Qty</th>
                        <th class="text-right" style="width: 18%">Harga</th>
                        <th class="text-right" style="width: 20%">Subtotal</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($order->items as $index => $item)
                        <tr>
                            <td class="text-center">{{ $index + 1 }}</td>
                            <td>
                                <strong>{{ $item->product_name }}</strong>
                                <br><small>{{ $item->file_name }}</small>
                                @if($item->material_name)
                                    <br><small>Bahan: {{ $item->material_name }}</small>
                                @endif
                                @if($item->finishing_note)
                                    <br><small>Finishing: {{ $item->finishing_note }}</small>
                                @endif
                            </td>
                            <td class="text-center">{{ $item->size_description }}</td>
                            <td class="text-center">{{ $item->sisi ?? 1 }}</td>
                            <td class="text-center">{{ $item->qty }}</td>
                            <td class="text-right">Rp {{ number_format($item->calculated_price, 0, ',', '.') }}</td>
                            <td class="text-right">Rp {{ number_format($item->subtotal, 0, ',', '.') }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>

            <div class="total-section">
                <table class="total-table">
                    <tr>
                        <td class="label">Subtotal ({{ $order->items->count() }} item)</td>
                        <td class="value">Rp {{ number_format($order->total_price, 0, ',', '.') }}</td>
                    </tr>
                    <tr class="grand-total">
                        <td class="label">TOTAL</td>
                        <td class="value">Rp {{ number_format($order->total_price, 0, ',', '.') }}</td>
                    </tr>
                </table>
            </div>

            <div class="payment-info">
                <h3>Informasi Pembayaran</h3>
                <div class="row">
                    <span>Dibayar:</span>
                    <span>Rp {{ number_format($order->paid_amount, 0, ',', '.') }}</span>
                </div>
                <div class="row">
                    <span>Kembalian:</span>
                    <span>Rp {{ number_format($order->change_amount, 0, ',', '.') }}</span>
                </div>
                <div class="status-paid">LUNAS</div>

                @if(env('APP_DEMO', false))
                    <div class="demo-legal-note">
                        Demo mode aktif. Dokumen ini tidak sah untuk pembayaran, penagihan,
                        audit, pengambilan barang, klaim transaksi, atau bukti transaksi resmi.
                    </div>
                @endif
            </div>

            <div class="signature-section">
                <div class="signature-block">
                    <div class="title">Kasir</div>
                    <div class="line">{{ $order->cashier->name ?? '-' }}</div>
                </div>

                <div class="signature-block">
                    <div class="title">Penerima</div>
                    <div class="line">(................................)</div>
                </div>
            </div>

            <div class="footer">
                Dicetak pada {{ now()->format('d M Y H:i:s') }}
            </div>
        </div>
    </div>
</body>
</html>