<?php
// Copyright 1999-2022. Plesk International GmbH. All rights reserved.
?>
<script type="text/javascript">
//<![CDATA[
    new Jsw.Hint({
        renderTo: 'main',
        hint: <?php echo Zend_Json::encode($this->pageHint); ?>
    });

    new Jsw.Panel({
        cls: 'list-box',
        renderTo: 'main',
        items: [
            new Jsw.List({
                id: 'customers-list',
                searchFilters: <?php echo Zend_Json::encode($this->customersList->getSearchFilterConfig()); ?>,
                searchOveral: 'customerName',
                data: <?php echo Zend_Json::encode($this->customersList->fetchData()); ?>,
                dataUrl: '/business-model-upgrade/customers-target-data/solutions/<?php echo $this->solutions?>',
                columns: [
                    {
                        header: <?php echo $this->customersList->jsLmsg('customerName');?>,
                        sortable: true,
                        dataIndex: 'customerName',
                        cls: 'name',
                        renderer: function (item, isDisabled) {
                            var status = 'ok';
                            var statusIcon = '<?php echo $this->skinUrl('/icons/16/plesk/subscription-status-ok.png') ?>';
                            var statusDescription = <?php echo $this->customersList->jsLmsg('admin.components.lists.customers.statusOk') ?>;
                            if (!item.active) {
                                status = 'suspended';
                                statusIcon = '<?php echo $this->skinUrl('/icons/16/plesk/subscription-status-suspended.png') ?>';
                                statusDescription = <?php echo $this->customersList->jsLmsg('admin.components.lists.customers.statusSuspended') ?>;
                            }

                            return (
                                '<div class="b-indent">' +
                                    '<span class="b-indent-icon">' +
                                        '<span class="tooltipData">' + Jsw.escapeHtml(statusDescription) + '</span>' +
                                        '<img src="' + statusIcon + '" alt="' + status + '" title="">' +
                                    '</span> ' +
                                    '<a href="' + Jsw.prepareUrl('/customer/overview/id/' + item.id) + '">' +
                                        Jsw.escapeHtml(item.customerName) +
                                    '</a>' +
                                    (item.companyName && item.companyName.length ? ', ' + Jsw.escapeHtml(item.companyName) : '') +
                                '</div>'
                            );
                        }
                    }, {
                        header: <?php echo $this->customersList->jsLmsg('subscription');?>,
                        sortable: false,
                        dataIndex: 'subscriptions',
                        renderer: function(item, isDisabled) {
                            var ret = '';
                            $A(item.subscriptions).each(function (subscription){
                                var name = Jsw.escapeHtml(subscription.domainDisplayName) + ' (' + Jsw.escapeHtml(subscription.planName) + ')';
                                var hintId = 'hint-item-' + subscription.domainId;
                                var link = '<a href="' + Jsw.prepareUrl('/subscription/overview/id/' + subscription.domainId)  + '">' + name + '</a>';
                                ret += (
                                    '<div class="owned-subscription ' + (subscription.subscriptionOveruse ? 'status-overused' : 'status-ok') + '">' +
                                        link +
                                        ' <span class="hint hint-info" id="' + hintId + '"><span>' +
                                            <?php echo $this->customersList->jsLmsg('admin.components.lists.customers.hintInfo') ?> +
                                        '</span></span>' +
                                    '</div>'
                                );

                            });
                            return ret;
                        }
                    },
                    <?php if ($this->showDomains): ?>
                    {
                        header: <?php echo $this->customersList->jsLmsg('domainLimits');?>,
                        sortable: false,
                        dataIndex: 'numberOfDomains',
                        renderer: function(item, isDisabled) {
                            var ret = item.numberOfDomains;
                            if (item.overuse) {
                                ret += ' (' + <?php echo $this->customersList->jsLmsg('domainLimitsOveruse');?> + ')'
                            }
                            return ret;
                        }
                    },
                    <?php endif?>
                    <?php if ($this->showOwner): ?>
                    {
                        header: <?php echo $this->customersList->jsLmsg('ownerName');?>,
                        sortable: true,
                        dataIndex: 'ownerName',
                        renderer: function(item, isDisabled) {
                            var linkHref = null;
                            if ('client' == item.ownerType) {
                                linkHref = 'customer';
                            } else
                            if ('reseller' == item.ownerType) {
                                linkHref = 'reseller';
                            }
                            var link =
                                (linkHref ? '<a href="' + Jsw.prepareUrl('/' + linkHref + '/overview/id/' + item.ownerId) + '">' : '')
                                    + Jsw.escapeHtml(item.ownerName) +
                                (linkHref ? '</a>' : '') +
                                (item.ownerCompanyName ? ', ' + Jsw.escapeHtml(item.ownerCompanyName) : '');
                            return link;
                        }
                    },
                    <?php endif?>
                    {
                        header: '',
                        sortable: false,
                        cls: 'min',
                        renderer: function(item, isDisabled) {
                            var subscriptions = $A(item.subscriptions);
                            if (0 == subscriptions.length) {
                                return '';
                            }

                            var domainId = subscriptions[0].domainId;

                            var link = Jsw.getControlPanelLoginHtml(domainId, {
                                'openInNew': true,
                                <?php if ($this->layout()->isClassicMode): ?>
                                    'label': <?php echo $this->customersList->jsLmsg('admin.components.lists.customers.buttonLoginClassicMode'); ?>,
                                    'hint': <?php echo $this->customersList->jsLmsg('admin.components.lists.customers.hintLoginClassicMode'); ?>,
                                <?php else: ?>
                                    'label': <?php echo $this->customersList->jsLmsg('admin.components.lists.customers.buttonLogin'); ?>,
                                    'hint': <?php echo $this->customersList->jsLmsg('admin.components.lists.customers.hintLogin'); ?>,
                                <?php endif; ?>
                                'isClassicUi': <?php echo $this->layout()->isClassicMode ? 'true' : 'false' ?>
                            });
                            return link;
                        }
                    }
                ],
                onRedraw: function () {
                    document.querySelectorAll('span.hint.hint-info').forEach(function (hintElement) {
                        new Jsw.DynamicPopupHint.Instance({
                            title: <?php echo $this->jsLmsg('admin.components.lists.subscriptions.hintTitle'); ?>,
                            waitMsg: <?php echo $this->jsLmsg('admin.components.lists.subscriptions.hintWaitMessage'); ?>,
                            url: '/subscription/stats-hint/domainId/' + hintElement.id.replace('hint-item-', ''),
                            placement: 'right',
                            target: hintElement.id
                        });
                    });
                }
            })
        ]
    });
//]]>
</script>
