<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>
<script type="text/javascript">
//<![CDATA[
    new Jsw.Panel({
        renderTo: 'main',
        cls: 'list-box',
        items: [
            new Jsw.List({
                id: 'customers-list',
                searchFilters: <?php echo Zend_Json::encode($this->customersList->getSearchFilterConfig()); ?>,
                searchOveral: 'name',
                data: <?php echo Zend_Json::encode($this->customersList->fetchData()); ?>,
                dataUrl: '<?php echo $this->url(['action' => $this->dataAction]); ?>',
                disabledSelectHint: <?php echo $this->customersList->jsLmsg('disabledSelectHint');?>,
                operations: [
                    <?php if ($this->allowCreateCustomer): ?>
                    {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonAddNewCustomer',
                        title: <?php echo $this->customersList->jsLmsg('buttonAddNewCustomer');?>,
                        description: <?php echo $this->customersList->jsLmsg('hintAddNewCustomer') ?>,
                        addCls: 'sb-add-new-customer',
                        href: '<?php echo $this->url(['action' => $this->createAction]); ?>'
                    },
                    <?php if ($this->isAdmin && $this->resellersSupport): ?>
                    {
                        componentType: 'Jsw.bar.Separator'
                    }, {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonConvertCustomersToReseller',
                        title: <?php echo $this->customersList->jsLmsg('buttonConvertCustomersToReseller');?>,
                        description: <?php echo $this->customersList->jsLmsg('hintConvertCustomersToReseller'); ?>,
                        addCls: 'sb-convert-to-reseller',
                        handler: function(event) {

                            var customersList = Jsw.getComponent('customers-list');
                            if (!customersList.checkNonEmptySelection()) {
                                return;
                            }

                            Plesk.require('app/convert-customers-to-resellers', function (ConvertCustomersToResellers) {
                                new ConvertCustomersToResellers({
                                    id: 'convertCustomersToResellers',
                                    cls: 'popup-panel',
                                    prepareUrl: Jsw.prepareUrl('/customer/prepare-convert/'),
                                    handlerUrl: Jsw.prepareUrl('/customer/convert-to-reseller/?redirect=<?php echo $this->url(); ?>'),
                                    ids: customersList.getSelectedItemsIds()
                                });
                            });

                            event.preventDefault();
                            event.stopPropagation();
                        }
                    },
                    <?php endif; ?>
                    <?php if ($this->isAdmin && $this->hasResellers && ($this->resellersSupport || $this->hasResellerCustomers)): ?>
                    {
                        componentType: 'Jsw.bar.Separator'
                    }, {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonChangeOwner',
                        title: <?php echo $this->customersList->jsLmsg('buttonChangeOwner');?>,
                        description: <?php echo $this->customersList->jsLmsg('hintChangeOwner'); ?>,
                        addCls: 'sb-move',
                        handler: function(event) {
                            var customerList = Jsw.getComponent('customers-list');
                            if (!customerList.checkNonEmptySelection()) {
                                return;
                            }

                            Plesk.require('app/change-customers-owner', function (ChangeCustomersOwner) {
                                new ChangeCustomersOwner({
                                    id: 'changeOwner',
                                    cls: 'popup-panel',
                                    prepareUrl: Jsw.prepareUrl('/customer/prepare-change-owner/'),
                                    handlerUrl: Jsw.prepareUrl('/customer/change-owner/?redirect=<?php echo $this->url(); ?>'),
                                    ids: customerList.getSelectedItemsIds()
                                });
                            });

                            event.preventDefault();
                            event.stopPropagation();
                        }
                    },
                    <?php endif; ?>
                    {
                        componentType: 'Jsw.bar.Separator'
                    },
                    <?php endif; ?>
                    {
                        componentType: 'Jsw.list.AdditionalActions',
                        title: <?php echo $this->customersList->jsLmsg('buttonChangeStatus'); ?>,
                        operations: [
                            {
                                componentType: 'Jsw.SmallButton',
                                id: 'buttonSuspendCustomers',
                                title: <?php echo $this->customersList->jsLmsg('buttonSuspendCustomers');?>,
                                description: <?php echo $this->customersList->jsLmsg('hintSuspendCustomers'); ?>,
                                addCls: 'sb-suspend',
                                cls: 'sb-btn',
                                handler: function(event) {
                                    Jsw.getComponent('customers-list').execGroupOperation({
                                        url: '/customer/suspend/?redirect=<?php echo $this->url(); ?>',
                                        skipConfirmation: true
                                    });
                                }
                            }, {
                                componentType: 'Jsw.SmallButton',
                                id: 'buttonActivateCustomers',
                                title: <?php echo $this->customersList->jsLmsg('buttonActivateCustomers');?>,
                                description: <?php echo $this->customersList->jsLmsg('hintActivateCustomers'); ?>,
                                addCls: 'sb-activate',
                                cls: 'sb-btn',
                                handler: function(event) {
                                    Jsw.getComponent('customers-list').execGroupOperation({
                                        url: '/customer/activate/?redirect=<?php echo $this->url(); ?>',
                                        skipConfirmation: true
                                    });
                                }
                            }
                        ]
                    <?php if ($this->allowCreateCustomer): ?>
                    }, {
                        componentType: 'Jsw.bar.Separator'
                    }, {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonRemoveUser',
                        title: <?php echo $this->customersList->jsLmsg('buttonRemoveCustomer') ?>,
                        description: <?php echo $this->customersList->jsLmsg('hintRemoveCustomer') ?>,
                        addCls: 'sb-remove-selected',
                        handler: function(event) {
                            Jsw.getComponent('customers-list').execGroupOperation({
                                url: '/customer/delete/?redirect=<?php echo $this->url(); ?>',
                                subtype: 'delete',
                                mouseEvent: event,
                                locale: {
                                    confirmOnGroupOperation: <?php echo $this->customersList->jsLmsg('confirmOnDelete'); ?>
                                }
                            });
                        }
                    <?php endif; ?>
                    }
                ],
                columns: [
                    Jsw.list.COLUMN_SELECTION,
                    {
                        header: <?php echo $this->customersList->jsLmsg('customerName');?>,
                        sortable: true,
                        dataIndex: 'customerName',
                        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('statusOk');?>;
                            if (!item.active) {
                                status = 'suspended';
                                statusIcon = '<?php echo $this->skinUrl('/icons/16/plesk/subscription-status-suspended.png') ?>';
                                statusDescription = 1 == item.status
                                    ? <?php echo $this->customersList->jsLmsg('statusInactive');?>
                                    : <?php echo $this->customersList->jsLmsg('statusSuspended');?>;
                            }

                            var link = (
                                '<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) : '')
                            );

                            var description = '';
                            if (item.description && item.description.length) {
                                description = '<div class="minor">' + Jsw.escapeHtml(item.description.truncate(50, '...')) + '<span class="tooltipData">' + Jsw.escapeHtml(item.description) + '</span></div>';
                            }

                            return '<div class="b-indent">' + link + description + '</div>';
                        }
                    }, {
                        header: <?php echo $this->customersList->jsLmsg('subscription');?>,
                        sortable: false,
                        dataIndex: 'subscriptions',
                        renderer: function(item, isDisabled) {
                            var ret = '';
                            var maxSubscriptionsDisplayed = <?php echo AdminPanel_View_List_Customers::MAX_SUBSCRIPTIONS_DISPLAYED ?>;
                            var subscriptions = $A(item.subscriptions);
                            var trailingEllipsis = maxSubscriptionsDisplayed < subscriptions.length;
                            if (trailingEllipsis) {
                                subscriptions.splice(maxSubscriptionsDisplayed, subscriptions.length - maxSubscriptionsDisplayed);
                            }
                            subscriptions.each(function (subscription){
                                var link = Jsw.escapeHtml(subscription.domainDisplayName) + ' (' + Jsw.escapeHtml(subscription.planName) + ')';
                                var hintId = 'hint-item-' + subscription.domainId;
                                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('hintInfo');?> +
                                        '</span></span>' +
                                    '</div>'
                                );
                            });
                            if (trailingEllipsis) {
                                ret += '<div class="owned-subscription status-ok">...</div>';
                            }
                            return ret;
                        }
                    }, {
                        header: <?php echo $this->customersList->jsLmsg('setupDate');?>,
                        sortable: true,
                        dataIndex: 'setupDateTimestamp',
                        renderer: function(item, isDisabled) {
                            return Jsw.escapeHtml(item.setupDateString);
                        }
                    },
                    <?php if ($this->showOwnerName): ?>
                    {
                        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) {
                                <?php if ($this->resellersSupport): ?>
                                linkHref = 'reseller';
                                <?php endif; ?>
                            }
                            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,
                        renderer: function (item, isDisabled) {
                            if (!item.allowLogin) {
                                return '';
                            }

                            var href = '/admin/customer/login/id/' + item.id + '/all/true';

                            return (
                                '<a href="' + href + '" class="s-btn sb-login"><span>' +
                                    <?php echo $this->customersList->jsLmsg('buttonLogin') ?> +
                                '</span></a>'
                            );
                        }
                    }
                ],
                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>
