<?php
$this->load->view('partials/header');
$this->load->view('window');
/* --------------------------------------------
 * Order Initialized Order && list pages pages
 * ------------------------------------------- */
if ((in_array('table', $this->session->userdata('userApps')) && $services['table'] === '3') || $this->session->userdata('group') == '2') {
    $this->load->view('table');
}
if ((in_array('waiting', $this->session->userdata('userApps')) && $services['waiting'] === '3') || $this->session->userdata('group') == '2') {
    $this->load->view('waiting');
}

if ((in_array('bar', $this->session->userdata('userApps')) && $services['bar'] === '3') || $this->session->userdata('group') == '2') {
    $this->load->view('bar');
}
if ((in_array('collection', $this->session->userdata('userApps')) && $services['collection'] === '3') || $this->session->userdata('group') == '2') {
    $this->load->view('collection');
}
if ((in_array('delivery', $this->session->userdata('userApps')) && $services['delivery'] === '3') || $this->session->userdata('group') == '2') {
    $this->load->view('delivery');
}
if ((in_array('booking', $this->session->userdata('userApps')) && $services['booking'] === '3') || $this->session->userdata('group') == '2') {
    $this->load->view('booking');
}
if ((in_array('online', $this->session->userdata('userApps')) && $services['online'] === '3') || $this->session->userdata('group') == '2') {
    $this->load->view('online');
}
/* ----------------------------------------
 * Order list pages 
 * --------------------------------------- */
//$this->load->view('list/all')

/* ---------------------------------------------
 * oprder option pagers
 * --------------------------------------------- */
$this->load->view('options/attributes');
if ($branch['default_ccFee'] > 0) {
    $this->load->view('options/ccfee');
}
$this->load->view('options/cbFee');
$this->load->view('options/convert');
$this->load->view('options/custom_deal');
$this->load->view('options/edit');
$this->load->view('options/iteminfo');
$this->load->view('options/kitchentxt');
$this->load->view('options/misc');
$this->load->view('options/modify');
/* $this->load->view('options/neworder'); */
$this->load->view('options/payment');
if ($this->session->userdata('group') === '5') {
    $this->load->view('options/remoteOrderPayment');
}
$this->load->view('options/quantity');
$this->load->view('options/servicecharge');
$this->load->view('options/specialdiscount');
$this->load->view('options/toppings');
$this->load->view('options/orderinfo');
$this->load->view('options/delivery_charge');
$this->load->view('options/freeItem');
$this->load->view('options/userProfile');
$this->load->view('options/deletePassword');
/* --------------------------------------------
 * All popups content Ends
 * ------------------------------------------- */
?>
<!---Configuration Program Data-->
<?php //debugPrint($terminals) ?>
<input type="hidden" id="categoryPaginationBtnValue" value="0">
<input type="hidden" id="menuPaginationBtnValue" value="0">
<input type="hidden" id="selectionPaginationBtnValue" value="0">
<input type="hidden" id="currentItemPageMenuOrSelection">
<input type="hidden" id="currentItemsCategory" value="">
<input type="hidden" id="currentItemsMenu" value="">
<input type="hidden" id="CategoryOptionType" value="all">
<div class="virtual-keyboard-area">
    <div class="keyboard-fixed-orderpage" style="display: none;"></div>
    <div class="setting-pagination-keyboard" style="display: none"></div>
</div>
<!-- All common js Goes from here -->
<script type="text/javascript">
    $(document).ready(function () {
        eposRelodOnIdleTimeElapse();
        var EposInitialCurrentOrderId = zamanEposCart.getEposCurrentId();
        if (EposInitialCurrentOrderId) {
            zamanEposDb.epos_order.get({orderId: Number(EposInitialCurrentOrderId)}).then(function (anOrder) {
                manageOrderListDisplay(anOrder, true);
            });
        } else {
            manageOrderListDisplay({}, true);
        }
    });
    function sleep(time) {
        return new Promise((resolve) => setTimeout(resolve, time));
    }
    function getRndInteger(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    var CheckUserDataForApi =<?php echo $this->session->userdata('group') ?>;
    var hasCallerId = '<?php (in_array('callerId', $this->session->userdata('userApps')) && $services['callerId'] === '3' && (!empty($terminals) && in_array('callerDevice', $terminals))) ? print 'yes' : 'no' ?>';
    var hasOnlineOrder = '<?php in_array('online', $this->session->userdata('userApps')) && $services['online'] === '3' ? print'yes' : print'no' ?>';
    var hasOnlineBooking = '<?php in_array('booking', $this->session->userdata('userApps')) && $services['booking'] === '3' ? print'yes' : print'no' ?>';
    var isTheUserAppReceiver = '<?php in_array('appReceiver', $this->session->userdata('userApps')) && $services['appReceiver'] === '3' ? print'yes' : print'no' ?>';
    if (hasCallerId === 'yes' || hasOnlineOrder === 'yes' || hasOnlineBooking === 'yes' || isTheUserAppReceiver === 'yes') {
        sleep(getRndInteger(1000, 13000)).then(function () {
            updatePendingIssues();
        });
    }
    function updatePendingIssues() {
        $.ajax({
            url: '<?php echo site_url('epos/pendingIssues') ?>',
            type: "GET",
            dataType: 'json',
            success: function (issues) {
                if (hasCallerId === 'yes') {
                    updateCallerIdIssue(issues.pendingCallerId);
                }
                if (hasOnlineBooking === 'yes') {
                    updateBookingIssue(issues.pendingBooking);
                }
                if (hasOnlineOrder === 'yes') {
                    updateOnlineOrderIssue(issues.pendingOnlineOrder);
                }
                if (isTheUserAppReceiver === 'yes') {
                    updatePendingAppOrder(issues.pendingAppOrder)
                }
            }
        });
        sleep(getRndInteger(3000, 5000)).then(function () {
            updatePendingIssues();
        });
    }
    function updateBookingIssue(issue) {
        if ($('#onlineBookingNotification').length > 0 && Number(issue) > 0) {
            $('#onlineBookingNotificationBtn').addClass('flash-button');
            $('#onlineBookingNotification').text(issue);
            $('#onlineBookingNotification').addClass('qty-badge');
            playBeepSound();
        } else {
            $('#onlineBookingNotification').text('');
            $('#onlineBookingNotification').removeClass('qty-badge');
            $('#onlineBookingNotificationBtn').removeClass('flash-button');
        }
    }
    function updateOnlineOrderIssue(issue) {
        if ($('#onlineNotification').length > 0 && Number(issue) > 0) {
            $('#onlineNotificationBtn').addClass('flash-button');
            $('#onlineNotification').text(issue);
            $('#onlineNotification').addClass('qty-badge');
            palyNewOnlineOrderWarningAudio();
        } else {
            $('#onlineNotification').text('');
            $('#onlineNotification').removeClass('qty-badge');
            $('#onlineNotificationBtn').removeClass('flash-button');
        }
    }
    function updatePendingAppOrder(pendingAppOrder) {
        if ($('#pendingAppOrder').length > 0) {
            $('#pendingAppOrder').text(pendingAppOrder);
            $('#pendingAppOrder').addClass('qty-badge');
        } else {
            $('#pendingAppOrder').removeClass('qty-badge');
        }
    }
    function settleDownNewOrderOption() {
        $('#itemNumPad').hide();
        if (ifUserCanCreateOrderInEpos()) {
            var userData = zamanEposCart.getUserData();
            var userApps = userData.userApps;
            var preveleges = userApps.length;
            if (preveleges > 0) {
                if (Number(preveleges) === 1) {
                    if (userApps.includes('table')) {
                        zamanPlaceTableOrder(0, '', '', '', '');
                    }
                    if (userApps.includes('waiting')) {
                        zamanPlaceWaitingOrder(0, '', '', '', '');
                    }
                    if (userApps.includes('bar')) {
                        zamanPlaceBarOrder(0, '', '', '', '');
                    }
                    if (userApps.includes('collection')) {
                        zamanPlaceCollectionOrder(0, '', '', '', '', '');
                    }
                    if (userApps.includes('delivery')) {
                        zamanPlaceDeliveryOrder(0, '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
                    }
                    if (userApps.includes('booking')) {
                        zamanBookingOrderInfoControllerPopup(0, '', '', '', '', '', '', '', '', '');
                    }
                    playBeepSound();
                } else {
                    chooseOrderType();
                    playBeepSound();
                }
            }
        } else {
            warning('Opps', 'You are not belongs to this team to create any order here', 'Close');
        }
    }
    function saveEposOrder(orderInfo, type) {
        var userData = zamanEposCart.getUserData();
        zamanEposDb.epos_branch.get({'id': userData.branch}).then(function (branchObj) {
            if (orderInfo.hasOwnProperty('status')) {
                delete orderInfo.status;
            }
            var dateObj = new Date();
            var year = dateObj.getFullYear();
            var month = dateObj.getMonth() + 1;
            var day = dateObj.getDate();
            var hours = dateObj.getHours();
            var minutes = dateObj.getMinutes();
            var seconds = dateObj.getSeconds();
            if (Number(dateObj.getHours()) >= 0 && Number(dateObj.getHours()) <= 3) {
                if (dateObj.getMinutes() > 0 || dateObj.getSeconds() > 0) {
                    day -= 1;
                }
            }
            var OrderData = {
                dateInfo: {year: year, month: month, day: day, hours: hours, minutes: minutes, seconds: seconds},
                cid: 0,
                owner: '<?php echo $this->session->userdata('owner') ?>',
                team: '<?php echo $this->session->userdata('team') ?>',
                branch: '<?php echo $this->session->userdata('branch') ?>',
                status: '1',
                source: 'epos',
                type: type,
                cart: [],
                firstTakenAt: dateObj.toLocaleTimeString(),
                lastTakenAt: dateObj.toLocaleTimeString(),
                subTotal: 0,
                tax: 0,
                deliveryCharge: type === 'delivery' ? orderInfo.deliveryCharge : 0,
                serviceCharge: 0,
                ccFee: 0,
                cbFee: 0,
                discount: 0,
                promo: 0,
                multiDiscount: 0,
                grandTotal: 0,
                payment: [],
                onlinePaymentDetails: [],
                orderInfo: orderInfo,
                isPaid: 2,
                changeAmount: 0,
                changeAsTips: 2,
                sid: 0,
                taxIsIncluded: branchObj.taxIsIncluded,
                taxIncludedPercent: branchObj.default_tax
            };
            zamanEposDb.epos_order.add(OrderData).then(function (orderId) {
                if (orderId > 0) {
                    zamanEposDb.epos_order.get({'orderId': orderId}).then(function (theOrder) {
                        zamanEposPopupcloser();
                        zamanEposCart.setEposCurrentId(theOrder.orderId);
                        if (zamanEposCart.getEposCurrentId() > 0) {
                            zamanEposCart.setPromoDiscountPlan();
                            zamanEposCart.setDiscountmulti();
                            zamanEposCart.setItemOffer();
                            if (theOrder.type === 'delivery') {
                                zamanEposCart.setDeliveryCharge(theOrder.deliveryCharge);
                            }
                            if (theOrder.type === 'table') {
                                zamanEposCart.setServiceCharge(branchObj.default_sercharge, 2);
                            }
                            eposLoadOrder(zamanEposCart.getEposCurrentId(), 'yes');
                            displayEposCart();
                            hideKeyBoard();
                        } else {
                            alert('not yet set');
                        }
                    });

                }
            });
        });

    }
    function eposLoadOrder(orderId, justPlaced) {
        if (orderId > 0) {
            if (Number(orderId) !== Number(zamanEposCart.getEposCurrentId())) {
                zamanEposCart.setEposCurrentId(orderId);
            }
            if (zamanEposCart.getEposCurrentId() > 0) {
                var isToShowAllOrders = $('#isToShowAllOrders').val();
                zamanEposDb.epos_order.get({orderId: Number(orderId)}).then(function (anOrder) {
                    zamanEposPopupcloser();
                    displayEposCart();
                    if (typeof (anOrder) !== 'undefined') {
                        if (Number(anOrder.status) === 1 && justPlaced === 'yes') {
                            if (isToShowAllOrders === 'yes') {
                                auxiliaryFunctionToShowAllTypeOrders(anOrder, true);
                            } else {
                                auxiliaryFunctionToShowMonoTypeOrders(anOrder, true);
                            }
                        } else {
                            auxiliaryFunctionForShowLoadedOrderInfo(anOrder, true);
                        }
                        playBeepSound();
                    }
                });
            } else {
                warning('<?php echo $this->lang->line('warning') ?>', 'Order not yet set', '<?php echo $this->lang->line('close') ?>');
            }
        }
    }
    function auxiliaryFunctionToShowAllTypeOrders(anOrder, isTrue) {
        if (anOrder.type === 'table' && isTrue) {
            $('#loadedOrderInformation').text('Tab : ' + anOrder.orderInfo.tableNumber);
        } else if (anOrder.type === 'waiting' && isTrue) {
            $('#loadedOrderInformation').text('Wait: ' + anOrder.orderInfo.name);
        } else if (anOrder.type === 'bar' && isTrue) {
            $('#loadedOrderInformation').text('Bar :' + anOrder.orderInfo.name);
        } else if (anOrder.type === 'collection' && isTrue) {
            $('#loadedOrderInformation').text('Col : ' + anOrder.orderInfo.name);
        } else if (anOrder.type === 'delivery' && isTrue) {
            $('#loadedOrderInformation').text('Del : ' + anOrder.orderInfo.name);
        } else {
            $('#loadedOrderInformation').text('<?php echo $this->lang->line('void_order') ?>');
        }
        $('.page-loader').hide();
        zamanOrderListPageController();
    }
    function auxiliaryFunctionToShowMonoTypeOrders(anOrder, isTrue) {
        var userData = zamanEposCart.getUserData();
        if (!jQuery.isEmptyObject(userData)) {
            var userApps = userData.userApps;
            if (userApps.length > 0) {
                if (anOrder.type === 'table' && userApps.includes('table')) {
                    $('#loadedOrderInformation').text('Tab : ' + anOrder.orderInfo.tableNumber);
                    listTableOrder('no');
                } else if (anOrder.type === 'waiting' && userApps.includes('waiting')) {
                    $('#loadedOrderInformation').text('Wait : ' + anOrder.orderInfo.name);
                    listWaitingOrder('no');
                } else if (anOrder.type === 'bar' && userApps.includes('bar')) {
                    $('#loadedOrderInformation').text('Bar : ' + anOrder.orderInfo.name);
                    listBarOrder('no');
                } else if (anOrder.type === 'collection' && userApps.includes('collection')) {
                    $('#loadedOrderInformation').text('Col : ' + anOrder.orderInfo.name);
                    listCollectionOrder('no');
                } else if (anOrder.type === 'delivery' && userApps.includes('delivery')) {
                    $('#loadedOrderInformation').text('Del : ' + anOrder.orderInfo.name);
                    listDeliveryOrder('no');
                } else {
                    $('#loadedOrderInformation').text('<?php echo $this->lang->line('void_order') ?>');
                }
            }
            if (!isTrue) {
                $('#loadedOrderInformation').text('<?php echo $this->lang->line('void_order') ?>');
            }
            $('.page-loader').hide();
        }
    }
    function auxiliaryFunctionForShowLoadedOrderInfo(anOrder, isTrue) {
        if (anOrder.type === 'table' && isTrue) {
            $('#loadedOrderInformation').text('Tab : ' + anOrder.orderInfo.tableNumber);
        } else if (anOrder.type === 'waiting' && isTrue) {
            $('#loadedOrderInformation').text('Wait : ' + anOrder.orderInfo.name);
        } else if (anOrder.type === 'bar' && isTrue) {
            $('#loadedOrderInformation').text('Bar : ' + anOrder.orderInfo.name);
        } else if (anOrder.type === 'collection' && isTrue) {
            $('#loadedOrderInformation').text('Col : ' + anOrder.orderInfo.name);
        } else if (anOrder.type === 'delivery' && isTrue) {
            $('#loadedOrderInformation').text('Del : ' + anOrder.orderInfo.name);
        } else {
            $('#loadedOrderInformation').text('<?php echo $this->lang->line('void_order') ?>');
        }
        $('.page-loader').hide();
        displayEposCart();
    }
    function zamanOrderListPageController() {
        $('#placeOrderTypeBlock').hide();
        var userData = zamanEposCart.getUserData();
        if (!jQuery.isEmptyObject(userData)) {
            var userApps = userData.userApps;
            $('.menu-category.menu-button-inner').removeClass('active');
            $('.footer-order-list-button').removeClass('active');
            $('#list-all-orders').addClass('active');
            $('#categoryBreadcurm').hide();
            $('#eposCategoryMenuList').html('').hide();
            $('#eposOrderStatusFlags').show();
            $('#footerOrderNavigation').show();
            $('#EposOrderListArea').html('').show();
            $('#EposOrderListAreaTable').html('').show();
            $('#EposOrderListAreaBar').html('').show();
            $('#EposOrderListAreaWaiting').html('').show();
            $('#EposOrderListAreaCollection').html('').show();
            $('#EposOrderListAreaDelivery').html('').show();
            if (userApps.includes('table')) {
                listTableOrder('yes');
            }
            if (userApps.includes('waiting')) {
                listWaitingOrder('yes');
            }
            if (userApps.includes('bar')) {
                listBarOrder('yes');
            }
            if (userApps.includes('collection')) {
                listCollectionOrder('yes');
            }
            if (userApps.includes('delivery')) {
                listDeliveryOrder('yes');
            }
            playBeepSound();
        }
    }
    function clearEposWorkArea() {
        $('#eposOrderStatusFlags').hide();
        $('#footerOrderNavigation').hide();
        $('#EposOrderListArea').html('').hide();
        $('#EposOrderListAreaTable').html('').hide();
        $('#EposOrderListAreaBar').html('').hide();
        $('#EposOrderListAreaWaiting').html('').hide();
        $('#EposOrderListAreaCollection').html('').hide();
        $('#EposOrderListAreaDelivery').html('').hide();
        $('#categoryBreadcurm').show();
        $('#eposCategoryMenuList').show();
    }
    function clearEposWorkAreaForOrderList(showing) {
        $('#categoryBreadcurm').hide();
        $('#eposCategoryMenuList').html('').hide();
        $('#eposOrderStatusFlags').show();
        $('#footerOrderNavigation').show();
        $('#EposOrderListArea').html('').hide();
        $('#EposOrderListAreaTable').html('').hide();
        $('#EposOrderListAreaBar').html('').hide();
        $('#EposOrderListAreaWaiting').html('').hide();
        $('#EposOrderListAreaCollection').html('').hide();
        $('#EposOrderListAreaDelivery').html('').hide();
        $('#' + showing).show();
    }
    function manageOrderListDisplay(theOrder, isTrue) {
        var isToShowAllOrders = $('#isToShowAllOrders').val();
        if (isToShowAllOrders === 'yes') {
            auxiliaryFunctionToShowAllTypeOrders(theOrder, isTrue);
        } else {
            auxiliaryFunctionToShowMonoTypeOrders(theOrder, isTrue);
        }
        auxiliaryFunctionForShowLoadedOrderInfo(theOrder, isTrue);
        displayEposCart();
        $('.page-loader').hide();
    }
    function listAppOrder() {
        if (checkInternetConnection()) {
            $.ajax({
                url: '<?php echo site_url('api/getPushedOrder') ?>',
                type: 'GET',
                dataType: 'json',
                beforeSend: function (xhr) {
                    $('.page-loader').show();
                },
                success: function (orders) {
                    $('.page-loader').hide();
                    $('#placeOrderTypeBlock').hide();
                    clearEposWorkAreaForOrderList();
                    if (orders.allOrders.length > 0) {
                        var tableHtml = '';
                        if (orders.table.length > 0) {
                            orders.table.forEach(function (tableOrder) {
                                var isPaid = Number(tableOrder.isPaid) === 3 ? '(<?php echo $this->lang->line('paid') ?>)' : '&nbsp;';
                                tableHtml += `<div class="pane-content">
                                <div class="zaman-nav-item-pane">
                                    <div class="order-item-img-block pull-left table-bg order-process-status-` + tableOrder.status + `" onclick="paymentForRemoteOrder(` + tableOrder.sid + `,'` + tableOrder.orderId + `','` + tableOrder.grandTotal + `')">
                                        <p style="padding-top:2px; line-height:10px;">` + tableOrder.orderInfo.tableNumber + `<br/>` + isPaid + `<br/><img class="table-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=" style="width:25px"><br/>` + tableOrder.orderInfo.guestQty + `</p>   
                                    </div>
                                 </div>
                            </div>`;
                            });
                            tableHtml += `<div class="clearfix">&nbsp;</div>`;
                            $('#EposOrderListAreaTable').html(tableHtml).show();
                        }
                        if (orders.bar.length > 0) {
                            var barHtml = '';
                            orders.bar.forEach(function (barOrder) {
                                var isPaid = Number(barOrder.isPaid) === 3 ? '(<?php echo $this->lang->line('paid') ?>)' : '&nbsp;';
                                barHtml += `<div class="pane-content">
                                    <div class="zaman-nav-item-pane">
                                        <div class="order-item-img-block pull-left bar-bg order-process-status-` + barOrder.status + `" onclick="paymentForRemoteOrder(` + barOrder.sid + `,'` + barOrder.orderId + `','` + barOrder.grandTotal + `')">
                                            <p class="text-capitalize" style="line-height:0.9;">` + isPaid + `<br/><img class="bar-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC8AAAAvAQMAAABJ6QHvAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgFVAEAAUkAAf+U2WcAAAAASUVORK5CYII=" style="width:30px"><br/>` + barOrder.orderInfo.name + `</p>        
                                        </div>
                                    </div>
                                </div>`;
                            });
                            barHtml += `<div class="clearfix">&nbsp;</div>`;
                            $('#EposOrderListAreaBar').html(barHtml).show();
                        }
                        if (orders.waiting.length > 0) {
                            var waitingHtml = '';
                            orders.waiting.forEach(function (waitingOrder) {
                                var isPaid = Number(waitingOrder.isPaid) === 3 ? '(<?php echo $this->lang->line('paid') ?>)' : '&nbsp;';
                                waitingHtml += `<div class="pane-content">
                                        <div class="zaman-nav-item-pane">
                                            <div class="order-item-img-block pull-left waiting-bg order-process-status-` + waitingOrder.status + `" onclick="paymentForRemoteOrder(` + waitingOrder.sid + `,'` + waitingOrder.orderId + `','` + waitingOrder.grandTotal + `')">
                                              <p class="text-capitalize" style="line-height:10px;">` + isPaid + `<br/> <img class="waiting-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=" style="width:30px"><br/>` + waitingOrder.orderInfo.name + `</p>
                                           </div>
                                        </div>           
                                    </div>`;
                            });
                            waitingHtml += `<div class="clearfix">&nbsp;</div>`;
                            $('#EposOrderListAreaWaiting').html(waitingHtml).show();
                        }
                        if (orders.collection.length > 0) {
                            var collectionHtml = '';
                            orders.collection.forEach(function (collectionOrder) {
                                var isPaid = Number(collectionOrder.isPaid) === 3 ? '(<?php echo $this->lang->line('paid') ?>)' : '&nbsp;';
                                collectionHtml += `<div class="pane-content">
                                    <div class="zaman-nav-item-pane">
                                        <div class="order-item-img-block pull-left collection-bg order-process-status-` + collectionOrder.status + `" onclick="paymentForRemoteOrder(` + collectionOrder.sid + `,'` + collectionOrder.orderId + `','` + collectionOrder.grandTotal + `')">
                                            <p class="text-capitalize" style="line-height:0.9;">` + isPaid + `<br/><img class="collection-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=" style="width:30px;"><br/>` + collectionOrder.orderInfo.name + `<br/>` + collectionOrder.orderInfo.collectionTime + `</p>
                                        </div>
                                    </div>
                                </div>`;
                            });
                            collectionHtml += `<div class="clearfix">&nbsp;</div>`;
                            $('#EposOrderListAreaCollection').html(collectionHtml).show();
                        }
                        if (orders.delivery.length > 0) {
                            var deliveryHtml = '';
                            orders.delivery.forEach(function (deliveryOrder) {
                                var isPaid = Number(deliveryOrder.isPaid) === 3 ? '(<?php echo $this->lang->line('paid') ?>)' : '&nbsp;';
                                deliveryHtml += `<div class="pane-content">
                                <div class="zaman-nav-item-pane">
                                    <div class="order-item-img-block pull-left delivery-bg order-process-status-` + deliveryOrder.status + `" onclick="paymentForRemoteOrder(` + deliveryOrder.sid + `,'` + deliveryOrder.orderId + `','` + deliveryOrder.grandTotal + `')">
                                        <p class="text-capitalize" style="line-height:0.9;">` + isPaid + `<br/> <img class="delivery-icon" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAAA1BMVEX///+nxBvIAAAAAXRSTlMAQObYZgAAAA1JREFUeNpjYBgF1AQAAVAAATS4OtkAAAAASUVORK5CYII=" style="width:36px; height:30px"><br/>` + deliveryOrder.orderInfo.name + `<br/>` + deliveryOrder.orderInfo.deliveryTime + `</p>
                                    </div>
                                </div>
                              </div>`;
                            });
                            deliveryHtml += `<div class="clearfix">&nbsp;</div>`;
                            $('#EposOrderListAreaDelivery').html(deliveryHtml).show();
                        }
                    }
                }
            });
        } else {
            warning('<?php echo $this->lang->line('lost_connection') ?>', '<?php echo $this->lang->line('lost_connection_message') ?>', '<?php echo $this->lang->line('close') ?>');
        }
    }
    function paymentForRemoteOrder(sid, orderId, grandTotal) {
        var total = Number(Number(grandTotal).toFixed(2));
        remoteOrderPaymentProcessor(sid, orderId, total);
    }
    function sendCommandToEpos(command) {
        var message = new Array();
        message['exitApp'] = "<?php echo $this->lang->line('application_getting_down') ?>";
        message['reloadApp'] = "<?php echo $this->lang->line('application_getting_refreshed') ?>";
        if (command === 'exitApp') {
            $.confirm({
                title: '<?php echo $this->lang->line('warning') ?>',
                message: '<?php echo $this->lang->line('exit_app_warning') ?>',
                buttons: {
                    '<?php echo $this->lang->line('yes') ?>': {
                        'class': 'primary',
                        'action': function () {
                            $.ajax({
                                url: '<?php echo site_url('api/sendCommandToEpos/') ?>' + command,
                                type: 'GET',
                                dataType: 'json',
                                beforeSend: function () {
                                    if (command === 'exitApp' || command === 'reloadApp') {
                                        warning('<?php echo $this->lang->line('warning') ?>', message[command], '<?php echo $this->lang->line('ok') ?>');
                                    } else {
                                        $('.page-loader').show();
                                    }
                                },
                                success: function (response) {
                                    $('.page-loader').hide();
                                }
                            });
                        }
                    },
                    '<?php echo $this->lang->line('no') ?>': {
                        'class': 'positive',
                        'action': function () { }
                    },
                }
            });
        } else {
            $.ajax({
                url: '<?php echo site_url('api/sendCommandToEpos/') ?>' + command,
                type: 'GET',
                dataType: 'json',
                beforeSend: function () {
                    if (command === 'exitApp') {
                        warning('<?php echo $this->lang->line('warning') ?>', message[command], '<?php echo $this->lang->line('ok') ?>');
                    } else {
                        $('.page-loader').show();
                    }
                },
                success: function (response) {
                    if (command === 'reloadApp') {
                        window.location.reload(true);
                    } else {
                        $('.page-loader').hide();
                    }
                }
            });
        }
    }
    function postupdateForExtraCharge(curentId, column, value) {
        zamanEposDb.epos_order.get({orderId: Number(curentId)}).then(function (anOrder) {
            if (Number(anOrder.status) >= 2) {
                var getEposCartNewQty = zamanEposCart.getEposCartNewQty(curentId);
                var cart = zamanEposCart.listCart();
                var numberOfUnsentItem = 0;
                cart.forEach(function (item) {
                    if (item.sent === 'no') {
                        numberOfUnsentItem += Number(item.qty);
                    }
                });
                if (numberOfUnsentItem <= 0 || Number(getEposCartNewQty) <= 0) {
                    var cartCalculator = zamanEposCart.cartCalculator();
                    $.ajax({
                        url: '<?php echo site_url('api/postupdateForExtraCharge') ?>',
                        data: {sid: anOrder.sid, column: column, value: value, grandTotal: cartCalculator.Total},
                        dataType: 'json',
                        type: 'POST',
                        success: function (response) {
                            if (response.status) {
                                zamanEposPopupcloser();
                            }
                        }
                    });
                }
            }
        });
    }
    function eposRelodOnIdleTimeElapse() {
        var t;
        window.onload = EposResetTimer;
        window.onmousemove = EposResetTimer;
        window.onmousedown = EposResetTimer;
        window.onclick = EposResetTimer;
        window.onscroll = EposResetTimer;
        window.onkeypress = EposResetTimer;
        function relodeEpos() {
            if (checkInternetConnection()) {
                location.reload();
            }
        }
        function EposResetTimer() {
            clearTimeout(t);
            t = setTimeout(relodeEpos, 900000);
        }
    }
</script>
