<div style="display: none;">
    <div id="deleteSendItemPasscodeReceiver">
        <div class="popup-header toolbar-header">
            <div class="popup-title"><?php echo $this->lang->line('password') ?></div>
            <div class="popup-close-btn" onclick="zamanEposPopupcloser()"><span class="icon icon-cancel"></span></div>
        </div>
        <div class="popup-content">
            <form id="orderItemDeletePasscodeForm">
                <div class="neworder-table-guest collection-phone ccfee">
                    <div class="form-group">
                        <input type="password" name="passcode" id="zamanPasscodePrimaryNumpad" class="form-control" placeholder="<?php echo $this->lang->line('password') ?>">
                        <input type="hidden" id="deletedItemLine">
                        <input type="hidden" id="deletedItemStatus">
                        <input type="hidden" id="deletedItemAction">
                        <input type="hidden" id="deleteType">
                    </div>
                    <div class="clearfix"></div>
                    <div class="tab-group keypad">
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">1</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">2</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">3</div>
                    </div>
                    <div class="tab-group keypad">
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">4</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">5</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">6</div>
                    </div>
                    <div class="tab-group keypad">
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">7</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">8</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">9</div>
                    </div>
                    <div class="tab-group keypad">
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryActionKey">DEL</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryNumKey">0</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key passCodePrimaryActionKey">CLR</div>
                    </div>
                    <div class="tab-group config-submit">
                        <button type="submit" onclick="checkAndValidatePasscode('orderItemDeletePasscodeForm')" class="text-capitalize"><?php echo $this->lang->line('confirm') ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    function zamanOrderItemDeletePasscodeControllerPopup(line, status, action, type) {
        zamanEposDb.epos_order.get({orderId: Number(zamanEposCart.getEposCurrentId())}).then(function (anOrder) {
            if (typeof (anOrder) !== 'undefined') {
                if (Number(anOrder.isPaid) === 3) {
                    orderPaymentPreProcessor(anOrder);
                } else {
                    $.colorbox({
                        inline: true,
                        scrolling: false,
                        href: "#deleteSendItemPasscodeReceiver",
                        width: '290px',
                        height: 'auto',
                        maxWidth: "100%",
                        open: true,
                        closeButton: false,
                        transition: 'none',
                        overlayClose: false,
                        className: 'transparent-overlay',
                        onOpen: function () {
                            resetForm('orderItemDeletePasscodeForm');
                        },
                        onLoad: function () {
                            $('#deletedItemLine').val(line);
                            $('#deletedItemStatus').val(status);
                            $('#deletedItemAction').val(action);
                            $('#deleteType').val(type);
                        },
                        onComplete: function () {

                        },
                        onCleanup: function () {
                        },
                        onClosed: function () {
                            resetForm('orderPasscodeForm');
                        }
                    });
                }
            } else {
                warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('load_order_in_cart') ?>', '<?php echo $this->lang->line('ok') ?>');
            }
        });
    }
    function checkAndValidatePasscode(formId) {
        $('#' + formId).validate({
            submitHandler: function () {
                var passcode = $('#zamanPasscodePrimaryNumpad').val();
                if (passcode === '4321') {
                    zamanEposPopupcloser();
                    var line = $('#deletedItemLine').val();
                    var status = $('#deletedItemStatus').val();
                    var action = $('#deletedItemAction').val();
                    var activeLine = '';
                    var type = $('#deleteType').val();
                    if (type === 'item') {
                        if (action === 'remove') {
                            zamanEposCart.deleteItem(Number(line), status);
                        }
                        if (action === 'minus') {
                            activeLine = zamanEposCart.minusItem(line, status);

                        }
                        displayEposCart();
                        setCartLine(activeLine);
                    } else {
                        zamanDeleteCartVoidOrderConfirmByPassword(line);
                    }

                } else {
                    warning('<?php echo $this->lang->line('warning') ?>', '<?php echo sprintf($this->lang->line('invalid_value'), $this->lang->line('password')) ?>', '<?php echo $this->lang->line('close') ?>');
                }
            },
            errorClass: "has-error",
            highlight: function (e) {
                $(e).closest('.form-group').addClass('has-error');
                $.colorbox.resize();
            },
            unhighlight: function (e) {
                $(e).closest('.form-group').removeClass('has-error');
                $.colorbox.resize();
            },
            rules: {
                passcode: {required: true}
            },
            errorPlacement: function (error, element) {
                errorPlacement(error, element);
            }
        });
    }
    function zamanDeleteCartVoidOrderConfirmByPassword(id) {
        zamanEposDb.epos_order.get({'orderId': Number(id)}).then(function (theOrder) {
            if (theOrder.type === 'table') {
                zamanEposDb.epos_table.where("tableNumber").equals(theOrder.orderInfo.tableNumber).modify({
                    status: '2'
                });
            }
            zamanEposDb.epos_order.delete(theOrder.orderId).then(function () {
                $.ajax({
                    url: '<?php echo site_url('report/deleteOrder/') ?>' + theOrder.sid,
                    type: 'Get',
                    dataType: 'json',
                    beforeSend: function () {
                        $('.page-loader').show();
                    },
                    success: function (response) {
                        if (response.status) {
                            zamanEposCart.clearOnPayAndClose();
                            var isTrue = false;
                            var isToShowAllOrders = $('#isToShowAllOrders').val();
                            if (isToShowAllOrders === 'yes') {
                                auxiliaryFunctionToShowAllTypeOrders(theOrder, isTrue);
                            } else {
                                auxiliaryFunctionToShowMonoTypeOrders(theOrder, isTrue);
                            }
                            auxiliaryFunctionForShowLoadedOrderInfo(theOrder, isTrue);
                            displayEposCart();
                        } else {
                            warning('<?php echo $this->lang->line('error') ?>', 'Deleting order failed! Please try again', '<?php echo $this->lang->line('close') ?>');
                        }
                    }
                });
            });
        });
    }
</script>