<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 dt-info">
    <div class="head padding-bottom">
        <div class="dt-header">
            <div class="dt-header-left">
                <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="dt-filters">
                     <div class="filter-group">
                        <select id="addon-list-filter" class="normalSelect2 w-auto">
                        <?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>

            <div class="dt-header-right">
                <div class="filter-group dt-length"></div>
            </div>
        </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="pill danger"
                type="button"
                id="delete-button"
                disabled="disabled"
            >
                <i class="fas fa-trash"></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 data-cfasync="false">

    $(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": [
                {
                    "className": 'details-control',
                    "orderable": false,
                    "data": null,
                    "defaultContent": ''
                },
                { 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 () {
            $('#delete-button').prop(
                'disabled',
                table.rows({ selected: true }).count() === 0
            );
        });

        $('#table-list tbody').on('click', 'td.details-control', function () {
            var tr = $(this).closest('tr');
            var row = table.row(tr);

            if (row.child.isShown()) {
                row.child.hide();
                tr.removeClass('shown');
            }
            else {
                var record = row.data();
                var expanderBody = "<div style='padding-left:23px;padding-bottom:10px;'>";
                expanderBody += "<strong>"+clientexec.lang("Description")+":</strong> " + clientexec.unhtmlentities(record.description);
                expanderBody += "<br/><strong>"+clientexec.lang("Plugin Variable")+":</strong> " + record.plugin_var_name.replace("CUSTOM_","");
                expanderBody += "<br/><strong>"+clientexec.lang("Used in products")+":</strong>";
                fetchRowDetails(row.data()).then(function (json) {
                    data = clientexec.parseResponse(json);
                    if (data.products.length > 0) {
                        $.each(data.products,function(index, product) {
                            expanderBody = expanderBody + "<div>"+product.productname+" "+"("+product.productid+")</div>";
                        });
                    } else {
                        expanderBody += '<div>'+ clientexec.lang("Not used in any products") +"</div>";
                    }
                    expanderBody = expanderBody +  "</div>";

                    row.child(expanderBody).show();
                    tr.addClass('shown');
                });
            }
        });

        function fetchRowDetails(row) {
            return new Promise((resolve, reject) => {
                $.get("index.php?fuse=admin&controller=addons&action=admingetaddonsused", { addonid:row.id }, function(data) {})
                .done(resolve)
                .fail(reject);
            });
        }

    });
</script>







































