
$(document).ready(function () {
    if ($('#design').val() == 'new') {
        $('.body_res').css('display', 'block');
        getFoodMenu();
    }

    //choose cat event
    $('.parent-menu li a').live('click', function () {
        var catId = $(this).attr('data-cat');

        $('.parent-menu').css('display', 'none');
        $('.category-menu').css('display', 'block');
        //hide all products and show products of this cat
        $('.category-menu a[data-cat!="' + catId + '"]').css('display', 'none');
        $('.category-menu a[data-cat="' + catId + '"]').css('display', 'block');
    });
    //chooseProduct event
    $('.chooseProduct').live('click', function () {
        var productId = $(this).attr('data-productId');
        var proName = $(this).find('.proName').text();
        var itr = $('#itr').val();
        var quantity = getProductQuantity(productId);
        itr++;
        $('#orderTBody').append(
            '<tr> <input type="hidden" name="product'+itr+'" id="product'+itr+'" value="' + productId + '">' +
            '<td>'+itr+'</td>'+
            '<td><span>' + proName + '</span></td>' +
            '<td><span>' + quantity + '</span></td>' +
            '<td><input type="number" name="number' + itr + '" id="number' + itr + '" value="1" class="form-control" /></td>' +
            '<td><textarea name="comment' + itr + '" id="comment' + itr + '" class="form-control"></textarea></td>' +
            '<td id="' + itr + '" class="hidden-print">' +
            '<button id="remove' + itr + '" type="button" class="close flt-none removeicon removeproduct" aria-hidden="true">&times;</button>' +
            '</tr>');
        $('#itr').val(itr);

        $("#number" + itr).select();
    });
    $(document).on('click', '.removeproduct', function () {
            $itemRemove = Number($(this).parent().attr("id"));
            var productid = document.getElementById("product" + $itemRemove).value;
            // delete by partents
            $(this).parent().parent().remove();
            return false;
    });

    // prev icon click
    $('.prev-icon').live('click', function () {//img.prev-icon
        $('.parent-menu').css('display', 'block');
        $('.category-menu').css('display', 'none');
    });
    //search products event
    $('#query').live('keyup', function (e) {
        var serachName = $(this).val();
        $('.parent-menu').css('display', 'none');
        $('.category-menu').css('display', 'block');
        //hide all products and show products of this cat
        $('.proName').parent().parent().parent().css('display', 'none');
        $('.proName[data-name*="' + serachName + '"], .proName[data-parcode*="' + serachName + '"]').parent().parent().parent().css('display', 'block');
        if (serachName == "") {
            $('.proName').parent().parent().parent().css('display', 'block');
        }
    });
    $('#query').live('keydown', function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            e.stopPropagation();
            if ($('.chooseProduct:visible').length > 0) {
                $('.chooseProduct:visible').first().click();
                $('#query').val("");
            }
            return false;
        }
    });
});


function getProductQuantity(value, itr) {
    var storeidfrom = document.getElementById("storeId").value;
    var result = 0;
    $.ajax({
        url: 'storemovementAjaxController.php?do=getquantity&productid=' + value + '&storeidfrom=' + storeidfrom,
        async: false,
        success: function (x) {
            if (x === '' || x === null) {
                result = 0;
            } else {
                result = x;
            }
        },
        error: function () {
            result = 0;
        }
    });
    return result;
}

function getFoodMenu(tableId) {
    $('#catDiv').html("loading...");
    $('#foodDiv').html("loading...");
    $.ajax({
        type: 'POST',
        url: 'sellbillAjaxController.php?do=getFoodMenu',
        data: { tableId: tableId },
        async: false,
        success: function (data) {
            data = JSON.parse(data);
            var hallData = data[2];
            //draw cats of food
            var html = '';
            data[0].forEach(function (cat) {
                html += '<li>' +
                    '<a data-cat="' + cat.productCatId + '">';
                if (cat.logo != "" && cat.logo != ".") {

                    html += '<img src="../views/default/images/cat_image/' + cat.logo + '">';
                } else {
                    html += '<img src="../views/default/assets/img/resturant/food1.jpg">';
                }
                html += '<span>' + cat.productCatName + '</span>' +
                    '</a>' +
                    '</li>';
            });
            $('#catDiv').html(html);
            //draw products of food
            var html = '';
            data[1].forEach(function (product) {
                html += '<a href="javascript:;" class="chooseProduct" data-cat="' + product.productCatId + '" data-productId="' + product.productId + '">' +
                    '<div class="block">';
                if (product.logo != "" && product.logo != ".") {
                    html += '<img src="../views/default/images/product_image/' + product.logo + '">';
                } else {
                    html += '<img src="../views/default/assets/img/resturant/food1.jpg">';
                }
                html += '<div class="info">' +
                    '    <span class="name proName" data-name="' + product.productName + '" data-parcode="' + product.parcode + '">' + product.productName + '</span>';
                var hallPrice = 0;
                if (hallData != null) {
                    hallPrice = Number(hallData.hallPrice)
                }
                switch (hallPrice) {
                    case 0:
                        html += '    <span class="price" data-productId="' + product.productId + '">' + product.productSellAllPrice + '</span>';
                        break;
                    case 1:
                        html += '    <span class="price" data-productId="' + product.productId + '">' + product.productSellUnitPrice + '</span>';
                        break;
                    case 2:
                        html += '    <span class="price" data-productId="' + product.productId + '">' + product.productSellHalfPrice + '</span>';
                        break;
                }

                html += ' </div>' +
                    '</div>' +
                    '</a>';
            });
            $('#foodDiv').html(html);

            $('.parent-menu').css('display', 'block');
            $('.category-menu').css('display', 'none');
        }
    });
}