<script>
	var page_type = "<?php echo $type;?>";

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

        if (content != 'null') {
            jQuery('.container-scanner').addClass('hide');
            
            jQuery("#inp_code").val(content);
            if (page_type == 'pickup') {
                get_detail(content);
            }
            else {
                get_data();
            }

            scanner.stop();            
        }
    });

    jQuery('body').on('click', '#btn_scan', function(es) {
        es.stopPropagation();

        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');
    });