<div style="display: none;">
    <div id="specialDiscountContent">
        <div class="popup-header toolbar-header">
            <div class="popup-title">Special discount</div>
            <div class="popup-close-btn" onclick="zamanEposPopupcloser()"><span class="icon icon-cancel"></span></div>
        </div>
        <div class="popup-content">
            <div class="neworder-table-guest collection-phone ccfee">
                <form id="orderSpecialDisocuntForm">
                    <div class="form-group">
                        <input type="text" name="discount" id="orderSpecialDisocunt" class="form-control" placeholder="Special discount">
                        <input type="hidden" name="func" id="orderSpecialDisocuntFunction" value="3" class="form-control">
                        <input type="hidden" name="type" id="orderSpecialDisocuntOnType" value="all" class="form-control">
                    </div>
                    <div class="tab-group text-center radio-checkbox-optiongroup">
                        <div class="tab-item discount-amount-type fixed" onclick="setDiscountAmountType('3')"><?php echo $this->lang->line('fixed') ?></div>
                        <div class="tab-item discount-amount-type percent" onclick="setDiscountAmountType('2')"><?php echo $this->lang->line('percent') ?></div>
                    </div>
                    <div class="tab-group text-center radio-checkbox-optiongroup">
                        <div class="tab-item discount-amount-on all" onclick="setDiscountApplyOn('all')"><?php echo $this->lang->line('all') ?></div>
                        <div class="tab-item discount-amount-on kitchen" onclick="setDiscountApplyOn('kitchen')"><?php echo $this->lang->line('kitchen') ?></div>
                        <!--<div class="tab-item discount-amount-on bar" onclick="setDiscountApplyOn('bar')"><?php echo $this->lang->line('bar') ?></div>
                        <div class="tab-item discount-amount-on bakery" onclick="setDiscountApplyOn('bakery')"><?php echo $this->lang->line('bakery') ?></div>-->
                        <div class="tab-item discount-amount-on drinks" onclick="setDiscountApplyOn('drinks')"><?php echo $this->lang->line('drinks') ?></div>
                    </div>
                    <div class="clearfix"></div>
                    <div class="tab-group keypad">
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">1</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">2</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">3</div>
                    </div>
                    <div class="tab-group keypad">
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">4</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">5</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">6</div>
                    </div>
                    <div class="tab-group keypad">
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">7</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">8</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">9</div>
                    </div>
                    <div class="tab-group keypad">
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">.</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryNumKey">0</div>
                        <div class="tab-item zaman-epos-nav quantity-digit keypad-key specialDiscountPrimaryActionKey">CLR</div>
                    </div>
                    <div class="tab-group config-submit">
                        <button type="submit" onclick="addOrderSpecialDisocunt('orderSpecialDisocuntForm')"><span class="icon icon-folder"></span>&nbsp;<?php echo $this->lang->line('add') ?></button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    function zamanOrderSpecialDiscountControllerPopup() {
        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: "#specialDiscountContent",
                        width: '320px',
                        height: 'auto',
                        maxWidth: "100%",
                        maxHeight: "95%",
                        open: true,
                        closeButton: false,
                        transition: 'none',
                        overlayClose: false,
                        className: 'transparent-overlay',
                        onOpen: function () {
                            playBeepSound();
                            resetForm('orderSpecialDisocuntForm');
                        },
                        onLoad: function () {
                            var specialDiscount = zamanEposCart.getSpecialDiscount();
                            //console.log(specialDiscount);
                            if (specialDiscount) {
                                //console.log('here-1 to me');
                                if (!jQuery.isEmptyObject(specialDiscount) || specialDiscount !== null || typeof (specialDiscount) !== 'undefined') {
                                    $('#orderSpecialDisocunt').val(specialDiscount.discount);
                                    $('#orderSpecialDisocuntFunction').val(specialDiscount.func);
                                    $('#orderSpecialDisocuntOnType').val(specialDiscount.type);
                                    setDiscountAmountType(specialDiscount.func);
                                    setDiscountApplyOn(specialDiscount.type);
                                }
                            } else {
                                //console.log('here-2 to me');
                                var userData = zamanEposCart.getUserData();
                                zamanEposDb.epos_branch.get({'id': userData.branch}).then(function (branch) {
                                    if (typeof (branch) !== 'undefined') {
                                        $('#orderSpecialDisocunt').val('');
                                        $('.discount-amount-type').removeClass('active');
                                        $('.discount-amount-type.percent').addClass('active');
                                        $('.discount-amount-on').removeClass('active');
                                        $('.discount-amount-on.all').addClass('active');
                                        setDiscountAmountType('3');
                                        setDiscountApplyOn('all');
                                    }
                                });
                            }
                        },
                        onComplete: function () {
                        },
                        onCleanup: function () {
                        },
                        onClosed: function () {
                            resetForm('orderSpecialDisocuntForm');
                        }
                    });
                }
            } else {
                warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('load_order_in_cart') ?>', '<?php echo $this->lang->line('ok') ?>');
            }
        });

    }
    function setDiscountAmountType(type) {
        console.log('type' + type);
        $('#orderSpecialDisocuntFunction').val(type);
        if (Number(type) === 2) {
            $('.discount-amount-type').removeClass('active');
            $('.discount-amount-type.percent').addClass('active');
        } else {
            $('.discount-amount-type').removeClass('active');
            $('.discount-amount-type.fixed').addClass('active');
        }
    }
    function setDiscountApplyOn(type) {
        $('#orderSpecialDisocuntOnType').val(type);
        if (type === 'kitchen') {
            $('.discount-amount-on').removeClass('active');
            $('.discount-amount-on.kitchen').addClass('active');
        } else if (type === 'bar') {
            $('.discount-amount-on').removeClass('active');
            $('.discount-amount-on.bar').addClass('active');
        } else if (type === 'bakery') {
            $('.discount-amount-on').removeClass('active');
            $('.discount-amount-on.kitchen').addClass('active');
        } else if (type === 'drinks') {
            $('.discount-amount-on').removeClass('active');
            $('.discount-amount-on.drinks').addClass('active');
        } else {
            $('.discount-amount-on').removeClass('active');
            $('.discount-amount-on.all').addClass('active');
        }
    }
    function addOrderSpecialDisocunt(formId) {
        $('#' + formId).validate({
            submitHandler: function () {
                var disocunt = parseFloat(Number($('#orderSpecialDisocunt').val()));
                var func = Number($('#' + formId + ' input[name=func]').val());
                var type = $('#' + formId + ' input[name=type]').val();
                var access = true;
                if (Number(func) === 3) {
                    var cartSubTotal = zamanEposCart.cartSubtotal();
                    if (Number(cartSubTotal) < Number(disocunt)) {
                        warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('invalid_amount_for_discount') ?>', '<?php echo $this->lang->line('close') ?>');
                        access = false;
                    } else {
                        access = true;
                    }
                }
                if (access) {
                    zamanEposCart.setSpecialDiscount(disocunt, func, type);
                    displayEposCart();
                    zamanEposPopupcloser();
                    postupdateForExtraCharge(zamanEposCart.getEposCurrentId(), 'discount', zamanEposCart.cartCalculator().specialDiscount);
                }
            },
            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: {
                discount: {number: true, required: true},
                func: {number: true, required: true},
                type: {number: true, required: true}
            },
            errorPlacement: function (error, element) {
                errorPlacement(error, element);
            }
        });
    }
</script>