<script>
    let scanner = new Instascan.Scanner({ video: document.getElementById('scanner'), mirror: false });
        scanner.addListener('scan', function (content) {
        console.log(content);

        if (content != 'null') {
            jQuery('.container-scanner').addClass('hide');
            
            var index = jQuery("#product_selected").val();

            choose_product(content, index);

            scanner.stop();            
        }
    });

    jQuery('body').on('click', '.btn-qr-product', function(es) {
        es.stopPropagation();

        var index = jQuery('.container-product-search').index(jQuery(this));
                
       
        jQuery('#product_selected').val(index);
        Instascan.Camera.getCameras().then(function (cameras) {
            if (cameras.length > 0) {
                if(cameras[1]){ 
                    scanner.start(cameras[1]); 
                } 
                else { 
                    scanner.start(cameras[0]); 
                }
                // scanner.start(cameras[0]);
            } else {
                console.error('No cameras found.');
            }
        }).catch(function (e) {
            console.error(e);
        });         

        jQuery('.container-scanner').removeClass('hide');



    });

    jQuery('body').on('click', '.btn-close-scanner', function() {
        scanner.stop();
        jQuery('.container-scanner').addClass('hide');
    });
