<div class="row">
    <div class="container">
        <ol class="breadcrumb">
            <li><a href="#">المنتجات</a></li>
            <li class="active">إعادة جرد بالباركود</li>
        </ol>
        <div class="panel panel-default panel-m">
            <div class="panel-heading">
                <h3 class="panel-title">إعادة جرد بالباركود</h3>
                <a href="{$youtubes[62]->url}" target="_blank" class="hidden-print"><img src="../views/default/assets/img/youtube.png" class="pull-left" alt="Youtube" style="width: 2%;"></a>

            </div>
            <div class="panel-body">
                <input type="hidden" name="usedParcode" id="usedParcode" value="{$Programsettingdata->usedParcode}"/>
                <form class="form-horizontal" role="form" action="inventoryController.php?do=add" method="post" >
                    <div class="form-group">
                        <label for="storesData" class="col-lg-2 control-label">المخزن</label>
                        <div class="col-lg-4">
                            <select class="select2 wdt100" name="storeId" id="storeId">
                                <option value=""></option>
                                {foreach $storesData as $store}
                                <option value="{$store->storeId}">{$store->storeName}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <!-- ================================================= -->
                    <button type="button" class="btn btn-primary pull-left ft-nor" onclick="newRow();"> اضافة جديد</button>
                    <table class="table table-black">
                        <thead>
                            <tr>
                                <th style="width: 20%">الباركود</th>
                                <th>إسم المنتج</th>
                                <th>الكمية الحالية</th>
                                <th>الكمية الجديدة</th>
                                <th>تاريخ آخر إعادة جرد</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody id="inv_tbody">

                        </tbody>
                    </table>

                    <input type="hidden" name="itr" id="itr" value="0"/>
                    <input type="hidden" name="Inv_byCode" id="Inv_byCode" value="1"/>
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-4">
                            <button type="submit"  class="btn btn-primary pull-left">حفظ</button>
                        </div>
                    </div>
                </form>
                <input name="storenegative" type="hidden" value="{$storenegative}" id="storenegative" />
            </div>
        </div>
    </div>
</div>

{literal}
<script>
    function getproductInventorybyOnlyParcode(e, value, n)
    {

        var code = (e.keyCode ? e.keyCode : e.whichparcode);
        //to stop the submit of the form
        if (e.which == 13)
        {
            e.preventDefault();
            e.stopPropagation();
            if (value != '') {
                var store = Number($('#storeId').val());
                if (store > 0) {
                    //here get the price of product from ajax
                    //the selected value of drop downlsit
                    var usedParcode = Number($('#usedParcode').val());
                    if (usedParcode == 1) {
                        var theselectedvalue = value.replace("ه", "i");//this replacement is for parcode reader
                        theselectedvalue = theselectedvalue.replace("÷", "i");//this replacement is for parcode reader
                        theselectedvalue = theselectedvalue.replace("/", "i");//this replacement is for parcode reader
                        theselectedvalue = theselectedvalue.replace("I", "i");//this replacement is for parcode reader
                    } else {
                        theselectedvalue = value;
                    }
                    //alert(theselectedvalue.charAt(0));
                    $.ajax({
                        url: 'sellbillAjaxController.php?do=getproductNamebyOnlyParcode&parcode=' + theselectedvalue + '&billnameid=0&inventory=1&store=' + store,
                        type: "get",
                        success: function (data) {

                            if (data != null && data != "")
                            {
                                var response = $.parseJSON(data);
                                if (response.id != 0 && response.text != 0)
                                {
                                    //send value as a string of id and name to the initSelection function
                                    $("#productName" + n).text(response.text);
                                    $("#newQty" + n).focus();
                                    $("#productId" + n).val(response.id);
                                    $("#oldQty" + n).val(response.oldQty);
                                    $("#proOldQty" + n).text(response.oldQty);
                                    if (Number(response.storedetailid) > 0) {
                                        $("#storedetailid" + n).val(response.storedetailid);
                                    } else {
                                        $("#storedetailid" + n).val(response.sizecolorstoredetailid);
                                    }
                                    $("#storeid" + n).val(store);
                                    if (!response.lastInventoryDate) {
                                        response.lastInventoryDate = '';
                                    }
                                    $("#lastInventoryDate" + n).text(response.lastInventoryDate);


                                    if (response.inv_status == 0) {
                                        $('#newQty' + n).parent().parent().removeClass('success');
                                    } else {
                                        $('#newQty' + n).parent().parent().addClass('success');
                                    }

                                    newRow();
                                } else
                                {
                                    var parcodeValue = $("#parcode" + n).val();
                                    $("#parcode" + n).val("");//empty parcode input
                                    $("#productName" + n).text("");
                                    $("#productId" + n).val(0);
                                    $("#oldQty" + n).val(0);
                                    $("#proOldQty" + n).text("");
                                    if (Number(response.storedetailid) > 0) {
                                        $("#storedetailid" + n).val(0);
                                    } else {
                                        $("#storedetailid" + n).val(0);
                                    }
                                    $("#lastInventoryDate" + n).text("");
                                    $("#newQty" + n).val("");

                                    $('#newQty' + n).parent().parent().removeClass('success');

                                    alert("رقم الباركود: " + parcodeValue + " غير موجود، تأكد من إدخال رقم صحيح");
                                }
                            }
                        },
                        error: function () {
                            alert("لقد حدث خطأ أثناء إجراء العملية");
                        }
                    });
                } else {
                    alert('عفوا لم تختر مخزن لبدء الجرد')
                }

            }
        }
    }

    $('.delthis').live('click', function () {
        $(this).parent().parent().remove();
    });

    function newRow() {
        var itr = Number($('#itr').val());
        itr++;
        var row = '<tr>' +
                '        <td>' +
                '            <input type="text" name="parcode' + itr + '" id="parcode' + itr + '" class="form-control" onkeypress="getproductInventorybyOnlyParcode(event, this.value, ' + itr + ');">' +
                '        </td>' +
                '        <td>' +
                '            <label id="productName' + itr + '"></label>' +
                '            <input type="hidden" name="productId' + itr + '" id="productId' + itr + '" value=""/>' +
                '            <input type="hidden" name="oldQty' + itr + '" id="oldQty' + itr + '" value=""/>' +
                '            <input name="storedetailid' + itr + '" id="storedetailid' + itr + '" type="hidden" value="">' +
                '            <input name="storeid' + itr + '" id="storeid' + itr + '" type="hidden" value="">' +
                '        </td>' +
                '        <td>' +
                '            <label id="proOldQty' + itr + '"></label>' +
                '        </td>' +
                '        <td>' +
                '            <input name="newQty' + itr + '" id="newQty' + itr + '" type="text"> <!-- onkeyup="checkdata(' + itr + ');" -->' +
                '        </td>' +
                '        <td>' +
                '            <label id="lastInventoryDate' + itr + '"></label>' +
                '        </td>' +
                '        <td>' +
                '            <button type="button" class="close flt-none removeicon delthis" aria-hidden="true">×</button>' +
                '        </td>' +
                '    </tr>';
        $('#inv_tbody').append(row);
        $('#itr').val(itr);
    }

    newRow();

    setTimeout(function () {
        $('#parcode1').focus();
    }, 100)

    $(document).on("keydown", 'input[id^="parcode"]', function(e) {

        // اختيار كل عناصر الباركود
        let barcodes = $('input[id^="parcode"]');
        let index = barcodes.index(this);
    
        // Shift + Tab → رجوع للخلف
        if (e.key === "Tab" && e.shiftKey) {
            e.preventDefault();
            if (barcodes.eq(index - 1).length) {
                barcodes.eq(index - 1).focus();
            }
        }
    
        // Tab فقط → تقدم للأمام
        else if (e.key === "Tab") {
            e.preventDefault();
            if (barcodes.eq(index + 1).length) {
                barcodes.eq(index + 1).focus();
            }
        }
    });
</script>
{/literal}