<div style="display: none;">
    <div id="orderCartEditContent">
        <div class="popup-header toolbar-header">
            <div class="popup-title">Edit Cart Item</div>
            <div class="popup-close-btn" onclick="zamanEposPopupcloser()"><span class="icon icon-cancel"></span></div>
        </div>
        <div class="popup-content">
            <table id="cartItemTable" class="display-attr-category-element-table collection-bg"></table>
            <table id="cartItemSetItemTable"></table>
            <table id="cartItemToppingTable" class="display-attr-category-element-table"></table>
            <table id="cartItemModifierTable" class="display-attr-category-element-table"></table>
        </div>
        <div class="tab-group">
            <div class="tab-item zaman-epos-nav quantity-digit" onclick="zamanEposPopupcloser()">&nbsp;<?php echo $this->lang->line('finish') ?></div>
        </div>
    </div>
</div>
<script type="text/javascript">
    function zamanOrderCartEditControllerPopup() {
        zamanEposDb.epos_order.get({orderId: Number(zamanEposCart.getEposCurrentId())}).then(function (anOrder) {
            if (typeof (anOrder) !== 'undefined') {
                if (Number(anOrder.isPaid) === 3) {
                    orderPaymentPreProcessor(anOrder);
                } else {
                    var itemLine = $.trim($('#cartSelectedLine').val());
                    if (itemLine) {
                        $.colorbox({
                            inline: true,
                            scrolling: false,
                            href: "#orderCartEditContent",
                            width: '40%',
                            height: 'auto',
                            maxWidth: "100%",
                            open: true,
                            left: '<?php $this->session->userdata('eposStyle') === 'LTR' ? print'25%' : print '40%' ?>',
                            closeButton: false,
                            transition: 'elastic',
                            overlayClose: false,
                            className: 'transparent-overlay',
                            onOpen: function () {
                                $('#cartItemTable').html('');
                                $('#cartItemToppingTable').html('');
                                $('#cartItemToppingTable').html('');
                                $('#orderCartEditContent .popup-content').css({'min-height': '50vh', 'max-height': '70vh', 'overflow-y': 'auto'});
                                playBeepSound();
                            },
                            onLoad: function () {
                                var itemHtml = '';
                                var lineItem = zamanEposCart.listCart()[itemLine];
                                var itemTitle = '';
                                if ($.trim(lineItem.selectionTitle)) {
                                    itemTitle = lineItem.selectionTitle + '-' + lineItem.menuTitle;
                                } else {
                                    itemTitle = lineItem.menuTitle;
                                }
                                itemHtml = '<tr><td class="text-left" style="padding:2px 10px;"><span style="font-size:15px;">' + lineItem.qty + ' ' + itemTitle + '</span></td><td style="padding:2px 10px;"  class="text-right"><span onclick="editCartItemQuatity(\'' + lineItem.qty + '\',\'minus\')" class="icon icon-minus btn btn-large btn-negative"></span>&nbsp;&nbsp;<span onclick="editCartItemQuatity(\'' + lineItem.qty + '\',\'plus\')" class="icon icon-plus btn btn-large btn-positive"></span>&nbsp;&nbsp;<span onclick="editCartItemQuatity(\'' + lineItem.qty + '\',\'delete\')" class="icon icon-trash btn btn-large btn-negative"></span></td></tr>';
                                $('#cartItemTable').html(itemHtml);

                                if ((lineItem.setItem).length > 0) {
                                    var cartItemSetItemTable = '<caption style="padding:3px;">' + itemTitle + ' <?php echo $this->lang->line('items') ?></caption>';
                                    (lineItem.setItem).forEach(function (anSetItem, index) {
                                        cartItemSetItemTable += '<tr><td class="text-left" style="padding:2px 15px!important;border-bottom:1px solid #380a0a;"><span>' + anSetItem.qty + ' ' + anSetItem.title + '</span></td><td class="text-right" style="padding:2px 15px!important;border-bottom:1px solid #380a0a;"><span onclick="editCartUpdateSetItem(\'' + itemLine + '\',\'' + index + '\',\'minus\')" class="icon icon-minus btn btn-small btn-negative"></span>&nbsp;&nbsp;<span onclick="editCartUpdateSetItem(\'' + itemLine + '\',\'' + index + '\',\'plus\')" class="icon icon-plus btn btn-small btn-positive"></span>&nbsp;&nbsp;<span onclick="editCartUpdateSetItem(\'' + itemLine + '\',\'' + index + '\',\'remove\')" class="icon icon-trash btn btn-small btn-negative"></span></td></tr>';
                                    });
                                    $('#cartItemSetItemTable').html(cartItemSetItemTable);
                                }

                                if ((lineItem.Topping).length > 0) {
                                    var itemTopping = '<caption style="padding:3px;">' + itemTitle + ' <?php echo $this->lang->line('toppings') ?></caption>';
                                    (lineItem.Topping).forEach(function (anitemTopping, index) {
                                        itemTopping += '<tr><td class="text-left" style="padding:2px 15px!important;border-bottom:1px solid #380a0a;"><span>' + anitemTopping.qty + ' ' + anitemTopping.title + '</span></td><td class="text-right" style="padding:2px 15px!important;border-bottom:1px solid #380a0a;"><span onclick="editCartUpdateItemTopping(\'' + itemLine + '\',\'' + index + '\',\'minus\')" class="icon icon-minus btn btn-small btn-negative"></span>&nbsp;&nbsp;<span onclick="editCartUpdateItemTopping(\'' + itemLine + '\',\'' + index + '\',\'plus\')" class="icon icon-plus btn btn-small btn-positive"></span>&nbsp;&nbsp;<span onclick="editCartUpdateItemTopping(\'' + itemLine + '\',\'' + index + '\',\'remove\')" class="icon icon-trash btn btn-small btn-negative"></span></td></tr>';
                                    });
                                    $('#cartItemToppingTable').html(itemTopping);
                                }
                                if ((lineItem.modifier).length > 0) {
                                    var itemModifiers = '<caption style="padding:3px;">' + itemTitle + ' <?php echo $this->lang->line('modifiers') ?></caption>';
                                    (lineItem.modifier).forEach(function (aModifier, i) {
                                        itemModifiers += '<tr><td class="text-left" style="padding:2px 15px!important;border-bottom:1px solid #380a0a;"><span>' + aModifier.title + '</span></td><td class="text-right" style="padding:2px 15px!important;border-bottom:1px solid #380a0a;"><span onclick="editCartUpdateItemModifier(\'' + itemLine + '\',\'' + i + '\')" class="icon icon-trash btn btn-small btn-negative"></td></tr>';
                                    });
                                    $('#cartItemModifierTable').html(itemModifiers);
                                }
                            },
                            onComplete: function () {
                                $.colorbox.resize();
                            },
                            onCleanup: function () {
                            },
                            onClosed: function () {
                                $('#cartItemTable').html('');
                                $('#cartItemToppingTable').html('');
                                $('#cartItemModifierTable').html('');
                            }
                        });
                    } else {
                        warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('cart_item_select_msg') ?>', '<?php echo $this->lang->line('close') ?>');
                    }
                }
            } else {
                warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('load_order_in_cart') ?>', '<?php echo $this->lang->line('ok') ?>');
            }
        });
    }
    function editCartItemQuatity(currentQty, action) {
        if (action === 'delete') {
            zamanEposPopupcloser();
            removeEposCartItem();
        }
        if (action === 'minus') {
            if (Number(currentQty) === 1) {
                zamanEposPopupcloser();
                removeEposCartItem();
            } else {
                decrementEposCartItem();
                zamanOrderCartEditControllerPopup();
            }
        }
        if (action === 'plus') {
            incrementEposCartItem();
            zamanOrderCartEditControllerPopup();
        }

    }
    function editCartUpdateItemTopping(itemIndex, toppingIndex, action) {
        var lineItem = zamanEposCart.listCart()[itemIndex];
        if ((lineItem.Topping).length <= 0) {
            $('#cartItemToppingTable').html('');
            setCartLine(itemIndex);
        } else {
            var line = zamanEposCart.updateToppingInfo(itemIndex, toppingIndex, action);
            displayEposCart();
            setCartLine(line);
            var lineItem = zamanEposCart.listCart()[itemIndex];
            if ((lineItem.Topping).length <= 0) {
                $('#cartItemToppingTable').html('');
            } else {
                zamanOrderCartEditControllerPopup();
            }
        }
    }
    function editCartUpdateItemModifier(itemIndex, toppingIndex) {
        var lineItem = zamanEposCart.listCart()[itemIndex];
        if ((lineItem.modifier).length <= 0) {
            $('#cartItemModifiermodifierTable').html('');
            setCartLine(itemIndex);
        } else {
            var line = zamanEposCart.updateModifierInfo(itemIndex, toppingIndex);
            displayEposCart();
            setCartLine(line);
            var lineItem = zamanEposCart.listCart()[itemIndex];
            if ((lineItem.modifier).length <= 0) {
                $('#cartItemModifierTable').html('');
            } else {
                zamanOrderCartEditControllerPopup();
            }
        }
    }
    function editCartUpdateSetItem(itemIndex, setIndex, action) {
        var lineItem = zamanEposCart.listCart()[itemIndex];
        if ((lineItem.Topping).length <= 0) {
            $('#cartItemSetItemTable').html('');
            setCartLine(itemIndex);
        } else {
            var line = zamanEposCart.updateSetItemInfo(itemIndex, setIndex, action);
            displayEposCart();
            setCartLine(line);
            var lineItem = zamanEposCart.listCart()[itemIndex];
            if ((lineItem.Topping).length <= 0) {
                $('#cartItemSetItemTable').html('');
            } else {
                zamanOrderCartEditControllerPopup();
            }
        }
    }
</script>