<?php
/**
 * Landofcoder
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Landofcoder.com license that is
 * available through the world-wide-web at this URL:
 * https://landofcoder.com/terms
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category   Landofcoder
 * @package    Lof_Formbuilder
 * @copyright  Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
 * @license    https://landofcoder.com/terms
 */

$helper = $this->helper(Lof\Formbuilder\Helper\Data::class);
$enabled_barcode = $helper->getConfig("message_setting/enabled_barcode");
$enableButton = false;
$moduleId = rand().time();
?>
<div class="barcode-reader-form row">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <label for="barcode-code<?= $moduleId; ?>"><?= __("Scan Barcode with External Device or Manually Input Tracking Code");?></label>
        <input type="text" class="form-input" name="barcode-code" id="barcode-code<?= $moduleId; ?>" value=""/>
    </div>
    <?php if($enableButton): ?>
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pt-2 mt-2" style="margin-top:10px">
        <button class="button btn btn-primary button-check-barcode"><?= __("Check Code");?></button>
    </div>
    <?php endif; ?>
</div>
<script type="text/javascript">
    function onScanSuccess(decodedResult) {
        console.log(`Code scanned = `, decodedResult);
        checkBarcode(decodedResult)
    }
    function onEnterForm(data){
        const inputValue = document.getElementById("barcode-code<?= $moduleId; ?>").value;
        if(inputValue){
            checkBarcode(inputValue)
        }
    }
    function checkBarcode(barcode){
        if(barcode){
            setTimeout(function() {
                window.location.href = "<?= $block->getUrl("formbuilder/view");?>"+barcode+"?back=5";
            }, 500);
        }
    }
    function loadScriptByURL(id, url, callback) {
        const isScriptExist = document.getElementById(id);

        if (!isScriptExist) {
            const script = document.createElement("script");
            script.type = "text/javascript";
            script.src = url;
            script.id = id;
            script.attributes = 'async defer';
            script.onload = function () {
            if (callback) callback();
            };
            document.body.appendChild(script);
        }

        if (isScriptExist && callback) callback();
    }
    require([
        'jquery',
        'Lof_Formbuilder/js/barcode-reader'
    ], function ($, BarcodeScanner) {
        const barcodeScanner = new BarcodeScanner("barcode-code<?= $moduleId; ?>");
        barcodeScanner.addEventListener('scan', (e) => {
            onScanSuccess(e.data);
        });
        barcodeScanner.addEventListener('doEnter', (e) => {
            onEnterForm(e.data);
        });

        $(".button-check-barcode").on("click", function(){
            const barcode = $("#barcode-code<?= $moduleId; ?>").val();
            checkBarcode(barcode);
        })
    })
</script>
