<div class="col-lg-10 col-md-10 col-sm-10 col-xs-10 bodyitem no-padding">
    <fieldset class='text-uppercase'>
        <input type="checkbox" class="checkbox-inline" id="showHideAddNewMealDealbox" onclick="checkBlock(this.id, 'AddNewMealDealbox')">
        <span style="color: #FFF; padding-top: 5px; "><?php echo "Add new meal deal" ?></span>
        <div class="row" id="AddNewMealDealbox">
            <?php echo form_open('gkpos/menumanagement/savemealdeal', array('class' => 'form-horizontal', 'id' => 'addNewMealDealForm')) ?>
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" style="margin:10px 0px;">
                <div class="form-group form-group-sm">	
                    <?php echo form_label('Name', 'title', array('class' => 'control-label col-lg-5 col-md-5 col-sm-5 col-xs-5 text-capitalize required')); ?>
                    <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7">
                        <div class="input-group">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input name="title" class="form-control required"  type="text" id="title" value="<?php isset($thisMealDeal) ? print $thisMealDeal[0]->title : print'' ?>">
                        </div>
                    </div>
                </div>
                <?php if (isset($thisMealDeal)): ?>
                    <input type="hidden" name="id" value="<?php echo $thisMealDeal[0]->id ?>">
                <?php endif ?>
                <div class="form-group form-group-sm">	
                    <?php echo form_label('Price', 'price', array('class' => 'control-label col-lg-5 col-md-5 col-sm-5 col-xs-5 text-capitalize required')); ?>
                    <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7">
                        <div class="input-group">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa" aria-hidden="true"><?php echo $this->config->item('currency_symbol') ?></i></a></span>
                            <input name="price" class="form-control required"  type="text" id="price" value="<?php isset($thisMealDeal) ? print $thisMealDeal[0]->price : print'' ?>">
                        </div>
                    </div>
                </div>
                <div class="form-group form-group-sm">	
                    <?php echo form_label('How Many Free Toppings', 'free_toppings', array('class' => 'control-label col-lg-5 col-md-5 col-sm-5 col-xs-5 text-capitalize')); ?>
                    <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7">
                        <div class="input-group">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input name="free_toppings" class="form-control"  type="text" id="free_toppings" value="<?php isset($thisMealDeal) ? print $thisMealDeal[0]->free_toppings : print'' ?>">
                        </div>
                    </div>
                </div>
                <div class="form-group form-group-sm">	
                    <?php echo form_label('Max Toppings', 'gk_name', array('class' => 'control-label col-lg-5 col-md-5 col-sm-5 col-xs-5 text-uppercase')); ?>
                    <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7">
                        <div class="input-group">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input name="max_toppings" class="form-control"  type="text" id="max_toppings" value="<?php isset($thisMealDeal) ? print $thisMealDeal[0]->max_toppings : print'' ?>">
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" style="margin:10px 0px;">
                <div class="form-group form-group-sm">	
                    <?php echo form_label('Deal Image', 'image', array('class' => 'control-label col-lg-4 col-md-4 col-sm-4 col-xs-4 text-uppercase')); ?>
                    <div class='col-lg-8 col-md-8 col-sm-8 col-xs-8'>
                        <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 200px;">
                            <img data-src="holder.js/100%x100%" alt='deal image'
                                 src="<?php
                                 if (isset($thisMealDeal) && $thisMealDeal[0]->image != null)
                                     echo UPLOAD_PATH . 'mealdeal/' . $thisMealDeal[0]->image;
                                 else
                                     echo '';
                                 ?>"
                                 style="max-height: 100%; max-width: 100%;">
                        </div>
                        <div>
                            <span class="btn btn-default btn-sm btn-file">
                                <input type="file" name="image">
                            </span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <?php echo form_label('Description', 'content', array('class' => 'control-label col-lg-4 col-md-4 col-sm-4 col-xs-4 text-uppercase')); ?>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                        <textarea class="form-control" rows="3" id="content" name="content"><?php isset($thisMealDeal) ? print $thisMealDeal[0]->content : print'' ?></textarea>
                    </div>
                </div>
            </div>
            <div class="form-group-sm" style="margin-top: 10px;">
                <ul id="addNewMealDealFormError" class="error_message_box clearfix"></ul>
                <input class="btn btn-danger" style="width: 100%; margin: 0 auto; padding: 20px" type="submit" name="submit_form" tabindex="4" value="<?php echo $this->lang->line('gkpos_numpad_key_enter') ?>" onclick="saveMealDealData('addNewMealDealForm', 'addNewMealDealFormError')">
                <?php echo form_close() ?>
            </div>
        </div>
    </fieldset>
    <?php if (!empty($allDeals)): ?>
        <div id="MealDealAccordion" style="margin: 0px 0px; border-radius: 5px;min-height: 30px">
            <?php foreach ($allDeals as $deal): ?>
                <div class="mealdeal_panel" id="<?php echo $deal->id . '_' . $deal->order ?>">
                    <h3><?php echo $deal->title ?></h3>
                    <div>
                        <div id="mealDealBlock_<?php echo $deal->id ?>">
                            <table class="table tab-content table-bordered table-responsive table-bg-color-white">
                                <tr>
                                    <td>
                                        <table class="table table-responsive">
                                            <tr>
                                                <th class="text-center">Description</th>
                                                <th class="text-center">Price</th>
                                                <th class="text-center">Free Toppings</th>
                                                <th class="text-center">Max Toppings</th>
                                                <th class="text-center">Status</th>
                                                <th class="text-center">Bogo</th>
                                                <th class="text-center">Toppings</th>
                                                <th class="text-center">Action</th>
                                            </tr>
                                            <tr>
                                                <td><?php echo $deal->content ?></td>
                                                <td class="text-center"><?php echo to_currency($deal->price) ?></td>
                                                <td class="text-center"><?php echo $deal->free_toppings ?></td>
                                                <td class="text-center"><?php echo $deal->max_toppings ?></td>
                                                <td class="text-center"><?php $deal->status == 1 ? print 'Published' : 'Unpublished' ?></td>

                                                <td class="text-center">
                                                    <a span style="color:#000000;" href="javascript:void(0)" onclick="editdealbogo('<?php echo"mealdeal_bogo_" . $deal->id ?>')" data-type="select" id="mealdeal_bogo_<?php echo $deal->id ?>" data-name="bogo" data-pk="<?php echo $deal->id ?>" data-url="<?php echo site_url('gkpos/menumanagement/editdealcell') ?>" data-value="<?php echo $deal->bogo ?>" data-title="<?php echo "Update Buy one get one" ?>">
                                                        <span style="color:#000000;"><?php $deal->bogo == 1 ? print $this->lang->line('gkpos_yes') : print $this->lang->line('gkpos_no') ?></span>
                                                    </a>
                                                </td>
                                                <td class="text-center"><a href="javascript:void(0)" onclick="openTopingBox('<?php echo $deal->id ?>', 'meal_deal', 'meal_deal', '0')" style="color: #000000">Open</a></td>
                                                <td class="text-center">
                                                    <a style="color: #003399;" href="javascript:void(0)" onclick="getMenumanagementPage('<?php echo site_url('gkpos/menumanagement/mealdeal/' . $deal->id) ?>', 'Update Mealdeal')">Update</a>
                                                    <a style="color: #003399;" href="javascript:void(0)" onclick="deleteMealdeal('<?php echo $deal->id ?>')">Delete</a>
                                                </td>
                                            </tr>

                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center"><a href="javascript:void(0)" style="color: #003399;" onclick="dealContentAddingBlock('<?php echo $deal->id ?>')"><span class="glyphicon glyphicon-plus text-uppercase" style="cursor:pointer">create set</span></a></td>
                                </tr>
                                <tr id="dealContentAddingBlock_<?php echo $deal->id ?>" style="display: none;">
                                    <td class="text-center" id="dealSetContent_<?php echo $deal->id ?>"></td>
                                </tr>

                                <tr id="dealContentBlock_<?php echo $deal->id ?>">
                                    <td>
                                        <?php if (!empty($deal_sets[$deal->id])): ?>
                                            <table class="table table-responsive">
                                                <tr class="table-bg-color table-bordered" style="color: #ffffff;">
                                                    <th>Set Title</th>

                                                    <th class="text-center">Number of items customer can grab</th>
                                                    <th class="text-center">Status</th>
                                                    <th class="text-center">Edit</th>
                                                </tr>
                                                <?php foreach ($deal_sets[$deal->id] as $set): ?>
                                                    <tr class="table-bg-color-white" style="color: #000000;">
                                                        <td><?php echo $set->title ?></td>
                                                        <td class="text-center"><?php echo $set->allowed ?></td>
                                                        <td class="text-center"><?php $set->status == 1 ? print 'published' : print 'unpublished' ?></td>
                                                        <td class="text-center"><a href="javascript:void(0)" style="color:#003399;cursor: pointer" onclick="editSet('<?php echo $deal->id ?>', '<?php echo $set->id ?>')">View And Update</a></td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </table>
                                        <?php else: ?>
                                            No set yet created under this deal
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
            <div class="clearfix" style="margin-bottom: 5px; padding-bottom: 50px;"></div>
        </div>
    <?php endif ?>
</div>
<div id="TopingToolBoxHolder"></div>
<script>
    $(document).ready(function () {
<?php if (!empty($thisMealDeal)) { ?>
            $('#showHideAddNewMealDealbox').click();
<?php } ?>
        $('#MealDealAccordion').accordion({
            collapsible: true,
            active: false,
            height: 'fill',
            header: 'h3',
            autoHeight: false,
        }).sortable({
            items: '.mealdeal_panel',
            cursor: "move",
            update: function (event, ui) {
                var data = $(this).sortable('serialize');
                $.post("<?php echo site_url('gkpos/menumanagement/mealdealsort') ?>", {data: data}, function (output) {
                    console.log(output);
                }, 'json');
            }
        });
        $('#MealDealAccordion').on('accordionactivate', function (event, ui) {
            if (ui.newPanel.length) {
                $('#MealDealAccordion').sortable('disable');
            } else {
                $('#MealDealAccordion').sortable('enable');
            }
        });
        if ($('#showHideAddNewMealDealbox').is(":checked")) {
            $('#AddNewMealDealbox').show();
        } else {
            $('#AddNewMealDealbox').hide();
        }
    });


    function editdealbogo(field) {
        
        $.fn.editable.defaults.mode = 'inline';
        $('#' + field).editable({
            source: function () {
                if (field.includes("bogo")) {
                    return [
                        {value: 1, text: '<?php echo $this->lang->line('gkpos_yes') ?>'},
                        {value: 0, text: '<?php echo $this->lang->line('gkpos_no') ?>'}
                    ]
                }
            }
        });
    }

    function saveMealDealData(formId, error_message_box) {
        $('#' + formId).validate({
            submitHandler: function () {
                var myform = document.getElementById(formId);
                var formdata = new FormData(myform);
                $.ajax({
                    url: $('#' + formId).attr('action'),
                    data: formdata,
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'POST',
                    dataType: 'json',
                    success: function (output) {
                        if (output.success) {
                            getMenumanagementPage('<?php echo site_url('gkpos/menumanagement/mealdeal') ?>', 'Meal Deal')
                        } else {
                            alert(output.message);
                        }
                    }
                });
            },
            errorClass: "has-error",
            errorLabelContainer: "#" + error_message_box,
            wrapper: "li",
            highlight: function (e) {
                $(e).closest('.form-group').addClass('has-error clearfix');
            },
            unhighlight: function (e) {
                $(e).closest('.form-group').removeClass('has-error clearfix');
            },
            rules:
                    {
                        title: 'required',
                        price: {
                            number: true,
                            required: true
                        },
                        free_toppings: {
                            number: true
                        },
                        max_toppings: {
                            number: true
                        }
                    },
            messages:
                    {
                        title: "Meal Deal title is a required field",
                        price: "Meal Deal price is mandatory",
                        free_toppings: "Please specify free toppings only in mumber",
                        max_toppings: "Please specify max toppings only in mumber"
                    }
        });
    }
    function dealContentAddingBlock(id) {
        $.ajax({
            url: '<?php echo site_url('gkpos/menumanagement/add_set_block') ?>',
            type: 'POST',
            data: {
                deal: id
            },
            success: function (output) {
                $('#dealContentAddingBlock_' + id).show();
                $('#dealSetContent_' + id).html(output);
            }
        });
    }

    function getMenuSelection(id, block) {
        var deal_category = $('#' + id).val();
        $.ajax({
            url: '<?php echo site_url('gkpos/menumanagement/pupulate') ?>',
            type: 'POST',
            data: {
                deal_category: deal_category
            },
            success: function (output) {
                $('#' + block).html(output);
            }
        });
    }
    function editSet(deal, set) {
        $.ajax({
            url: '<?php echo site_url('gkpos/menumanagement/edit_set') ?>',
            type: 'POST',
            data: {
                id: set,
                deal: deal
            },
            success: function (output) {
                $('#dealContentAddingBlock_' + deal).show();
                $('#dealSetContent_' + deal).html(output);
            }
        });
    }
    function updateDealTopping(deal_id, top_id, top_category) {
        if ($('#top_' + deal_id + '_' + top_id).is(":checked")) {
            $.ajax({
                url: '<?php echo site_url('gkpos/menumanagement/deal_toppings') ?>',
                type: 'POST',
                data: {
                    meal_deal: deal_id,
                    toppings: top_id,
                    top_cat: top_category,
                    action: 'insert'
                },
                dataType: 'json',
                beforeSend: function () {
                    $('#dealTopLoader_' + deal_id + '_' + top_id).show();
                },
                success: function (output) {
                    if (output.success) {
                        $('#dealTopLoader_' + deal_id + '_' + top_id).hide();
                    }

                }

            });
        } else {
            $.ajax({
                url: '<?php echo site_url('gkpos/menumanagement/deal_toppings') ?>',
                type: 'POST',
                data: {
                    meal_deal: deal_id,
                    toppings: top_id,
                    action: 'delete',
                    top_cat: top_category,
                },
                dataType: 'json',
                beforeSend: function () {
                    $('#dealTopLoader_' + deal_id + '_' + top_id).show();
                },
                success: function (output) {
                    if (output.success) {
                        $('#dealTopLoader_' + deal_id + '_' + top_id).hide();
                    }
                }
            });
        }
    }
    function deleteMealdeal(id) {
        $.ajax({
            url: '<?php echo site_url('gkpos/menumanagement/mealdeal_delete') ?>',
            type: 'POST',
            dataType: 'json',
            data: {
                id: id
            },
            success: function (output) {
                if (output.success) {
                    getMenumanagementPage('<?php echo site_url('gkpos/menumanagement/mealdeal') ?>', 'Meal Deal')
                } else {
                    alert(output.message);
                }
            }
        });
    }


</script>