<?php if (isset($component)) { $__componentOriginal1770d22cf5af24e47b162c0db36175e7 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal1770d22cf5af24e47b162c0db36175e7 = $attributes; } ?>
<?php $component = App\View\Components\TechLayout::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('tech-layout'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\App\View\Components\TechLayout::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
    <div x-data="printerSearch()">
        <label for="q" class="form-label fw-medium">Пребарај печатач</label>
        <div class="position-relative mb-4">
            <input id="q" type="search" x-model="q" @input.debounce.250ms="search()"
                   class="form-control form-control-lg"
                   placeholder="Внесете име, модел или ознака…" autocomplete="off">
            <div x-show="open" x-cloak
                 class="list-group position-absolute w-100 shadow-sm"
                 style="z-index:1050; top:100%;">
                <template x-for="p in results" :key="p.id">
                    <a class="list-group-item list-group-item-action" :href="p.url" x-text="p.label"></a>
                </template>
                <div x-show="q.length > 0 && results.length === 0"
                     class="list-group-item text-muted small">Нема резултати</div>
            </div>
        </div>

        <button type="button" class="btn btn-primary btn-lg w-100 d-flex align-items-center justify-content-center gap-2"
                x-show="!scanning" @click="startScan()">
            <i class="bi bi-qr-code-scan fs-4"></i> Скенирај код
        </button>
        <p class="text-muted small text-center mt-2">Скенирајте ја налепницата со камерата на телефонот, или притиснете „Скенирај код".</p>

        <div x-show="scanning" x-cloak class="mt-3">
            <div id="reader" style="width:100%;"></div>
            <p class="text-muted small text-center mt-2" x-text="scanError"></p>
            <button type="button" class="btn btn-light border w-100" @click="stopScan()">Затвори</button>
        </div>
    </div>

    <script>
        function printerSearch() {
            return {
                q: '', results: [], open: false,
                scanning: false, scanError: '', scanner: null,
                async search() {
                    if (this.q.trim() === '') { this.results = []; this.open = false; return; }
                    const res = await fetch(`/printers/search?q=${encodeURIComponent(this.q)}`, {
                        headers: { 'Accept': 'application/json' },
                    });
                    const data = await res.json();
                    this.results = data.results;
                    this.open = true;
                },
                async startScan() {
                    this.scanError = 'Насочете ја камерата кон кодот';
                    this.scanning = true;
                    await this.$nextTick();
                    try {
                        this.scanner = new window.Html5Qrcode('reader');
                        await this.scanner.start(
                            { facingMode: 'environment' },
                            { fps: 10, qrbox: 240 },
                            (decoded) => this.onScan(decoded),
                            () => {}
                        );
                    } catch (e) {
                        this.scanError = 'Не може да се пристапи до камерата';
                    }
                },
                onScan(decoded) {
                    try {
                        const u = new URL(decoded, window.location.origin);
                        if (u.origin === window.location.origin && u.pathname.startsWith('/p/')) {
                            this.stopScan().then(() => { window.location.href = u.pathname; });
                            return;
                        }
                    } catch (e) { /* not a URL */ }
                    this.scanError = 'Непознат код';
                },
                async stopScan() {
                    if (this.scanner) {
                        try { await this.scanner.stop(); } catch (e) { /* already stopped */ }
                        this.scanner = null;
                    }
                    this.scanning = false;
                },
            };
        }
    </script>
 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal1770d22cf5af24e47b162c0db36175e7)): ?>
<?php $attributes = $__attributesOriginal1770d22cf5af24e47b162c0db36175e7; ?>
<?php unset($__attributesOriginal1770d22cf5af24e47b162c0db36175e7); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal1770d22cf5af24e47b162c0db36175e7)): ?>
<?php $component = $__componentOriginal1770d22cf5af24e47b162c0db36175e7; ?>
<?php unset($__componentOriginal1770d22cf5af24e47b162c0db36175e7); ?>
<?php endif; ?>
<?php /**PATH /Users/gego/Desktop/Projects/Sterna/sterna/resources/views/tech/home.blade.php ENDPATH**/ ?>