<?php
// Copyright 1999-2024. WebPros International GmbH. All rights reserved.
?>

<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {
    var operations = [];

    operations.push({
        componentType: 'Jsw.SmallButton',
        cls: 's-btn sb-add-new-subscription btn-primary',
        title: <?php echo $this->jsLmsg('buttonAddNew'); ?>,
        description: <?php echo $this->jsLmsg('buttonAddNewDescription'); ?>,
        href: '/web/add-domain/create-webspace/true'
    });

    <?php if ($this->showMyResources): ?>
        operations.push({
            componentType: 'Jsw.SmallButton',
            cls: 's-btn sb-resources',
            title: <?php echo $this->jsLmsg('buttonMyResources'); ?>,
            description: <?php echo $this->jsLmsg('buttonMyResourcesDescription'); ?>,
            href: '/account/my-resources'
        });
    <?php endif; ?>

    operations.push({
        componentType: 'Jsw.SmallButton',
        cls: 's-btn sb-remove-selected',
        title: <?php echo $this->jsLmsg('buttonRemove'); ?>,
        description: <?php echo $this->jsLmsg('buttonRemoveDescription'); ?>,
        handler: function(event) {
            Jsw.getComponent('subscriptions-list').execGroupOperation({
                url: '/web/subscription-delete/?redirect=/web/subscriptions',
                subtype: 'delete',
                mouseEvent: event,
                locale: {
                    confirmOnGroupOperation: <?php echo $this->subscriptionsList->jsLmsg('confirmOnDelete'); ?>
                },

                isAjax: <?php echo $this->isHomonymSearchEnabled ? 'true' : 'false' ?>,
                requestUrl: '/account/search-homonym-data/',
                loadingTitle: <?php echo $this->jsLmsg('components.homonyms.loading'); ?>,

                toJsonPayload: true,
                submitHandler: async (url, payload) => {
                    url = Jsw.prepareUrl('/web/subscription-delete-async/');
                    const { task } = await Jsw.api.post(url, { ids: payload.ids, redirectUrl: '/web/subscriptions/' });
                    if (task && task.componentType === 'Jsw.Task.ProgressBar.Item') {
                        Jsw.getComponent('asyncProgressBarWrapper').progressDialog(task);
                    }
                },
            });
        }
    });


    new Jsw.Panel({
        cls: 'list-box',
        renderTo: 'main',
        items: [{
            componentType: 'Jsw.List',
            id: 'subscriptions-list',
            data: <?php echo Zend_Json::encode($this->subscriptionsList->fetchData()); ?>,
            dataUrl: '/web/subscriptions-list-data',
            columns: [
                Jsw.list.COLUMN_SELECTION,
                {
                    header: <?php echo $this->subscriptionsList->jsLmsg('subscriptionPowerUser');?>,
                    sortable: true,
                    dataIndex: 'domain',
                    renderer: function(item) {
                        var subscriptionName = item.domain;
                        if (item.systemUser) {
                            var returnUrl = Jsw.prepareUrl('/web/edit');
                            subscriptionName = '<a href="' + Jsw.prepareUrl('/account/switch/id/' + item.id + '?hideNotice=1&returnUrl=' + encodeURIComponent(returnUrl)) + '">'
                                + item.domain + '</a>';
                        }

                        return subscriptionName;
                    }
                }, {
                    header: <?php echo $this->subscriptionsList->jsLmsg('systemUser'); ?>,
                    sortable: true,
                    dataIndex: 'systemUser'
                }, {
                    header: <?php echo $this->subscriptionsList->jsLmsg('ipAddress'); ?>,
                    sortable: true,
                    dataIndex: 'ipAddress'
                }, {
                    header: <?php echo $this->subscriptionsList->jsLmsg('addonDomains'); ?>,
                    dataIndex: 'addonDomains',
                    renderer: function(item) {
                        var list = '';

                        (item.addonDomains || []).forEach(function(name) {
                            list += name + '<br/>';
                        });

                        return list;
                    }
                }, {
                    header: <?php echo $this->subscriptionsList->jsLmsg('diskSpace'); ?>,
                    dataIndex: 'diskSpaceUsed'
                }
            ],
            operations: operations,
            isDisabledItem: function(item) {
                return (item.id == 0);
            }
        }]
    });

});
//]]>
</script>
