jQuery(document).ready(function () {

    var formatResult = function (data) {
        //alert(data);
        return data.text;
    }


    $('.select2product').select2({
        placeholder: "إسم المنتج",
        minimumInputLength: $('#searchvalue').val(),
        multiple: false,
        quietMillis: 100,
        ajax: {
            url: "buyBillControllerAjax.php?do=getProductsnoservice",
            dataType: 'json',
            data: function (term, page) {
                return {
                    term: term, //search term
                    page_limit: 10 // page size
                };
            },
            results: function (data) {
                //alert("data: "+data);
                return { results: data };
            }
        },
        formatResult: formatResult,
        initSelection: function (element, callback) {
            var item = element.val().split(':');
            var data = { id: item[0], text: item[1] };
            callback(data);
        }
    });



    // here the frist action on frist dropdownlist
    /************************/


    /**********************/
    //action of store dropdownlist
    /**********************/
    /*$("#storeId").on("change", function (e) {

     var theselectedvalue = $(this).val();
     var level = Number($("#level").val());
     var productCatId = Number($("#productCatId" + level).val());
     if (isNaN(productCatId) || productCatId < 1) {
     var productCatId = Number($("#productCatId" + (level - 1)).val());
     }

     $("#productsDiv").html('<img src="../views/default/images/loader/6.gif" alt="">');
     $('#productsDiv').load('inventoryControllerAjax.php?storeid=' + theselectedvalue + '&catid=' + productCatId + '&productid=' + $("#product").val() + '', function () {

     $("#product").select2({
     placeholder: "إسم المنتج",
     minimumInputLength: 0,
     multiple: false,
     quietMillis: 100,
     ajax: {
     url: 'buyBillControllerAjax.php?do=getProductsQueryStringnoservice&storeid=' + $("#storeId").val() + '&catid=' + productCatId + '&productid=' + $("#product").val() + '',
     dataType: 'json',
     data: function (term, page) {
     return {
     term: term, //search term
     page_limit: 10 // page size
     };
     },
     results: function (data) {
     //alert("data: "+data);
     return {results: data};
     }
     },
     formatResult: formatResult,
     initSelection: function (element, callback) {
     var item = element.val().split(':');
     var data = {id: item[0], text: item[1]};
     callback(data);
     }
     });
     });
     //alert('inventoryControllerAjax.php?storeid='+theselectedvalue+'&catid='+$("#catId").val()+'&productid='+$("#product").val()+'');
     });*/

    /*$(".productCat").on("change", function (e) {
     var level = Number($("#level").val());
     var productCatId = Number($("#productCatId" + level).val());
     if (isNaN(productCatId) || productCatId < 1) {
     var productCatId = Number($("#productCatId" + (level - 1)).val());
     }
     // var theselectedvalue = $(this).val();
     $("#productsDiv").html('<img src="../views/default/images/loader/6.gif" alt="">');
     $('#productsDiv').load('inventoryControllerAjax.php?storeid=' + $("#storeId").val() + '&catid=' + productCatId + '&productid=' + $("#product").val() + '', function () {

     $("#product").select2({
     placeholder: "إسم المنتج",
     minimumInputLength: 0,
     multiple: false,
     quietMillis: 100,
     ajax: {
     url: 'buyBillControllerAjax.php?do=getProductsQueryStringnoservice&storeid=' + $("#storeId").val() + '&catid=' + productCatId + '&productid=' + $("#product").val() + '',
     dataType: 'json',
     data: function (term, page) {
     return {
     term: term, //search term
     page_limit: 10 // page size
     };
     },
     results: function (data) {
     //alert("data: "+data);
     return {results: data};
     }
     },
     formatResult: formatResult,
     initSelection: function (element, callback) {
     var item = element.val().split(':');
     var data = {id: item[0], text: item[1]};
     callback(data);
     }
     });

     });
     });*/


    /*$("#product").on("change", function (e) {


     // console.log(theSelection);
     //the selected value of drop downlsit
     var theselectedvalue = $("#product").val()// e.val;
     var level = Number($("#level").val());
     var productCatId = Number($("#productCatId" + level).val());
     if (isNaN(productCatId) || productCatId < 1) {
     var productCatId = Number($("#productCatId" + (level - 1)).val());
     }

     //////////////////////////////////////////////////
     $.ajax({
     url: 'inventoryControllerAjax.php?storeid=' + $("#storeId").val() + '&catid=' + productCatId + '&productid=' + theselectedvalue + '', //'&catid=' + $("#catId").val()
     type: "get",
     success: function (data) {
     $('#productsDiv').html(data);
     },
     error: function () {
     alert("لقد حدث خطأ أثناء إجراء العملية");
     }
     });
     //////////////////////////////////////////////////
     });*/

    $("#searchnow").on("click", function (e) {

        // console.log(theSelection);
        //the selected value of drop downlsit
        var theselectedvalue = $("#product").val()// e.val;
        var level = Number($("#level").val());
        var productCatId = Number($("#productCatId" + level).val());
        if (isNaN(productCatId) || productCatId < 1) {
            var productCatId = Number($("#productCatId" + (level - 1)).val());
        }
        var storeId = $("#storeId").val();
        if (storeId > 0) {
            $.ajax({
                url: 'inventoryControllerAjax.php?storeid=' + $("#storeId").val() + '&catid=' + productCatId + '&productid=' + $("#product").val() + '&lastInventoryDate=' + $("#lastInventoryDate").val() + '&beforeAfter=' + $('input.beforeAfter:checked').val() + ' ', //'&catid=' + $("#catId").val()
                type: "get",
                success: function (data) {
                    $('#productsDiv').html(data);
                    dataTableClass();
                },
                error: function () {
                    alert("لقد حدث خطأ أثناء إجراء العملية");
                }
            });

        } else {
            //////////////////////////////////////////////////
            alert('يجب اختيار المخزن');
            //////////////////////////////////////////////////
        }


    });


    $("#searchStoreHousing").on("click", function (e) {

        // console.log(theSelection);
        //the selected value of drop downlsit
        var theselectedvalue = $("#product").val()// e.val;
        var level = Number($("#level").val());
        var productCatId = Number($("#productCatId" + level).val());
        if (isNaN(productCatId) || productCatId < 1) {
            var productCatId = Number($("#productCatId" + (level - 1)).val());
        }

        //////////////////////////////////////////////////
        $.ajax({
            url: 'inventoryControllerAjax.php?do=storehousing&storeid=' + $("#storeId").val() + '&catid=' + productCatId + '&productid=' + $("#product").val() + '', //'&catid=' + $("#catId").val()
            type: "get",
            success: function (data) {
                $('#productsDiv').html(data);
                dataTableClass();
            },
            error: function () {
                alert("لقد حدث خطأ أثناء إجراء العملية");
            }
        });
        //////////////////////////////////////////////////
    });


    $(".autostorehousing").live("change", function (e) {
        var name = $(this).attr('data-name');// e.val;
        var values = $(this).val();
        var id = $(this).attr('data-id');// e.val;
        $.post('inventoryControllerAjax.php?do=autostorehousing', { id: id, values: values, name: name });
        var itr = $(this).attr('data-itr');
        var producttext1 = $("#producttext1" + itr).val();
        var producttext2 = $("#producttext2" + itr).val();
        var producttext3 = $("#producttext3" + itr).val();
        $("#alltext" + itr).text(producttext1 + ' - ' + producttext2 + ' - ' + producttext3);

    });


    $(".autosave").live("change", function (e) {
        var itr = $(this).attr('data-itr');
        var newQty = $(this).val();
        var productId = $("#productId" + itr).val();
        var storedetailid = $("#storedetailid" + itr).val();
        var oldQty = $("#oldQty" + itr).val();
        var storeid = $("#storeid" + itr).val();

        $.post('inventoryController.php?do=autosave', {
            newQty: newQty, productId: productId, storedetailid: storedetailid,
            oldQty: oldQty, storeid: storeid
        });
    });
});

$('.productCat').live('change', function () {
    var id = $(this).val();
    var level = Number($(this).attr('data-level'));
    level = level + 1;
    $('#level').val(level);
    $.post('salesreportajax.php?do=getsubcat', { id: id, level: level }, function (data) {
        $('.productCat').each(function () {
            var thisLevel = Number($(this).attr('data-level'));
            if (thisLevel >= level) {
                $(this).parent('div').remove();
            }
        });
        $('#productIdDiv').remove();
        if (data != "-1") {
            $('#manyParents').append(data);
        }
    });

});

$('.productCatt').live('change', function () {
    var id = $(this).val();
    var level = Number($(this).attr('data-level'));
    level = level + 1;
    $('#level').val(level);
    $.post('salesreportajax.php?do=getsubcat2', { id: id, level: level }, function (data) {
        $('.productCatt').each(function () {
            var thisLevel = Number($(this).attr('data-level'));
            if (thisLevel >= level) {
                $(this).parent('div').remove();
            }
        });
        $('#productIdDiv_').remove();
        if (data != "-1") {
            $('#manyParents').append(data);
        }
        else {
            if ($('#dontLoadProducts').length > 0 && $('#dontLoadProducts').val() == 1) {
            } else {
                $.post('salesreportajax.php?do=getproductsbills', { id: id }, function (data) {
                    $('#manyParents').append(data);
                });
            }
        }
    });

});

$(document).on('click', "#doAddManyParcodes", function () {
    if ($('#manyParcodes').val() != '') {
        $('#manyParcodesstoreid').val($('#storeId').val());
        $('#manyParcodesproductCatId').val($("#productCatId0").val());
        var parcodes = $('#manyParcodes').val();
        var storeid = $('#manyParcodesstoreid').val();
        var catid = $('#manyParcodesproductCatId').val();
        var lastInventoryDate = $('#lastInventoryDate').val();
        var beforeAfter = $('input.beforeAfter:checked').val();
        // $('#manyParcodesForm').submit();
        $.post('inventoryControllerAjax.php?do=addManyParcodesTest',
            { manyParcodes: parcodes, storeid: storeid, manyParcodesproductCatId: catid, lastInventoryDate: lastInventoryDate, beforeAfter: beforeAfter },
            function (data) {
                $('#productsDiv').html(data);
                dataTableClass();
                $('#myModalManyParcodes').modal('hide');
            });
    } else {
        alert('عفوا لم تدخل اى باركود');
    }

});
