<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ $purchaseBill->bill_number }}</title>
    <style>
        html, body {
            margin: 0;
            padding: 0;
            height: 100%;
            background: #525659;
            font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
        }
        .billing-pdf-print__toolbar {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            padding: 0.5rem 0.75rem;
            background: #fff;
            border-bottom: 1px solid #dee2e6;
            box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
        }
        .billing-pdf-print__toolbar .btn {
            font-size: 0.8125rem;
            padding: 0.35rem 0.75rem;
        }
        .billing-pdf-print__title {
            flex: 1;
            font-size: 0.875rem;
            font-weight: 600;
            color: #334155;
        }
        .billing-pdf-print__frame-wrap {
            height: calc(100% - 46px);
        }
        .billing-pdf-print__frame {
            width: 100%;
            height: 100%;
            border: 0;
            display: block;
            background: #525659;
        }
        @media print {
            .billing-pdf-print__toolbar {
                display: none !important;
            }
            html, body {
                background: #fff;
                height: auto;
            }
            .billing-pdf-print__frame-wrap {
                height: auto;
            }
            .billing-pdf-print__frame {
                position: fixed;
                inset: 0;
                width: 100%;
                height: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="billing-pdf-print__toolbar">
        <a href="{{ $backUrl }}" class="btn btn-light btn-sm border">&larr; Back</a>
        <span class="billing-pdf-print__title">Print — {{ $purchaseBill->bill_number }}</span>
        <button type="button" class="btn btn-dark btn-sm" id="btn-print-pdf">Print</button>
        <a href="{{ route('admin.purchase-bills.pdf', ['purchase_bill' => $purchaseBill, 'copy' => $copyKey]) }}" class="btn btn-primary btn-sm">
            Download PDF
        </a>
    </div>
    <div class="billing-pdf-print__frame-wrap">
        <iframe
            id="invoice-pdf-frame"
            class="billing-pdf-print__frame"
            src="{{ $pdfUrl }}"
            title="Purchase Bill {{ $purchaseBill->bill_number }}"
        ></iframe>
    </div>
    <script>
    (function () {
        const frame = document.getElementById('invoice-pdf-frame');
        const btn = document.getElementById('btn-print-pdf');
        let printed = false;

        function triggerPrint() {
            if (printed) return;
            printed = true;
            try {
                if (frame.contentWindow) {
                    frame.contentWindow.focus();
                    frame.contentWindow.print();
                    return;
                }
            } catch (e) { /* cross-origin fallback */ }
            window.print();
        }

        btn.addEventListener('click', function () {
            printed = false;
            triggerPrint();
        });

        window.addEventListener('keydown', function (e) {
            if ((e.ctrlKey || e.metaKey) && e.key === 'p') {
                e.preventDefault();
                printed = false;
                triggerPrint();
            }
        });

        frame.addEventListener('load', function () {
            @if($autoprint)
            setTimeout(triggerPrint, 600);
            @endif
        });
    })();
    </script>
</body>
</html>
