<style type="text/css">
#productaddongrid .x-form-checkbox {
    margin-left: 5px;
}
</style>

<script data-cfasync="false" type="text/javascript">
    var admin_edit_packagetypes = <?php echo ($this->user->hasPermission('admin_edit_packagetypes')) ? 1 : 0; ?>;
</script>

<div class="page-title">
    <h1><?php echo $this->user->lang("Product Addons"); ?></h1>
    <span><?php echo $this->user->lang("Addons are additional services/product addons that can be attached to product orders."); ?></span>
</div>

<div class="cm-holder settings-wrapper dt-info">
    <div class="head">
        <button class="action-buttons blue" id="add-button">
            <i class="icon-ic-21"></i>
            <?php echo $this->user->lang('Add Product Addon'); ?>
        </button>
         <div class="filter-block" style="">
            <select id="addon-list-filter" class="normalSelect2 w-25">
                <?php
                    foreach ($this->productgroups as $key=>$cat) {
                        echo "<optgroup value='0' label='".$key."'>";
                        foreach($cat as $productgroup) {
                            $selected = "";
                            if ($productgroup['id'] == $this->selected_group) $selected = "selected";
                            echo "<option value='".$productgroup['id']."' ".$selected.">".$productgroup['name']."</option>";
                        }
                        echo "</optgroup>";
                    }
                ?>
                </select>
        </div>
    </div>
    <div class="dt-wrapper pull-top">
        <div class="table-responsive">
            <table id="table-list" class="display" style="width:100%">
                <thead>
                    <tr>
                        <th></th>
                        <th style="text-align: center">
                            <input type="checkbox" id="checkAll">
                        </th>
                        <th class=""><?php echo $this->user->lang('Name'); ?></th>
                    </tr>
                </thead>
            </table>
        </div>
        <div class="table-actions">
            <button class="action-buttons" id="delete-button" disabled="disabled"><i class="icon-ic-25"></i><?php echo $this->user->lang('Delete'); ?></button>
        </div>
    </div>
</div>

<div class="alert alert-warning mt-3">
  <p class="mb-2">
    <?php echo $this->user->lang("Addons can only belong to a single product type, for example 'Hosting' addons cannot be used by 'Domain' products and vice versa.");?>
  </p>
  <p class="mb-0">
    <?php echo $this->user->lang("Addons can be shared among different products of the same type & changes to one addon will affect all products that use it. Create a new addon if your product requires unique settings for the addon.");?>
  </p>
</div>



<script>

    $(document).ready(function () {
        var ajaxUrl = 'index.php?fuse=admin&controller=addons&action=admingetaddons';
        var table = $('#table-list').DataTable({
            ordering: false,
            select: {
                style: 'multi',
                selector: 'td:nth-child(2)'
            },
            columnDefs: [
                {
                    targets: 0,
                    width: '5%'
                },
                {
                    targets: 1,
                    orderable: false,
                    className: 'select-checkbox',
                    checkboxes: {
                        selectRow: true
                    },
                    width: '5%'
                },
                {
                    targets: 2,
                    width: '90%'
                }
            ],
            "searching": false,
            "ajax": {
                url: ajaxUrl,
                dataSrc: 'addons',
                data: function(d) {
                    d.filter = $('#addon-list-filter').val()
                }
            },
            "columns": [
                { data: null, defaultContent: '', orderable: false, },
                { data: null, defaultContent: '', orderable: false, },
                {
                    "data": "name",
                    render: function (text, type, record) {
                        if ( text == '' ) {
                            text = 'N/A';
                        }
                        text = "<a href='index.php?fuse=admin&controller=addons&view=productaddon&id="+record.id+"'>"+text+"</a>";
                        if (record.plugin_var_name !== "None") {
                            text = text + " <span style='float:right;' class='label label-inverse'>" +clientexec.lang("Plugin Variable")+": " + record.plugin_var_name.replace("CUSTOM_","") +"</span>";
                        }
                        return text;
                    },
                },
            ],
        });

        $('#addon-list-filter').change(function(){
            table.ajax.reload();
        });

        $('#add-button').on('click', function(){
            window.location.href = 'index.php?fuse=admin&controller=addons&view=productaddon';
        });

        $('#table-list').on('click', '.edit-link', function(e) {
            e.preventDefault();
            addEditWindow.show({
                params: {
                    id: $(this).data('id'),
                }
            });
        });

        $('#delete-button').click(function () {
            if ($(this).attr('disabled')) {
                return false;
            }

            RichHTML.msgBox('<b>'+clientexec.lang('NOTICE:')+'</b>'+'<br><br>'+clientexec.lang('Do you want existing clients to continue being charged this addon?'),
            {type:"confirm"}, function(result) {
                if(result.btn === clientexec.lang("Yes")) {

                    $.post("index.php?fuse=admin&controller=addons&action=deleteaddonwithrevalidation", {
                        keeprecurringfees:1,
                        ids: table.getSelectedRows()
                    },
                    function(data){
                        data = clientexec.parseResponse(data);
                        table.ajax.reload();
                    });
                } else if(result.btn === clientexec.lang("No")) {
                    $.post("index.php?fuse=admin&controller=addons&action=deleteaddonwithrevalidation", {
                        keeprecurringfees:0,
                        ids: table.getSelectedRow()
                    },
                    function(data){
                        data = clientexec.parseResponse(data);
                        table.ajax.reload();
                    });
                }
            });
        });

        table.on('select.dt deselect.dt', function() {
            var selectedRows = table.rows({ selected: true }).count();
            if (selectedRows > 0) {
                $('.table-actions .action-buttons').prop('disabled', false);
            } else {
                $('.table-actions .action-buttons').prop('disabled', true);
            }
        });
    });
</script>







































