<div style="display: none;">
    <div id="menuFixedDealPopup" class="popup-attributes">
        <div class="popup-header toolbar-header">
            <div class="popup-title">
                <?php
                $AllStatus = array('2' => 'Inactive', '3' => 'Active');
                $heading = null;
                if ($set !== '0') {
                    $heading = get_title('menuset', array('id' => $set));
                } else if ($selection !== '0') {
                    $heading = get_title('menuselection', array('id' => $selection)) . '-' . get_title('menu', array('id' => $menu));
                } else {
                    $heading = get_title('menu', array('id' => $menu));
                }
                echo ucwords($heading . ' ' . $this->lang->line('fixed_deal'));
                ?>

            </div>
            <div class="popup-close-btn" onclick="zamanEposPopupcloser()"><span class="icon icon-cancel"></span></div>
        </div>
        <div class="popup-content">
            <div class="page-loader popup-page-loader" style="display: none;"></div>
            <div class="clearfix">
                <div class="caption"><?php echo $this->lang->line('fixed_deal_items') ?></div>
            </div>
            <div class="popup-block">
                <?php if ($this->session->userdata('group') <= 4): ?>
                    <?php echo form_open('menu/save_fixeddealitem', array('id' => 'fixedDealItemForm')); ?>
                <?php endif; ?>
                <div>
                    <table id="fixedDealElement">
                        <tr>
                            <td class="text-lef"><?php echo $this->lang->line('name') ?></td>
                            <td class="text-center"><?php echo $this->lang->line('price') ?> (<?php echo $this->lang->line('base') ?>-<?php echo $this->lang->line('in') ?>-<?php echo $this->lang->line('out') ?>)</td>
                            <td class="text-center"><?php echo $this->lang->line('qty') ?></td>
                            <td class="text-center"><?php echo $this->lang->line('delete') ?></td>
                        </tr>
                        <?php if (!empty($fixedDealItems)): ?>
                            <?php foreach ($fixedDealItems as $fdi): ?>
                                <tr>
                                    <?php if ($fdi->selection > 0): ?>
                                        <?php $thisItem = get_single_record('menuselection', array('id' => $fdi->selection)) ?>
                                        <td class="text-lef"><?php echo $thisItem->title . '-' . get_title('menu', array('id' => $thisItem->menu)) ?></td>
                                        <td class="text-center"><?php echo $thisItem->base_price . '-' . $thisItem->in_price . '-' . $thisItem->out_price ?></td>
                                        <td class="text-center">
                                            <a href="javascript:void(0)" onclick="updatefixedheaditemcell('<?php echo "fixedHeadItem_qty_" . $fdi->id ?>')" data-type="text" id="fixedHeadItem_qty_<?php echo $fdi->id ?>" data-name="qty" data-pk="<?php echo $fdi->id ?>" data-url="<?php echo site_url('menu/updatefixedheaditemcell') ?>" data-title="Update Quantity">
                                                <?php echo $fdi->qty ?>
                                            </a>
                                        </td>
                                        <td class="text-center"><span id="FDI-<?php echo $fdi->id ?>" class="icon icon-trash" onclick="deleteFDIitem('<?php echo $fdi->id ?>')"></span></td>
                                    <?php else: ?>
                                        <?php $thisItem = get_single_record('menu', array('id' => $fdi->menu)) ?>
                                        <td class="text-lef"><?php echo $thisItem->title ?></td>
                                        <td class="text-center"><?php echo $thisItem->base_price . '-' . $thisItem->in_price . '-' . $thisItem->out_price ?></td>
                                        <td class="text-center">
                                            <a href="javascript:void(0)" onclick="updatefixedheaditemcell('<?php echo "fixedHeadItem_qty_" . $fdi->id ?>')" data-type="text" id="fixedHeadItem_qty_<?php echo $fdi->id ?>" data-name="qty" data-pk="<?php echo $fdi->id ?>" data-url="<?php echo site_url('menu/updatefixedheaditemcell') ?>" data-title="Update Quantity">
                                                <?php echo $fdi->qty ?>
                                            </a>
                                        </td>
                                        <td class="text-center"><span id="FDI-<?php echo $fdi->id ?>" class="icon icon-trash" onclick="deleteFDIitem('<?php echo $fdi->id ?>')"></span></td>
                                    <?php endif; ?>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </table>
                </div>
                <?php if ($this->session->userdata('group') <= 4): ?>
                    <div class="tab-group config-submit clearfix">
                        <input type="hidden" id="modifierHeadCategory" name="category" value="<?php echo $category ?>">
                        <input type="hidden" id="modifierHeadMenu" name="menu" value="<?php echo $menu ?>">
                        <input type="hidden" id="modifierHeadSelection" name="selection" value="<?php echo $selection ?>">
                        <input type="hidden" id="modifierHeadSet" name="set" value="<?php echo $set ?>">
                        <button type="submit" onclick="saveComboItem('fixedDealItemForm')"><span class="icon icon-folder"></span>&nbsp;<?php echo $this->lang->line('save') ?></button>
                    </div>
                    <?php echo form_close() ?>
                <?php endif; ?>
                <div class="clearfix" style="height: 30px;"></div>
                <div class="caption"><?php echo $this->lang->line('add_new_modifier_head') ?></div>
                <div class="form-group">
                    <label><?php echo $this->lang->line('title') ?></label>
                    <select class="form-control" onchange="getItem(this.value)">
                        <option value="">Select menu</option>
                        <?php if (!empty($menus)): ?>
                            <?php foreach ($menus as $amenu): ?>
                                <?php if (!empty($selections[$amenu->id])): ?>
                                    <?php foreach ($selections[$amenu->id] as $aselection): ?>
                                        <option value="<?php echo $amenu->category . '_' . $amenu->id . '_' . $aselection->id ?>"><?php echo $aselection->title . '-' . $amenu->title ?></option>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <option value="<?php echo $amenu->category . '_' . $amenu->id . '_' . '0' ?>"><?php echo $amenu->title ?></option>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </div>
                <div>
                    <div class="pane-one-half">
                        <div class="caption">Fixed deal Topping</div>
                        <table>
                            <tr>
                                <td><?php echo $this->lang->line('title') ?></td>
                                <td><?php echo $this->lang->line('price') ?></td>
                                <td><?php echo $this->lang->line('qty') ?></td>
                                <td><?php echo $this->lang->line('status') ?></td>
                            </tr>
                            <?php if (!empty($toppingHeads)): ?>
                                <?php foreach ($toppingHeads as $th): ?>
                                    <tr>
                                        <th colspan="3" class="text-center"><strong><?php echo $th->title ?></strong></th>
                                    </tr>
                                    <?php $headToppings = get_record_list('menuattr', array('menuttrcategory' => $th->id)); ?>
                                    <?php if (!empty($headToppings)): ?>
                                        <?php foreach ($headToppings as $ht): ?>
                                            <tr>
                                                <td><?php echo $ht->title ?></td>
                                                <td><?php echo $ht->price ?></td>
                                                <td><?php echo $ht->qty ?></td>
                                                <td><?php echo $AllStatus[$ht->status] ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    <?php endif; ?>

                                <?php endforeach; ?>
                            <?php endif; ?>
                        </table>
                        <div class="tab-group config-submit clearfix">
                            <button type="button" onclick=" attrPopup('menuToppingPopup', '75%', '<?php echo $category ?>', '<?php echo $menu ?>', '<?php echo $selection ?>', '<?php echo $set ?>', '<?php echo site_url('menu/topping') ?>')"><span class="icon icon-tools"></span>&nbsp;<?php echo $this->lang->line('manage') . ' ' . $this->lang->line('toppings') ?></button>
                        </div>
                    </div>
                    <div class="pane-one-half">
                        <div class="caption">Fixed Deal Modifier</div>
                        <table>
                            <tr>
                                <td><?php echo $this->lang->line('title') ?></td>
                                <td><?php echo $this->lang->line('status') ?></td>
                            </tr>
                            <?php if (!empty($modifiersHeads)): ?>
                                <?php foreach ($modifiersHeads as $th): ?>
                                    <tr>
                                        <th colspan="2" class="text-center"><strong><?php echo $th->title ?></strong></th>
                                    </tr>
                                    <?php $headToppings = get_record_list('modifiermenu', array('modcategory' => $th->id)); ?>
                                    <?php if (!empty($headToppings)): ?>
                                        <?php foreach ($headToppings as $ht): ?>
                                            <tr>
                                                <td><?php echo $ht->title ?></td>
                                                <td><?php echo $AllStatus[$ht->status] ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    <?php endif; ?>

                                <?php endforeach; ?>
                            <?php endif; ?>
                        </table>
                        <div class="tab-group config-submit clearfix">
                            <button type="button" onclick=" attrPopup('menuModifierPopup', '75%', '<?php echo $category ?>', '<?php echo $menu ?>', '<?php echo $selection ?>', '<?php echo $set ?>', '<?php echo site_url('menu/itemmodifier') ?>')"><span class="icon icon-tools"></span>&nbsp;<?php echo $this->lang->line('manage') . ' ' . $this->lang->line('modifiers') ?></button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function getItem(combo) {
        if (!$('.page-loader').is(":visible")) {
            if (checkInternetConnection()) {
                $.ajax({
                    url: '<?php echo site_url('menu/getfixeddealitem') ?>',
                    data: {combo: combo},
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $('.page-loader').show();
                    },
                    success: function (response) {
                        if (response.status) {
                            var item = response.item;
                            var title = item.menuTitle ? item.title + '-' + item.menuTitle : item.title;
                            var combo = item.menu ? item.category + '_' + item.menu + '_' + item.id : item.category + '_' + item.id + '_' + '0';
                            $('#fixedDealElement').append('<tr class="newly-added-row"><td class="text-left">' + title + '<input type="hidden" name="items[]" value="' + combo + '"></td><td class="text-center">' + item.base_price + '-' + item.in_price + '-' + item.out_price + '</td><td class="text-center">1</td><td class="text-center"><span class="icon icon-trash" id="combo_' + combo + '"  onclick="deleteComboItem(\'' + combo + '\')"></span></td></tr>');
                            $.colorbox.resize();
                        }
                        $('.page-loader').hide();
                    }
                });
            } else {
                warning('<?php echo $this->lang->line('lost_connection') ?>', '<?php echo $this->lang->line('lost_connection_message') ?>', '<?php echo $this->lang->line('close') ?>');
            }
        }
    }
    function deleteComboItem(ids) {
        $('#combo_' + ids).closest('tr').remove();
        $.colorbox.resize();
    }
    function saveComboItem(formId) {
        if (!$('.page-loader').is(":visible")) {
            $('#' + formId).validate({
                submitHandler: function () {
                    var myform = document.getElementById(formId);
                    var formdata = new FormData(myform);
                    if (checkInternetConnection()) {
                        $.ajax({
                            url: $('#' + formId).attr('action'),
                            data: formdata,
                            cache: false,
                            processData: false,
                            contentType: false,
                            type: 'POST',
                            dataType: 'json',
                            beforeSend: function () {
                                $('.page-loader').show();
                            },
                            success: function (response) {
                                if (response.status) {
                                    attrPopup('menuFixedDealPopup', '75%', '<?php echo $category ?>', '<?php echo $menu ?>', '<?php echo $selection ?>', '<?php echo $set ?>', '<?php echo site_url('menu/fixeddeal') ?>')
                                } else {
                                    warning('<?php echo $this->lang->line('warning') ?>', '<?php echo $this->lang->line('nothing_to_happen') . ' ' . sprintf($this->lang->line('item_already_taken'), $this->lang->line('item')) ?>', '<?php echo $this->lang->line('ok') ?>');
                                }
                                $('.page-loader').hide();
                            }
                        });
                    } else {
                        warning('<?php echo $this->lang->line('lost_connection') ?>', '<?php echo $this->lang->line('lost_connection_message') ?>', '<?php echo $this->lang->line('close') ?>');
                    }
                },
                errorClass: "has-error",
                highlight: function (e) {
                    $(e).closest('.form-group').addClass('has-error');
                },
                unhighlight: function (e) {
                    $(e).closest('.form-group').removeClass('has-error');
                },
                rules: {combo: {required: true}},
                messages: {ombo: '<?php echo $this->lang->line('select_toppings_head') ?>'},
                errorPlacement: function (error, element) {
                    errorPlacement(error, element);
                }
            });
        }
    }
    function deleteFDIitem(id) {
        if (!$('.page-loader').is(":visible")) {
            if (checkInternetConnection()) {
                $.ajax({
                    url: '<?php echo site_url('menu/deletefixeddealitem') ?>',
                    data: {id: id},
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $('.page-loader').show();
                    },
                    success: function (response) {
                        if (response.status) {
                            $('#FDI-' + id).closest('tr').remove();
                            $.colorbox.resize();
                        }
                        $('.page-loader').hide();
                    }
                });
            } else {
                warning('<?php echo $this->lang->line('lost_connection') ?>', '<?php echo $this->lang->line('lost_connection_message') ?>', '<?php echo $this->lang->line('close') ?>');
            }
        }
    }
    function updatefixedheaditemcell(field) {
        $('#' + field).editable({
            mode: 'inline',
            source: function () {
                if (field.includes("status")) {
                    return [
                        {value: 3, text: '<?php echo $this->lang->line('active') ?>'},
                        {value: 2, text: '<?php echo $this->lang->line('inactive') ?>'}
                    ];
                }
                if (field.includes("rel")) {
                    return [
                        {value: 'mono', text: '<?php echo strtolower($this->lang->line('mono')) ?>'},
                        {value: 'poly', text: '<?php echo strtolower($this->lang->line('poly')) ?>'}
                    ];
                }
            },
            validate: function (value) {
                if ($.trim(value) === '') {
                    warning('<?php echo $this->lang->line('error') ?>', '<?php echo $this->lang->line('data_required') ?>', '<?php echo $this->lang->line('close') ?>');
                    return false;
                } else {
                    if (field.includes("qty") || field.includes("max") || field.includes("max")) {
                        if (isNaN(Number(value))) {
                            warning('<?php echo $this->lang->line('error') ?>', '<?php echo $this->lang->line('invalid_data') ?>', '<?php echo $this->lang->line('close') ?>');
                            return false;
                        }
                    }
                }
            }
        });
    }

</script>