<ul class="list-group">
    <li class="list-group-item">
        <?php echo form_open('gkpos/menumanagement/savemealdealset', array('id' => 'mealDealSetForm_' . $thisSet[0]->deal)) ?>
        <input type="hidden" name="id" value="<?php echo $thisSet[0]->id ?>"/>
        <input type="hidden" name="deal" value="<?php echo $thisSet[0]->deal ?>"/>
        <table class="table table-responsive table-bordered">
            <tr>
                <td class="text-right">Set Title</td>
                <td><input class="form-control" type="text" name="set_title" value="<?php echo $thisSet[0]->title ?>"></td>
            </tr>
            <tr>
                <td class="text-right">How many items customer can choose from this set?</td>
                <td><input class="form-control" type="number" min="1" name="allowed_selection" value="<?php echo $thisSet[0]->allowed ?>"></td>
            </tr>
            <tr>
                <td class="text-right">Is Published</td>
                <td>
                    <div class="radio-inline">
                        <label class="radio-inline text-uppercase" style='color: #000080'><input type="radio" name="status"  value="1" <?php $thisSet[0]->status == 1 ? print "checked='checked'" : print'' ?>><?php echo $this->lang->line('gkpos_yes') ?></label>
                        <label class="radio-inline text-uppercase" style='color: #000080'><input type="radio" name="status"  value="0" <?php $thisSet[0]->status == 0 ? print "checked='checked'" : print'' ?>><?php echo $this->lang->line('gkpos_no') ?></label>
                    </div>
                </td>
            </tr>
            <?php if (!empty($set_category)): ?>
                <tr>
                    <td colspan="2">
                        <?php foreach ($set_category as $stc): ?>
                            <?php if (!empty($menus)): ?>
                                <?php
                                $setCatMenus = $menus[$stc];
                                $len = count($setCatMenus);
                                $setCatFirstHalfMenus = array_slice($setCatMenus, 0, floor($len / 2));
                                $setCatSecondHalfMenus = array_slice($setCatMenus, $len / 2);
                                ?>
                                <h3 style="border-bottom: 2px dotted #000000;"><?php echo ucwords(strtolower(get_gkcategory_name($stc))) ?></h3>
                                <div class="col-lg-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5 text-left">
                                    <?php if (!empty($setCatFirstHalfMenus)): ?>
                                        <?php foreach ($setCatFirstHalfMenus as $menu): ?>
                                            <div class="checkbox">
                                                <label style="color: #000;">
                                                    <?php if (empty($selections[$menu->category][$menu->id])): ?>
                                                        <?php $checked = get_single_record('meal_deal_menu', array('deal' => $thisSet[0]->deal, 'set' => $thisSet[0]->id, 'cat' => $stc, 'menu' => $menu->id)) ?>
                                                        <input type="checkbox" <?php !empty($checked) == true ? print"checked='checked'" : print'' ?>  class="menu-options" name="menus[][<?php echo $menu->category ?>]" value="<?php echo $menu->id ?>">
                                                        <span class="title"><?php echo $menu->title; ?></span>
                                                        <span class="price"><strong>[<?php echo $this->config->item('currency_symbol') ?>
                                                                <span class="price-amount"><?php $menu->out_price > 0 ? print $menu->out_price : print $menu->base_price ?></span>]</strong>
                                                        </span>
                                                    <?php else: ?>
                                                        <?php $checked = get_single_record('meal_deal_menu', array('deal' => $thisSet[0]->deal, 'set' => $thisSet[0]->id, 'cat' => $stc, 'menu' => $menu->id)) ?>
                                                        <input type="checkbox" <?php !empty($checked) == true ? print"checked='checked'" : print'' ?>  class="menu-options" name="menus[][<?php echo $menu->category ?>]" value="<?php echo $menu->id ?>"><span class="title"><?php echo $menu->title; ?></span>
                                                    <?php endif; ?>
                                                </label>
                                                <?php if (!empty($selections[$menu->category][$menu->id])): ?>
                                                    <?php foreach ($selections[$menu->category][$menu->id] as $selection): ?>
                                                        <?php $checked = get_single_record('meal_deal_selection', array('deal' => $thisSet[0]->deal, 'set' => $thisSet[0]->id, 'cat' => $stc, 'menu' => $menu->id, 'selection' => $selection->id)) ?>
                                                        <div class="checkbox" style="margin-left: 20px;">
                                                            <label style="color: #000;">
                                                                <input type="checkbox" <?php !empty($checked) == true ? print"checked='checked'" : print'' ?> name='selection[][<?php echo $menu->category ?>][<?php echo $menu->id ?>]' value="<?php echo $selection->id ?>">
                                                                <span class="title"><?php echo $selection->title; ?></span>
                                                                <span class="price"><strong>[<?php echo $this->config->item('currency_symbol') ?>
                                                                        <span class="price-amount"><?php $menu->out_price > 0 ? print $selection->out_price : print $selection->base_price ?></span>]</strong>
                                                                </span>
                                                            </label>
                                                        </div>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </div>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </div>
                                <div class="col-lg-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5 text-left">
                                    <?php if (!empty($setCatSecondHalfMenus)): ?>
                                        <?php foreach ($setCatSecondHalfMenus as $menu): ?>
                                            <div class="checkbox">
                                                <label style="color: #000;">
                                                    <?php if (empty($selections[$menu->category][$menu->id])): ?>
                                                        <?php $checked = get_single_record('meal_deal_menu', array('deal' => $thisSet[0]->deal, 'set' => $thisSet[0]->id, 'cat' => $stc, 'menu' => $menu->id)) ?>
                                                        <input type="checkbox" <?php !empty($checked) == true ? print"checked='checked'" : print'' ?>  class="menu-options" name="menus[][<?php echo $menu->category ?>]" value="<?php echo $menu->id ?>">
                                                        <span class="title"><?php echo $menu->title; ?></span>
                                                        <span class="price"><strong>[<?php echo $this->config->item('currency_symbol') ?>
                                                                <span class="price-amount"><?php $menu->out_price > 0 ? print $menu->out_price : print $menu->base_price ?></span>]</strong>
                                                        </span>
                                                    <?php else: ?>
                                                        <?php $checked = get_single_record('meal_deal_menu', array('deal' => $thisSet[0]->deal, 'set' => $thisSet[0]->id, 'cat' => $stc, 'menu' => $menu->id)) ?>
                                                        <input type="checkbox" <?php !empty($checked) == true ? print"checked='checked'" : print'' ?>  class="menu-options" name="menus[][<?php echo $menu->category ?>]" value="<?php echo $menu->id ?>"><span class="title"><?php echo $menu->title; ?></span>
                                                    <?php endif; ?>
                                                </label>
                                                <?php if (!empty($selections[$menu->category][$menu->id])): ?>
                                                    <?php foreach ($selections[$menu->category][$menu->id] as $selection): ?>
                                                        <?php $checked = get_single_record('meal_deal_selection', array('deal' => $thisSet[0]->deal, 'set' => $thisSet[0]->id, 'cat' => $stc, 'menu' => $menu->id, 'selection' => $selection->id)) ?>
                                                        <div class="checkbox" style="margin-left: 20px;">
                                                            <label style="color: #000;">
                                                                <input type="checkbox" <?php !empty($checked) == true ? print"checked='checked'" : print'' ?> name='selection[][<?php echo $menu->category ?>][<?php echo $menu->id ?>]' value="<?php echo $selection->id ?>">
                                                                <span class="title"><?php echo $selection->title; ?></span>
                                                                <span class="price"><strong>[<?php echo $this->config->item('currency_symbol') ?>
                                                                        <span class="price-amount"><?php $menu->out_price > 0 ? print $selection->out_price : print $selection->base_price ?></span>]</strong>
                                                                </span>
                                                            </label>
                                                        </div>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </div>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </div>
                            <?php else: ?>
                                <p>No menus found to create a set for meal deal</p>
                            <?php endif; ?>
                            <div class="clearfix"></div>
                        <?php endforeach; ?>
                    </td>
                </tr>
            <?php endif; ?>
            <tr>
                <td colspan="2">
                    <div class="multi-field-wrapper">
                        <div class="multi-fields">
                            <div class="multi-field" id="catBox-0">
                                <span>Add More Category:</span><select id="catOption-0" name="stuff[]" style="width: 250px; height: 30px;" onchange="getData(this.id)">
                                    <option value="0"></option>
                                    <?php foreach ($MenuCategories as $menuCat): ?>
                                        <option value="<?php echo $deal . '_' . $menuCat->id ?>"><?php echo $menuCat->title ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <button type="button" class="remove-field btn btn-danger"><i class="fa fa-minus"></i></button>
                                <div class="clearfix"></div>
                                <div class="category-menu-loader" id="categoryMenuLoader-0"></div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                        <div class="clearfix"></div>
                        <button type="button" class="add-field btn btn-success" style="width: 250px; margin-left: 75px;" >Add field</button>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">  <input class="btn btn-danger" style="width: 100%; margin: 0 auto; padding: 20px" tabindex="4" value="<?php echo $this->lang->line('gkpos_numpad_key_enter') ?>" onclick="saveMealDealSet('mealDealSetForm_<?php echo $deal ?>')"></td></td>
            </tr>
        </table>
        <?php echo form_close() ?>
    </li>
</ul>
<script>
    $(document).ready(function () {
        addRemoveWrapper();
    });
    function addRemoveWrapper() {
        $('.multi-field-wrapper').each(function () {
            var index = 0;
            var $wrapper = $('.multi-fields', this);
            $(".add-field", $(this)).click(function (e) {
                index++;
                $('.multi-field:first-child', $wrapper).clone(true).prop({id: "catBox-" + index}).appendTo($wrapper).find('select').attr({id: 'catOption-' + index}).val('').end().find(' .category-menu-loader').attr({id: 'categoryMenuLoader-' + index}).html('');
            });
            $('.multi-field .remove-field', $wrapper).click(function () {
                if ($('.multi-field', $wrapper).length > 1)
                    $(this).parent('.multi-field').remove();
            });
        });
    }
    function getData(id) {
        var deal_category = $('#' + id).val();
        if (deal_category == '0') {
            alert('please select a category');
            return false;
        } else {
            var selectBox = id.split('-');
            var index = selectBox[1];
            var catBox = 'catBox-' + index;
            var loaderBox = 'categoryMenuLoader-' + index;
            $.ajax({
                url: '<?php echo site_url('gkpos/menumanagement/pupulate') ?>',
                type: 'POST',
                data: {
                    deal_category: deal_category
                },
                success: function (output) {
                    $('#' + loaderBox).html(output);
                }
            });
        }
    }
    function saveMealDealSet(setForm) {
        var myform = document.getElementById(setForm);
        var formData = new FormData(myform);
        var set_title = $.trim(formData.get('set_title'));
        var allowed = $.trim(formData.get('allowed_selection'));
        if (!set_title || set_title === null) {
            alert('Set title is required');
            return false;
        } else if (Number(allowed) <= 0) {
            alert('please provide positive integer value > 0 for maximum selection');
            return false;
        } else {
            $.ajax({
                url: '<?php echo site_url('gkpos/menumanagement/savemealdealset') ?>',
                data: formData,
                cache: false,
                processData: false,
                contentType: false,
                type: 'POST',
                dataType: 'json',
                success: function (output) {
                    if (output.success) {
                        alert(output.message)
                        getMenumanagementPage('<?php echo site_url('gkpos/menumanagement/mealdeal') ?>', 'Mealdeal');
                    } else {
                        alert(output.message)
                    }
                }
            });
        }
    }
</script>