<?php
// Copyright 1999-2015. Parallels IP Holdings GmbH. All Rights Reserved.
?>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {
    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;
                            }

                            new AdminPanel.ConfirmationPopupManager.ConvertCustomersToResellers({
                                id: 'convertCustomersToResellers',
                                cls: 'popup-panel',
                                prepareUrl: Jsw.prepareUrl('/customer/prepare-convert/'),
                                handlerUrl: Jsw.prepareUrl('/customer/convert-to-reseller/?redirect=<?php echo $this->url(); ?>'),
                                locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('admin.components.confirmations.customer')); ?>,
                                ids: customersList.getSelectedItemsIds()
                            });

                            Event.stop(event);
                        }
                    },
                    <?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;
                            }

                            new AdminPanel.ConfirmationPopupManager.ChangeCustomersOwner({
                                id: 'changeOwner',
                                cls: 'popup-panel',
                                prepareUrl: Jsw.prepareUrl('/customer/prepare-change-owner/'),
                                handlerUrl: Jsw.prepareUrl('/customer/change-owner/?redirect=<?php echo $this->url(); ?>'),
                                locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('admin.components.confirmations.change-customer-owner')); ?>,
                                ids: customerList.getSelectedItemsIds()
                            });

                            Event.stop(event);
                        }
                    },
                    <?php endif; ?>
                    <?php if ($this->isBillingAvailable && $this->isAllowExportToBilling): ?>
                    {
                        componentType: 'Jsw.bar.Separator'
                    }, {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonExportCustomers',
                        title: <?php echo $this->customersList->jsLmsg('buttonExportCustomers');?>,
                        description: <?php echo $this->customersList->jsLmsg('hintExportCustomers'); ?>,
                        addCls: 'sb-export',
                        handler: function() {
                            Jsw.redirectPost('<?php echo $this->billingImportCustomersURL; ?>', {panelGUID: <?php echo $this->jsEscape($this->panelGUID); ?>, panelClientIDs: Jsw.getComponent('customers-list').getSelectedItemsIds()}, '_parent');
                        }
                    },
                    <?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(); ?>',
                                        <?php if ($this->isBillingAvailable): ?>
                                        subtype: 'toggle',
                                        mouseEvent: event,
                                        locale: {
                                            confirmOnGroupOperation: <?php echo $this->customersList->jsLmsg('admin.components.billing.warningSuspendCustomersGroup'); ?>
                                        }
                                        <?php else: ?>
                                        skipConfirmation: true
                                        <?php endif; ?>
                                    });
                                }
                            }, {
                                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(); ?>',
                                        <?php if ($this->isBillingAvailable): ?>
                                        subtype: 'toggle',
                                        mouseEvent: event,
                                        locale: {
                                            confirmOnGroupOperation: <?php echo $this->customersList->jsLmsg('admin.components.billing.warningUnSuspendCustomersGroup'); ?>
                                        }
                                        <?php else: ?>
                                        skipConfirmation: true
                                        <?php endif; ?>
                                    });
                                }
                            }
                        ]
                    <?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,
                                <?php if ($this->isBillingAvailable): ?>
                                needAttention: true,
                                needAttentionText: <?php echo $this->jsLmsg('admin.components.billing.warningRemoveCustomersGroup'); ?>,
                                needAttentionBlockSubmit: true,
                                <?php endif; ?>
                                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">' + statusDescription.escapeHTML() + '</span>'
                                    + '<img src="' + statusIcon + '" alt="' + status + '" title="">'
                                + '</span> '
                                + '<a href="' + Jsw.baseUrl + '/customer/overview/id/' + item.id + '">'
                                + item.customerName.escapeHTML() + '</a>'
                                + (item.companyName && item.companyName.length ? ', ' + item.companyName.escapeHTML() : '');

                            var description = '';
                            if (item.description && item.description.length) {
                                description = '<div class="minor">' + item.description.truncate(50, '...').escapeHTML() + '<span class="tooltipData">' + item.description.escapeHTML() + '</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.size();
                            if (trailingEllipsis) {
                                subscriptions.splice(maxSubscriptionsDisplayed, subscriptions.size() - maxSubscriptionsDisplayed);
                            }
                            subscriptions.each(function(subscription){
                                var link = subscription.domainDisplayName.escapeHTML() + ' (' + subscription.planName.escapeHTML() + ')';
                                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 item.setupDateString.escapeHTML();
                        }
                    },
                    <?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.baseUrl + '/' + linkHref + '/overview/id/' + item.ownerId + '">' : '')
                                    + item.ownerName.escapeHTML() +
                                (linkHref ? '</a>' : '') +
                                (item.ownerCompanyName ? ', ' + item.ownerCompanyName.escapeHTML() : '');
                            return link;
                        }
                    },
                    <?php endif;?>
                    {
                        header: '',
                        sortable: false,
                        renderer: function (item, isDisabled) {
                            if (!item.subscriptions.length) {
                                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>';
                        }
                    <?php if ($this->isBillingAvailable): ?>
                    }, {
                        header: '',
                        sortable: false,
                        renderer: function(item, isDisabled) {
                            if (!item.goToBillingUrl) {
                                return '';
                            }
                            return '<a href="' + item.goToBillingUrl + '" class="s-btn sb-billing-info" target="_blank"><span>' +
                                    <?php echo $this->customersList->jsLmsg('buttonGoToBilling') ?> +
                                    '</span></a>';
                        }
                        <?php endif; ?>
                    }
                ],
                onRedraw: function () {
                    $$('span.hint.hint-info').each(function(hintElement) {
                        new Jsw.AjaxTooltip.Instance({
                            title: <?php echo $this->jsLmsg('admin.controllers.subscription.stat-hint.title'); ?>,
                            waitMsg: <?php echo $this->jsLmsg('admin.controllers.subscription.stat-hint.waitMessage'); ?>,
                            url: '/subscription/stats-hint/domainId/' + hintElement.id.replace('hint-item-', ''),
                            target: hintElement.id
                        });
                    });
                }
            })
        ]
    });
});
//]]>
</script>
