<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>
<script type="text/javascript">
//<![CDATA[
(function() {
    Jsw.namespace('Smb.Account.Index');
    Smb.Account.Index.onRemove = function (event) {
        Jsw.getComponent('subscriptions-list').execGroupOperation({
            url: '/account/delete/?redirect=/account/',
            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'); ?>
        });
    };

    new Jsw.Hint({
        renderTo: 'main',
        hint: <?php echo $this->jsLmsg('accountOverviewHint'); ?>
    });

    new Jsw.SmallTools({
        renderTo: 'main',
        operations: <?php echo $this->globalOperations; ?>
    });

    var listColumns = (function () {
        var listColumns = [];

        listColumns.push({
            header: <?php echo $this->subscriptionsList->jsLmsg('subscription');?>,
            sortable: true,
            dataIndex: 'domain',
            renderer: function (item) {
                var subscriptionName = '';

                if (item.isMyResources) {
                    subscriptionName += '<a href="' + Jsw.prepareUrl('/account/my-resources') + '">'
                        + item.domain + '</a>';
                } else {
                    var returnUrl = Jsw.prepareUrl('/account/show/id/' + item.id);
                    subscriptionName += '<a href="' + Jsw.prepareUrl('/account/switch/id/' + item.id + '?hideNotice=1&returnUrl=' + encodeURIComponent(returnUrl)) + '">'
                        + item.domain + '</a>';
                    var hintId = 'hint-item-' + item.id;
                    subscriptionName += ' <span class="hint hint-info" id="' + hintId + '"><span>(?)</span></span>';
                }

                return subscriptionName;
            },
        });

        listColumns.push({
            header: <?php echo $this->subscriptionsList->jsLmsg('planName');?>,
            sortable: true,
            dataIndex: 'planName',
            renderer: function (item) {
                return ('' == item.planName)
                    ? '&mdash;'
                    : Jsw.escapeHtml(item.planName);
            },
        });

        listColumns.push({
            header: <?php echo $this->subscriptionsList->jsLmsg('setupDate');?>,
            cls: 'text-right',
            headerCls: 't-r',
            sortable: true,
            dataIndex: 'setupDate',
            renderer: function (item) {
                return ('' == item.setupDate)
                    ? '&mdash;'
                    : item.setupDate;
            },
        });

        listColumns.push({
            header: '',
            cls: 'text-right',
            renderer: function (item) {
                if (item.isMyResources) {
                    return '';
                }

                var link =
                    '<span>'
                        + '<span class="tooltipData">'
                            + <?php echo $this->subscriptionsList->jsLmsg('switchToLinkHint'); ?>
                        + '</span>'
                        + '<a href="' + Jsw.prepareUrl('/account/switch/all/false/id/' + item.id) + '" class="s-btn sb-switch">'
                            + '<i><i><i><span>'
                                + <?php echo $this->subscriptionsList->jsLmsg('switchToLink'); ?>
                            + '</span></i></i></i>'
                        + '</a>'
                    '</span>';

                return link
            },
        });

        return listColumns;
    }());

    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: '/account/list-data',
            columns: listColumns,
            onRedraw: function () {
                document.querySelectorAll('span.hint.hint-info').forEach(function (hintElement) {
                    new Jsw.DynamicPopupHint.Instance({
                        title: <?php echo $this->jsLmsg('subscriptionsHintTitle'); ?>,
                        waitMsg: <?php echo $this->jsLmsg('subscriptionsHintWaitMessage'); ?>,
                        url: '/account/stats-hint/id/' + hintElement.id.replace('hint-item-', ''),
                        placement: 'right',
                        target: hintElement.id
                    });
                });
            },
            isDisabledItem: function (item) {
                return (item.id == 0);
            },
        }],
    });
})();
//]]>
</script>
