
<div class="row">
    <div class="container">
        <ol class="breadcrumb">
            <li class="active">التحقق من المنتجات</li>
        </ol>
        <div class="panel panel-default panel-m">
            <div class="panel-body">
                <!---------  end outputString alert ------------>
                <!-- <form class="form-inline hidden-print" action="productController.php?do=show" method="post"> -->
                    <div class="form-group">
                        <div class="col-md-2">
                            <label class="sr-only">الباركود</label>
                        </div>
                        <input type="text" name="parcode" id="parcode" class="form-control"
                               onkeypress="getproductbyParcode(event, this.value);">
                    </div>
                <form class="form-inline hidden-print" action="productController.php?do=addtoStore" method="post">
                    <div class="form-group">
                        <div class="col-md-2">
                            <label class="sr-only">المنتج</label>
                        </div>
                        <input type="hidden" name="searchvalue" id="searchvalue"
                               value="{$Programsettingdata->searchvalue}" />
                        <input type="hidden" id="product" name="product" class="select2product">

                        <button name="saveSupPayBtn" type="submit" class="btn btn-primary ">اضافة لكل المخازن</button>
                    </div>
                </form>
                <br />
            </div>
        </div>
    </div>

    {literal}
    <script>
        $(function () {
            setTimeout(function () {
               $('#parcode').focus();
            }, 100)
            
            $(".select2product").select2({
                placeholder: "المنتج",
                minimumInputLength: document.getElementById("searchvalue").value,
                multiple: false,
                quietMillis: 100,
                containerCssClass: "wdt300",
                ajax: {
                    url: "sellbillAjaxController.php?do=getProducts2",
                    dataType: 'json',
                    data: function (term, page) {
                        //alert("sellbillAjaxController.php?do=getProducts&term="+term+"&page_limit=10&storeid="+document.getElementById("storeid").value);
                        return {
                            term: term, //search term
                            page_limit: 10, // page size,
                        };
                    },
                    results: function (data) {

                        return {results: data};
                    }
                },
                formatResult: function (data) {
                    return data.text;
                    //            if (Number(data.id) == -2) {
                    //                return  '<span class="newoption">' + data.text + '</span >';
                    //            } else {
                    //                return  '<span>' + data.text + '</span >';
                    //            }
                },
                initSelection: function (element, callback) {
                    var item = element.val().split(':');
                    var data = {id: item[0], text: item[1], productquantity: item[2], price: item[3]};
                    callback(data);
                }

            });
        });
        function getproductbyParcode(e, value) {
            $("#product").select2('data', '');
            //\s means "one space", and \s+ means "one or more spaces".
            value = value.replace(/\s+/g, '');//\s is the regex for "whitespace", and g is the "global" flag, meaning match ALL \s (whitespaces).
            var code = (e.keyCode ? e.keyCode : e.which);
            //to stop the submit of the form
            if (e.which == 13 && value != "") {
                var billnameid = 3;
                value = value.replace("ه", "i");//this replacement is for parcode reader
                value = value.replace("÷", "i");//this replacement is for parcode reader
                value = value.replace("/", "i");//this replacement is for parcode reader
                value = value.replace("I", "i");//this replacement is for parcode reader
                //alert(value.charAt(0));
                $.ajax({
                    url: 'sellbillAjaxController.php?do=getproductNamebyOnlyParcode&parcode=' + value + '&billnameid=' + billnameid + '',
                    success: function (data) {
                        var response = $.parseJSON(data);
                        $("#product").select2('data', {id: response.id, text: response.text});
                    }
                });
                // }
            }
        }
    </script>
    {/literal}