<?php if (!empty($updates)): ?>
    <div class="tab-group page-order-tab" style="width: 100%" id="updateUserDataFromRemoteSource">
        <div style="padding: 10px" class="tab-item order-process-status-1" onclick="updateSystem()"><i class="icon icon-info"></i>&nbsp;<?php echo $this->lang->line('system_update_notification') ?>,&nbsp;<?php echo $this->lang->line('please_click_here') ?></div>
    </div>
<?php endif; ?>
<div class="tab-group page-order-tab" style="width: 100%; display: none;" id="forgeLocalDataFromRemote">
    <div  class="tab-item order-process-status-1" onclick="loadDataFromRemoteSource()" style="font-size: 10px; padding: 10px"><i class="icon icon-database"></i>&nbsp;<?php echo $this->lang->line('load_data') ?></div>
</div>
<div id="pleaseWait" class="tab-group page-order-tab" style="width: 100%; display: none;">
    <div  class="tab-item order-process-status-2" style="font-size: 10px; padding: 10px" id="systemUpdateInformationHook"><?php echo $this->lang->line('system_updating_msg') ?></div>
</div>
<div id='eposOrderStatusFlags' style="display: none">
    <div id="eposTableOrderPage" class="epos-order-list-page">
        <div class="tab-group page-order-tab" style="width: 100%;margin-top: -3px;">
            <div  class="tab-item order-process-status-1"><?php echo $this->lang->line('placed') ?></div>
            <div  class="tab-item order-process-status-2"><?php echo $this->lang->line('processing') ?></div>
            <div  class="tab-item order-process-status-3"><?php echo $this->lang->line('ready') ?></div>
            <div  class="tab-item order-process-status-4"><?php echo $this->lang->line('payment_waiting') ?></div>
        </div>
    </div>
</div>
<div class="place-order-type-block" id="placeOrderTypeBlock" style="display: none;">
    <div class="order-new-order" style="width: 50%; margin: 0 auto">
        <div style="margin-top: 25px;">
            <?php if ((in_array('table', $this->session->userdata('userApps')) && $services['table'] === '3') || $this->session->userdata('group') == '2'): ?>   
                <div class="tab-group">
                    <div class="tab-item zaman-epos-nav quantity-digit new-order-type table-bg" onclick="zamanPlaceTableOrder(0, '', '', '', '')">
                        <img class="table-icon" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=">
                        <span><?php echo $this->lang->line('table') ?></span>
                    </div>
                </div>
            <?php endif; ?>
            <?php if ((in_array('waiting', $this->session->userdata('userApps')) && $services['waiting'] === '3') || $this->session->userdata('group') == '2'): ?>   
                <div class="tab-group">
                    <div class="tab-item zaman-epos-nav quantity-digit new-order-type waiting-bg" onclick="zamanPlaceWaitingOrder(0, '', '', '', '')">
                        <img class="waiting-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=">
                        <span><?php echo $this->lang->line('waiting') ?></span>
                    </div>
                </div>
            <?php endif; ?>
            <?php if ((in_array('collection', $this->session->userdata('userApps')) && $services['collection'] === '3') || $this->session->userdata('group') == '2'): ?>   
                <div class="tab-group">
                    <div class="tab-item zaman-epos-nav quantity-digit new-order-type collection-bg" onclick="zamanPlaceCollectionOrder(0, '', '', '', '', '')">
                        <img class="collection-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=">
                        <span><?php echo $this->lang->line('collection') ?></span>
                    </div>
                </div>
            <?php endif; ?>
            <?php if ((in_array('delivery', $this->session->userdata('userApps')) && $services['delivery'] === '3') || $this->session->userdata('group') == '2'): ?>   
                <div class="tab-group">
                    <div class="tab-item zaman-epos-nav quantity-digit new-order-type delivery-bg" onclick="zamanPlaceDeliveryOrder(0, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '')">
                        <img class="delivery-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=">
                        <span><?php echo $this->lang->line('delivery') ?></span>
                    </div>
                </div>
            <?php endif; ?>
            <?php if ((in_array('bar', $this->session->userdata('userApps')) && $services['bar'] === '3') || $this->session->userdata('group') == '2'): ?>   
                <div class="tab-group">
                    <div class="tab-item zaman-epos-nav quantity-digit new-order-type bar-bg" onclick="zamanPlaceBarOrder(0, '', '', '', '')">
                        <img class="bar-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC8AAAAvAQMAAABJ6QHvAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgFVAEAAUkAAf+U2WcAAAAASUVORK5CYII=">
                        <span><?php echo $this->lang->line('bar') ?></span>
                    </div>
                </div>
            <?php endif; ?>
            <?php if ((in_array('booking', $this->session->userdata('userApps')) && $services['booking'] === '3') || $this->session->userdata('group') == '2'): ?>   
                <div class="tab-group">
                    <div class="tab-item zaman-epos-nav quantity-digit new-order-type booking-bg" onclick="zamanBookingOrderInfoControllerPopup(0, '', '', '', '', '', '', '', '', '')">
                        <img class="booking-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=">
                        <span><?php echo $this->lang->line('booking') ?></span>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>   
</div>
<div id='EposOrderListArea' style='display: none;'></div>
<?php if ((in_array('table', $this->session->userdata('userApps')) && $services['table'] === '3') || $this->session->userdata('group') == '2') { ?>
    <div id='EposOrderListAreaTable' style='display: none;'></div>
<?php } ?>
<?php if ((in_array('bar', $this->session->userdata('userApps')) && $services['bar'] === '3') || $this->session->userdata('group') == '2') { ?>
    <div id='EposOrderListAreaBar' style='display: none;'></div>
<?php } ?>
<?php if ((in_array('waiting', $this->session->userdata('userApps')) && $services['waiting'] === '3') || $this->session->userdata('group') == '2') { ?>
    <div id='EposOrderListAreaWaiting' style='display: none;'></div>
<?php } ?>
<?php if ((in_array('collection', $this->session->userdata('userApps')) && $services['collection'] === '3') || $this->session->userdata('group') == '2') { ?>
    <div id='EposOrderListAreaCollection' style='display: none;'></div>
<?php } ?>
<?php if ((in_array('delivery', $this->session->userdata('userApps')) && $services['delivery'] === '3') || $this->session->userdata('group') == '2') { ?>
    <div id='EposOrderListAreaDelivery' style='display: none;'></div>
<?php } ?>
<input type="hidden" id="isToShowAllOrders" value="yes">
<div id='categoryBreadcurm'>
    <div class="breadcurmb caption" style="padding: 1px;">
        <ul class="breadcurmb-page text-left">
            <li class="breadcurmb-category text-left"></li>
            <li class="breadcurmb-menu text-left"></li>
        </ul>
    </div>
</div>
<div class="menu-list" id="eposCategoryMenuList"></div>
<div class="clearfix"></div>
<?php $userAppPriveleges = $this->session->userdata('userApps') ?>
<?php if (count($userAppPriveleges) !== 1): ?>
    <div id='footerOrderNavigation' style='display: none;'>
        <div style="bottom: 41px;position: fixed;width: 41.7%;border-right: 1px solid #9ca5a0;border:none">
            <div class="tab-group footer-sticky footer-sticky-top">
                <div  class="tab-item zaman-epos-nav footer-order-list-button" id="list-all-orders" onclick="zamanOrderListPageController()"><span class="icon icon-list"></span></div>
                <?php if ((in_array('table', $this->session->userdata('userApps')) && $services['table'] === '3') || $this->session->userdata('group') == '2'): ?>   
                    <div  class="tab-item zaman-epos-nav footer-order-list-button" id="list-table-only" onclick="listTableOrder('no')"><span class="icon"><img class="table-icon" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=" style="width: 14px;"></span></div>
                <?php endif; ?>   
                <?php if ((in_array('waiting', $this->session->userdata('userApps')) && $services['waiting'] === '3') || $this->session->userdata('group') == '2'): ?>   
                    <div  class="tab-item zaman-epos-nav footer-order-list-button" id="list-waiting-only" onclick="listWaitingOrder('no')"><span class="icon"><img class="waiting-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII="  style="width: 14px;"></span></div>
                <?php endif; ?>   
                <?php if ((in_array('collection', $this->session->userdata('userApps')) && $services['collection'] === '3') || $this->session->userdata('group') == '2'): ?>   
                    <div  class="tab-item zaman-epos-nav footer-order-list-button" id="list-collection-only" onclick="listCollectionOrder('no')"><span class="icon"><img class="collection-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=" style="width: 14px;"></span></div>
                <?php endif; ?>   
                <?php if ((in_array('delivery', $this->session->userdata('userApps')) && $services['delivery'] === '3') || $this->session->userdata('group') == '2'): ?>   
                    <div  class="tab-item zaman-epos-nav footer-order-list-button" id="list-delivery-only" onclick="listDeliveryOrder('no')"><span class="icon"><img class="delivery-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=" style="width: 14px;"></span></div>
                <?php endif; ?>  
                <?php if ((in_array('bar', $this->session->userdata('userApps')) && $services['bar'] === '3') || $this->session->userdata('group') == '2'): ?>   
                    <div  class="tab-item zaman-epos-nav footer-order-list-button" id="list-bar-only" onclick="listBarOrder('no')"><span class="icon"><img class="bar-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC8AAAAvAQMAAABJ6QHvAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgFVAEAAUkAAf+U2WcAAAAASUVORK5CYII=" style="width: 14px;"></span></div>
                <?php endif; ?>  
                <?php if ($this->session->userdata('group') === '5'): ?>
                    <div  class="tab-item zaman-epos-nav footer-order-list-button" id="list-delivery-only" onclick="listAppOrder()">App <span id="pendingAppOrder"></span></div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
<?php endif; ?>
<div id='footerMenuNavigation'>
    <div style="bottom: 0px;position: fixed;width: 41.7%;border-right: 1px solid #9ca5a0;border-left: 1px solid #9ca5a0;">
        <div class="tab-group footer-sticky">
            <?php if ($this->session->userdata('eposStyle') === 'RTL'): ?>
                <div class="tab-item zaman-epos-nav new-order-tab-btn" onclick="zamanOrderListPageController()"><span style="color: #ddfc0d" class="icon icon-list"></span>&nbsp;<?php echo $this->lang->line('my_orders') ?></div>
                <?php if ($this->session->userdata('group') === '5' && isDevice('Electron')): ?>
                    <div class="tab-item zaman-epos-nav" onclick="sendCommandToEpos('reloadApp')"><?php echo $this->lang->line('refresh') ?></div>
                <?php else: ?>
                    <div class="tab-item zaman-epos-nav" onclick="window.location.reload(true)"><?php echo $this->lang->line('refresh') ?></div>
                <?php endif; ?>
            <?php endif; ?>
            <?php if ($this->session->userdata('eposStyle') === 'LTR'): ?>
                <div class="tab-item zaman-epos-nav" onclick="warning('<?php echo $this->lang->line('contact') ?>', '<?php echo $this->lang->line('system_owner_company') ?><br/><?php echo $this->lang->line('system_owner_phone') ?><br/><a style=' + 'cursor:pointer;' + ' href=' + 'mailto:<?php echo $this->lang->line('system_notify_email') ?>' + '><?php echo $this->lang->line('system_notify_email') ?></a><br/><?php echo $this->lang->line('system_owner_website') ?>', '<?php echo $this->lang->line('close') ?>')">Help</div>
            <?php endif; ?>
            <div class="tab-item" onclick="menuSelectionPagination('prev')"><span class="icon icon-left-open-big"></span></div>
            <div class="tab-item"><span id="menuSelCurrentPage"></span>&nbsp;/&nbsp;<span id="menuSelTotalPage"></span></div>
            <div class="tab-item" onclick="menuSelectionPagination('next')"><span class="icon icon-right-open-big"></span></div>
            <?php if ($this->session->userdata('eposStyle') === 'LTR'): ?>
                <?php if ($this->session->userdata('group') === '5' && isDevice('Electron')): ?>
                    <div class="tab-item zaman-epos-nav" onclick="sendCommandToEpos('reloadApp')"><?php echo $this->lang->line('refresh') ?></div>
                <?php else: ?>
                    <div class="tab-item zaman-epos-nav" onclick="window.location.reload(true)"><?php echo $this->lang->line('refresh') ?></div>
                <?php endif; ?>
                <div class="tab-item zaman-epos-nav new-order-tab-btn" onclick="zamanOrderListPageController()"><span style="color: #ddfc0d" class="icon icon-list"></span>&nbsp;<?php echo $this->lang->line('my_orders') ?></div>
            <?php endif; ?>
            <?php if ($this->session->userdata('eposStyle') === 'RTL'): ?>
                <div class="tab-item zaman-epos-nav" onclick="warning('<?php echo $this->lang->line('contact') ?>', '<?php echo $this->lang->line('system_owner_company') ?><br/><?php echo $this->lang->line('system_owner_phone') ?><br/><a style=' + 'cursor:pointer;' + ' href=' + 'mailto:<?php echo $this->lang->line('system_notify_email') ?>' + '><?php echo $this->lang->line('system_notify_email') ?></a><br/><?php echo $this->lang->line('system_owner_website') ?>', '<?php echo $this->lang->line('close') ?>')">Help</div>
            <?php endif; ?>   
        </div>
    </div>
</div>
<script type="text/javascript">
    function updateSystem() {
        $('.page-loader').show();
        $('#updateUserDataFromRemoteSource').hide();
        $('#pleaseWait').show();
        var updates = JSON.parse('<?php echo json_encode($updates) ?>');
        updates.forEach(function (anUpdate) {
            if (checkInternetConnection()) {
                window[anUpdate.issueTitle](arguments);
                window.setTimeout(function () {
                    $.ajax({
                        url: "<?php echo site_url('user/update') ?>",
                        type: 'POST',
                        dataType: 'JSON',
                        data: {issue: anUpdate.issueTitle},
                        success: function (response) {
                            if (response.status) {
                                if (response.issuesPending < 1) {
                                    location.reload();
                                }
                            } else {
                                warning('<?php echo $this->lang->line('error') ?>', '<?php echo $this->lang->line('data_saving_failed') ?>', '<?php echo $this->lang->line('close') ?>');
                            }
                        }
                    });
                }, 5000);
            } else {
                warning('<?php echo $this->lang->line('lost_connection') ?>', '<?php echo $this->lang->line('lost_connection_message') ?>', '<?php echo $this->lang->line('close') ?>');
            }
        });
    }
    function loadDataFromRemoteSource() {
        Dexie.exists('zamanEposDb').then(function (exists) {
            if (!exists) {
                initializeZamanDbTableStructure();
                zamanEposDb.open();
                startForgingRemoteDatabase();
            } else {
                zamanEposDb.epos_order.count(function (count) {
                    if (count > 0) {
                        warning('<?php echo $this->lang->line('error') ?>', '<?php echo $this->lang->line('unclosed_order_msg_on_forging') ?>', '<?php echo $this->lang->line('ok') ?>')
                    } else {
                        $('.page-loader').show();
                        zamanEposDb.close();
                        zamanEposDb.delete().then(function () {
                            initializeZamanDbTableStructure();
                            zamanEposDb.open();
                            startForgingRemoteDatabase();
                        }).catch(function (err) {
                            $('.page-loader').hide();
                            console.log(err);
                        });
                    }
                });
            }
        });
    }
    function startForgingRemoteDatabase() {
        if (checkInternetConnection()) {
            $.ajax({
                url: "<?php echo site_url('user/changeOwnerUpdateStatus') ?>",
                type: 'GET',
                dataType: 'JSON',
                success: function (response) {
                    if (response.status) {
                        $('#forgeLocalDataFromRemote').hide();
                        $('#pleaseWait').show();
                        initiateAllZamanEposTables();
                    } else {
                        warning('<?php echo $this->lang->line('error') ?>', '<?php echo $this->lang->line('data_saving_failed') ?>', '<?php echo $this->lang->line('close') ?>');
                    }
                }
            });
        } else {
            warning('<?php echo $this->lang->line('lost_connection') ?>', '<?php echo $this->lang->line('lost_connection_message') ?>', '<?php echo $this->lang->line('close') ?>');
        }
    }
    function chooseOrderType() {
        clearEposWorkArea();
        clearEposWorkAreaForOrderList();
        $('#placeOrderTypeBlock').show();
    }
    function menuSelectionPagination(btn) {
        if (Number($('#menuSelCurrentPage').text()) <= Number($('#menuSelTotalPage').text())) {
            var curentPage = $('#currentItemPageMenuOrSelection').val();
            var currentItemsCategory = $('#currentItemsCategory').val();
            if ($.trim(curentPage) === $.trim('menupage')) {
                var currentValue = Number($('#menuPaginationBtnValue').val());
                if ($.trim(btn) === $.trim('next')) {
                    if (Number($('#menuSelCurrentPage').text()) < Number($('#menuSelTotalPage').text())) {
                        if (currentValue >= 0) {
                            currentValue++;
                            $('#menuPaginationBtnValue').val(currentValue);
                            zamanEposMenu(currentItemsCategory);
                            playBeepSound();
                        }
                    } else {
                        warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('no_more_items_to_show') ?>', '<?php echo $this->lang->line('close') ?>');
                    }
                } else {
                    if (currentValue >= 1) {
                        currentValue--;
                        $('#menuPaginationBtnValue').val(currentValue);
                        zamanEposMenu(currentItemsCategory);
                        playBeepSound();
                    } else {
                        warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('no_more_items_to_show') ?>', '<?php echo $this->lang->line('close') ?>');
                    }
                }
            } else if ($.trim(curentPage) === $.trim('selectionpage')) {
                var currentValue = Number($('#selectionPaginationBtnValue').val());
                var currentMenu = $('#currentItemsMenu').val();
                if ($.trim(btn) === $.trim('next')) {
                    if (Number($('#menuSelCurrentPage').text()) < Number($('#menuSelTotalPage').text())) {
                        if (currentValue >= 0) {
                            currentValue++;
                            $('#selectionPaginationBtnValue').val(currentValue);
                            zamanEposMenuSelection(currentMenu);
                            playBeepSound();
                        }
                    } else {
                        warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('no_more_items_to_show') ?>', '<?php echo $this->lang->line('close') ?>');
                    }
                } else {
                    if (currentValue >= 1) {
                        currentValue--;
                        $('#selectionPaginationBtnValue').val(currentValue);
                        zamanEposMenuSelection(currentMenu);
                        playBeepSound();
                    } else {
                        warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('no_more_items_to_show') ?>', '<?php echo $this->lang->line('close') ?>');
                    }
                }
            } else {
                $('#currentItemPageMenuOrSelection').val('menupage');
            }
        } else {
            warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('no_more_items_to_show') ?>', '<?php echo $this->lang->line('close') ?>');
        }

    }
    function zamanEposMenu(category) {
        playBeepSound();
        zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_menucategory, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection, zamanEposDb.epos_order], function () {
            zamanEposDb.epos_order.get({orderId: Number(zamanEposCart.getEposCurrentId())}).then(function (anOrder) {
                if (typeof (anOrder) !== 'undefined') {
                    $('#placeOrderTypeBlock').hide();
                    clearEposWorkArea();
                    if (Number(category) > 0) {
                        $('#currentItemsCategory').val(category);
                        $('#currentItemsMenu').val('');
                        $('#selectionPaginationBtnValue').val(0);
                        var userData = zamanEposCart.getUserData();
                        zamanEposDb.epos_branch.get({'id': userData.branch}).then(function (branch) {
                            if (typeof (branch) !== "undefined") {
                                zamanEposDb.epos_menucategory.get({'id': category}).then(function (categoryObj) {
                                    if (typeof (categoryObj) !== "undefined") {
                                        $('.breadcurmb-category').html('<span style="color:#ddfc0d;" onclick="zamanEposMenu(\'' + categoryObj.id + '\')">' + categoryObj.title + '</span>');
                                        $('.breadcurmb-menu').html('&nbsp;&nbsp;<span class="icon icon-right-open-big">&nbsp;&nbsp;&nbsp;<span style="color:#ddfc0d;">Menus</span></span>');
                                        $('.menu-category.menu-button-inner').removeClass('active');
                                        $('.menu-category.menu-button-inner#category-' + categoryObj.id).addClass('active');
                                    }
                                    zamanEposDb.epos_menu.where({'category': category}).count(function (count) {
                                        var pages = Math.ceil(Number(count) / Number(branch.pagination_menu));
                                        $('#menuSelCurrentPage').text(Number($('#menuPaginationBtnValue').val()) + 1);
                                        $('#menuSelTotalPage').text(pages);
                                        var limit = Number(branch.pagination_menu);
                                        var offset = limit * Number($('#menuPaginationBtnValue').val());
                                        zamanEposDb.epos_menu.orderBy('lid').filter(function (filterItem) {
                                            return filterItem.status === '3' && filterItem.inline === '3' && filterItem.category === category && filterItem.branch === branch.id;
                                        }).distinct().offset(offset).limit(limit).toArray().then(function (catMenus) {
                                            var menuHtml = '';
                                            if (catMenus[0]) {
                                                catMenus.forEach(function (menu) {
                                                    menuHtml += '<div class="menu-button"><div class="menu-button-inner text-capitalize" onclick="processMe(\'' + menu.category + '\',\'' + menu.id + '\',\'0\')">' + menu.title + '</div></div>';
                                                });
                                            } else {
                                                zamanEposMenu(0);
                                                $('#menuPaginationBtnValue').val('0');
                                                $('#menuSelCurrentPage').text('0');
                                                $('#menuSelTotalPage').text('0');
                                            }
                                            $('#eposCategoryMenuList').html(menuHtml);
                                        });
                                        $('#currentItemPageMenuOrSelection').val('menupage');
                                    });
                                });
                            }
                        });
                    }
                } else {
                    playBeepSound();
                    settleDownNewOrderOption();

                }
            });
        });
    }
    function processMe(category, menu, selection) {
        $('#placeOrderTypeBlock').hide();
        zamanEposPopupcloser();
        clearEposWorkArea();
        zamanEposDb.epos_order.get({orderId: Number(zamanEposCart.getEposCurrentId())}).then(function (anOrder) {
            if (typeof (anOrder) !== 'undefined') {
                if (Number(anOrder.isPaid === 3)) {
                    orderPaymentPreProcessor(anOrder);
                } else {
                    if (Number(selection) > 0) {
                        menuOrSelection(category, menu, selection);
                    } else {
                        zamanEposDb.epos_menuselection.orderBy('lid').filter(function (filterItem) {
                            return filterItem.status === '3' && filterItem.menu === menu;
                        }).toArray().then(function (items) {
                            if (items.length > 0) {
                                ;
                                $('#currentItemPageMenuOrSelection').val('selectionpage');
                                zamanEposMenuSelection(menu);
                            } else {
                                menuOrSelection(category, menu, selection);
                            }
                        });
                    }
                }
            } else {
                settleDownNewOrderOption();
            }
        });
    }
    function zamanEposMenuSelection(menu) {
        clearEposWorkArea();
        $('#currentItemsMenu').val(menu);
        zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection], function () {
            var userData = zamanEposCart.getUserData();
            zamanEposDb.epos_branch.get({'id': userData.branch}).then(function (branch) {
                zamanEposDb.epos_menu.get({'id': menu}).then(function (menuObj) {
                    $('.breadcurmb-menu').html('&nbsp;&nbsp;<span style="color:#ddfc0d;" onclick="zamanEposMenuSelection(\'' + menuObj.id + '\')">&nbsp;&nbsp;&nbsp;<span class="icon icon-right-open-big">&nbsp;&nbsp;&nbsp;</span>' + menuObj.title + '&nbsp;&nbsp;&nbsp;<span class="icon icon-right-open-big">&nbsp;&nbsp;&nbsp;</span>Selections</span>');
                    zamanEposDb.epos_menuselection.where({'menu': menu}).count(function (count) {
                        var pages = Math.ceil(Number(count) / Number(branch.pagination_selection));
                        $('#menuSelCurrentPage').text(Number($('#selectionPaginationBtnValue').val()) + 1);
                        $('#menuSelTotalPage').text(pages);
                        var limit = Number(branch.pagination_selection);
                        var offset = limit * Number($('#selectionPaginationBtnValue').val());
                        zamanEposDb.epos_menuselection.orderBy('lid').filter(function (filterItem) {
                            return filterItem.status === '3' && filterItem.inline === '3' && filterItem.menu === menu && filterItem.branch === branch.id;
                        }).distinct().offset(offset).limit(limit).toArray().then(function (selections) {
                            var selectionHtml = '';
                            selections.forEach(function (selection) {
                                selectionHtml += '<div class="menu-button"><div class="menu-button-inner text-capitalize" onclick="menuOrSelection(\'' + selection.category + '\',\'' + selection.menu + '\',\'' + selection.id + '\')">' + selection.title + '</div></div>';
                            });
                            $('#eposCategoryMenuList').html(selectionHtml);
                        });
                    });
                });
            });
        });
    }
    function menuOrSelection(category, menu, selection) {
        clearAllOptions();
        if (Number(selection) === 0) {
            manageMenu(category, menu);
        } else {
            manageSelection(category, menu, selection);
        }
    }
    function manageMenu(category, menu) {
        zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_order, zamanEposDb.epos_configuration, zamanEposDb.epos_menucategory, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection, zamanEposDb.epos_menuattrcategory, zamanEposDb.epos_modifiercategory, zamanEposDb.epos_menufixeddeal, zamanEposDb.epos_menuset, zamanEposDb.epos_menusetitem, zamanEposDb.epos_discountplan, zamanEposDb.epos_discountmulti], function () {
            zamanEposDb.epos_menucategory.get({id: category}).then(function (categoryObject) {
                zamanEposDb.epos_menu.get({id: menu}).then(function (menuObject) {
                    var showItemInfoValue = $('#showItemInfo').val();
                    if (showItemInfoValue === 'no') {
                        if (Number(menuObject.deal_type) > 0) {
                            if (menuObject.deal_type === '3') {
                                manageMenuFixedDeal(categoryObject, menuObject, {});
                            }
                            if (menuObject.deal_type === '2') {
                                manageMenuCustomDeal(categoryObject, menuObject, {});
                            }
                        } else {
                            if (Number(menuObject.has_topping) > 0 || Number(menuObject.has_modifier) > 0) {
                                zamanEposDb.epos_menuattrcategory.where({'status': '3'}).filter(function (attrCat) {
                                    return attrCat.category === category && attrCat.menu === menu && attrCat.selection === '0' && attrCat.set === '0';
                                }).toArray().then(function (attrObject) {
                                    zamanEposDb.epos_modifiercategory.where({'status': '3'}).filter(function (modCat) {
                                        return modCat.category === category && modCat.menu === menu && modCat.selection === '0' && modCat.set === '0';
                                    }).toArray().then(function (modifierObject) {
                                        if (modifierObject.length > 0 || attrObject.length > 0) {
                                            zamanItemAttributeControllerPopup(category, menu, '0', '0', menuObject.title);
                                        } else {
                                            addToCart(categoryObject, menuObject, {});
                                        }
                                    });
                                });
                            } else {
                                addToCart(categoryObject, menuObject, {});
                            }
                        }
                    } else {
                        zamanItemInfoControllerPopup(categoryObject, menuObject, {});
                    }
                });
            });
        });
    }
    function manageMenuFixedDeal(categoryObject, menuObject, selectionObject) {
        zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_order, zamanEposDb.epos_configuration, zamanEposDb.epos_menufixeddeal, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection, zamanEposDb.epos_discountplan, zamanEposDb.epos_discountmulti], function () {
            zamanEposDb.epos_menufixeddeal.where({'deal': menuObject.id}).filter(function (item) {
                return item.status === '3';
            }).toArray().then(function (items) {
                items.forEach(function (dealItem) {
                    zamanEposDb.epos_menu.get({'id': dealItem.menu}, function (thisMneuItem) {
                        var setItem = new Array();
                        setItem.id = thisMneuItem.id;
                        setItem.title = thisMneuItem.title;
                        setItem.qty = parseInt(Number(dealItem.qty));
                        setItem.set = dealItem.deal;
                        setItem.setTitle = jQuery.isEmptyObject(selectionObject) ? menuObject.title : menuObject.title + '-' + selectionObject.title;
                        zamanEposCart.addSetItem(setItem);
                    });
                });
            }).then(function () {
                if (Number(menuObject.has_topping) > 0 || Number(menuObject.has_modifier) > 0) {
                    zamanItemAttributeControllerPopup(menuObject.category, menuObject.id, '0', '0', menuObject.title);
                } else {
                    window.setTimeout(function () {
                        addToCart(categoryObject, menuObject, selectionObject);
                    }, 100);
                }
            });
        });
    }
    function manageMenuCustomDeal(categoryObject, menuObject, selectionObject) {
        zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_order, zamanEposDb.epos_configuration, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection, zamanEposDb.epos_menuset, zamanEposDb.epos_menusetitem, zamanEposDb.epos_discountplan, zamanEposDb.epos_discountmulti], function () {
            zamanEposDb.epos_menuset.where({'menu': menuObject.id}).filter(function (set) {
                return set.status === '3' && set.category === categoryObject.id;
            }).toArray().then(function (allSets) {
                if (allSets.length > 0) {
                    zamanCustomDealControllerPopup(categoryObject, menuObject, selectionObject, allSets);
                } else {
                    addToCart(categoryObject, menuObject, selectionObject);
                }
            });
        });
    }
    function manageSelection(category, menu, selection) {
        zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_order, zamanEposDb.epos_configuration, zamanEposDb.epos_menucategory, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection, zamanEposDb.epos_menuattrcategory, zamanEposDb.epos_modifiercategory, zamanEposDb.epos_menufixeddeal, zamanEposDb.epos_menuset, zamanEposDb.epos_menusetitem, zamanEposDb.epos_discountplan, zamanEposDb.epos_discountmulti], function () {
            zamanEposDb.epos_menucategory.get({id: category}).then(function (categoryObject) {
                zamanEposDb.epos_menu.get({id: menu}).then(function (menuObject) {
                    zamanEposDb.epos_menuselection.get({id: selection}).then(function (selectionObject) {
                        var showItemInfoValue = $('#showItemInfo').val();
                        if (showItemInfoValue === 'no') {
                            if (Number(selectionObject.deal_type) > 0) {
                                if (selectionObject.deal_type === '3') {
                                    manageSelectionFixedDeal(categoryObject, menuObject, selectionObject);
                                }
                                if (selectionObject.deal_type === '2') {
                                    manageSelectionCustomDeal(categoryObject, menuObject, selectionObject);
                                }
                            } else {
                                if (Number(selectionObject.has_topping) > 0 || Number(selectionObject.has_modifier) > 0) {
                                    zamanEposDb.epos_menuattrcategory.where({'status': '3'}).filter(function (attrCat) {
                                        return attrCat.category === category && attrCat.menu === menu && attrCat.selection === selection && attrCat.set === '0';
                                    }).toArray().then(function (attrObject) {
                                        zamanEposDb.epos_modifiercategory.where({'status': '3'}).filter(function (modCat) {
                                            return modCat.category === category && modCat.menu === menu && modCat.selection === selection && modCat.set === '0';
                                        }).toArray().then(function (modifierObject) {
                                            if (modifierObject.length > 0 || attrObject.length > 0) {
                                                zamanItemAttributeControllerPopup(category, menu, selection, '0', selectionObject.title + '-' + menuObject.title);
                                            } else {
                                                addToCart(categoryObject, menuObject, selectionObject);
                                            }
                                        });
                                    });
                                } else {
                                    addToCart(categoryObject, menuObject, selectionObject);
                                }
                            }
                        } else {
                            zamanItemInfoControllerPopup(categoryObject, menuObject, selectionObject);
                        }
                    });
                });
            });
        });
    }
    function manageSelectionFixedDeal(categoryObject, menuObject, selectionObject) {
        zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_order, zamanEposDb.epos_configuration, zamanEposDb.epos_menufixeddeal, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection, zamanEposDb.epos_discountplan, zamanEposDb.epos_discountmulti], function () {
            zamanEposDb.epos_menufixeddeal.where({'deal': selectionObject.id}).filter(function (item) {
                return item.status === '3';
            }).toArray().then(function (items) {
                items.forEach(function (dealItem) {
                    zamanEposDb.epos_menu.get({'id': dealItem.menu}, function (thisMneuItem) {
                        zamanEposDb.epos_menuselection.get({'id': dealItem.selection}, function (thisSelectionItem) {
                            var setItem = new Array();
                            setItem.title = typeof (thisSelectionItem) !== "undefined" ? thisSelectionItem.title + '-' + thisMneuItem.title : thisMneuItem.title;
                            setItem.qty = parseInt(Number(dealItem.qty));
                            zamanEposCart.addSetItem(setItem);
                        });
                    });
                });
            }).then(function () {
                if (Number(selectionObject.has_topping) > 0 || Number(selectionObject.has_modifier) > 0) {
                    zamanItemAttributeControllerPopup(selectionObject.category, selectionObject.menu, selectionObject.id, '0', selectionObject.title + '-' + menuObject.title);
                } else {
                    window.setTimeout(function () {
                        addToCart(categoryObject, menuObject, selectionObject);
                    }, 100);
                }
            });
        });
    }
    function manageSelectionCustomDeal(categoryObject, menuObject, selectionObject) {
        zamanEposDb.transaction('rw', [zamanEposDb.epos_branch, zamanEposDb.epos_order, zamanEposDb.epos_configuration, zamanEposDb.epos_menu, zamanEposDb.epos_menuselection, zamanEposDb.epos_menuset, zamanEposDb.epos_menusetitem, zamanEposDb.epos_discountplan, zamanEposDb.epos_discountmulti], function () {
            zamanEposDb.epos_menuset.where({'selection': selectionObject.id}).filter(function (set) {
                return set.status === '3' && set.category === categoryObject.id && set.menu === menuObject.id;
            }).toArray().then(function (allSets) {
                if (allSets.length > 0) {
                    zamanCustomDealControllerPopup(categoryObject, menuObject, selectionObject, allSets);
                } else {
                    addToCart(categoryObject, menuObject, selectionObject);
                }
            });
        });
    }
</script>