<?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: 'subscriptions-list',
                searchFilters: <?php echo Zend_Json::encode($this->subscriptionsList->getSearchFilterConfig()); ?>,
                searchOveral: 'domainName',
                data: <?php echo Zend_Json::encode($this->subscriptionsList->fetchData()); ?>,
                dataUrl: '<?php echo $this->url(['action' => $this->dataAction]); ?>',
                disabledSelectHint: <?php echo $this->subscriptionsList->jsLmsg('disabledSelectHint');?>,
                operations: [
                    <?php if ($this->allowCreateSubscription): ?>{
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonAddNewOwnSubscription',
                        title: <?php echo $this->subscriptionsList->jsLmsg('buttonAddNewSubscription');?>,
                        description: <?php echo $this->subscriptionsList->jsLmsg('hintAddNewSubscription'); ?>,
                        addCls: '<?php echo $this->createButtonCls; ?>',
                        href: '<?php echo $this->url(['action' => $this->createAction]); ?>'
                    }, {
                        componentType: 'Jsw.bar.Separator'
                    },
                    <?php endif; ?>
                    <?php if ($this->allowChangeSubscription): ?>
                    {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonChangeSubscription',
                        title: <?php echo $this->subscriptionsList->jsLmsg('buttonChangeSubscription');?>,
                        description: <?php echo $this->subscriptionsList->jsLmsg('hintChangeSubscription'); ?>,
                        addCls: 'sb-change-subscription',
                        handler: function() {
                            var list = Jsw.getComponent('subscriptions-list');
                            if (!list.checkNonEmptySelection()) {
                                return;
                            }
                           <?php if ($this->isBillingAvailable): ?>
                            Jsw.messageBox.show({
                                'type': Jsw.messageBox.TYPE_YESNO,
                                'subtype': 'toggle',
                                'text': <?php echo $this->jsLmsg('admin.components.billing.buttonChangeSubscriptionsGroup') ?>,
                                'description': <?php echo $this->jsLmsg('admin.components.billing.warningChangeSubscritionsGroup') ?>,
                                'onYesClick': function() {
                                    Jsw.redirectPost('<?php echo $this->url(['action' => $this->changeAction]); ?>?redirect=<?php echo $this->url(); ?>', {
                                        ids: list.getSelectedItemsIds()
                                    });
                                },
                                'buttonTitles': {
                                    'yes': <?php echo $this->jsLmsg('components.buttons.yes') ?>,
                                    'no': <?php echo $this->jsLmsg('components.buttons.no') ?>
                                }
                            });
                            <?php else: ?>
                            Jsw.redirectPost('<?php echo $this->url(['action' => $this->changeAction]); ?>?redirect=<?php echo $this->url(); ?>', {
                                ids: list.getSelectedItemsIds()
                            });
                            <?php endif; ?>
                        }
                    }
                    <?php endif; ?>
                    <?php if ($this->customersSupport): ?>
                    , {
                        componentType: 'Jsw.bar.Separator'
                    },  {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonChangeOwner',
                        title: <?php echo $this->subscriptionsList->jsLmsg('buttonChangeOwner');?>,
                        description: <?php echo $this->subscriptionsList->jsLmsg('hintChangeOwner'); ?>,
                        addCls: 'sb-assign-new-owner',
                        handler: function(event) {
                            Jsw.getComponent('subscriptions-list').execGroupOperation({
                                submitVarName: 'del',
                                submitHandler: Jsw.redirectPost,
                                url: '/plesk/client@any/domain@multi/chowner',
                                <?php if ($this->isBillingAvailable): ?>
                                subtype: 'toggle',
                                mouseEvent: event,
                                locale: {
                                    confirmOnGroupOperation: <?php echo $this->jsLmsg('admin.components.billing.warningChangeOwnerGroup'); ?>
                                }
                                <?php else: ?>
                                skipConfirmation: true
                                <?php endif; ?>
                            });
                        }
                    }
                    <?php endif; ?>
                    , {
                        componentType: 'Jsw.bar.Separator'
                    }, {
                        componentType: 'Jsw.list.AdditionalActions',
                        title: <?php echo $this->subscriptionsList->jsLmsg('buttonChangeStatus'); ?>,
                        operations: [
                            {
                                componentType: 'Jsw.SmallButton',
                                id: 'buttonSuspendSubscriptions',
                                title: <?php echo $this->subscriptionsList->jsLmsg('buttonSuspendSubscriptions');?>,
                                description: <?php echo $this->subscriptionsList->jsLmsg('hintSuspendSubscriptions'); ?>,
                                addCls: 'sb-suspend',
                                cls: 'sb-btn',
                                handler: function(event) {
                                    Jsw.getComponent('subscriptions-list').execGroupOperation({
                                        url: '/subscription/suspend/?redirect=<?php echo $this->url(); ?>',
                                        <?php if ($this->isBillingAvailable): ?>
                                        subtype: 'toggle',
                                        mouseEvent: event,
                                        locale: {
                                            confirmOnGroupOperation: <?php echo $this->jsLmsg('admin.components.billing.warningSuspendSubscriptionsGroup'); ?>
                                        }
                                        <?php else: ?>
                                        skipConfirmation: true
                                        <?php endif; ?>
                                    });
                                }
                            }, {
                                componentType: 'Jsw.SmallButton',
                                id: 'buttonActivateSubscriptions',
                                title: <?php echo $this->subscriptionsList->jsLmsg('buttonActivateSubscriptions');?>,
                                description: <?php echo $this->subscriptionsList->jsLmsg('hintActivateSubscriptions'); ?>,
                                addCls: 'sb-activate',
                                cls: 'sb-btn',
                                handler: function(event) {
                                    Jsw.getComponent('subscriptions-list').execGroupOperation({
                                        url: '/subscription/activate/?redirect=<?php echo $this->url(); ?>',
                                        <?php if ($this->isBillingAvailable): ?>
                                        subtype: 'toggle',
                                        mouseEvent: event,
                                        locale: {
                                            confirmOnGroupOperation: <?php echo $this->jsLmsg('admin.components.billing.warningUnSuspendSubscriptionsGroup'); ?>
                                        }
                                        <?php else: ?>
                                        skipConfirmation: true
                                        <?php endif; ?>
                                    });
                                }
                            }
                        ]
                    }
                    <?php if ($this->allowCreateSubscription): ?>
                    , {
                        componentType: 'Jsw.bar.Separator'
                    }, {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonRemoveSubscription',
                        title: <?php echo $this->subscriptionsList->jsLmsg('buttonRemove');?>,
                        description: <?php echo $this->subscriptionsList->jsLmsg('hintRemove'); ?>,
                        addCls: 'sb-remove-selected',
                        handler: function(event) {
                            Jsw.getComponent('subscriptions-list').execGroupOperation({
                                url: '/subscription/delete/?redirect=<?php echo $this->url(); ?>',
                                subtype: 'delete',
                                mouseEvent: event,
                                <?php if ($this->isBillingAvailable): ?>
                                'needAttention': true,
                                'needAttentionText': <?php echo $this->jsLmsg('admin.components.billing.warningRemoveSubscriptionsGroup'); ?>,
                                'needAttentionBlockSubmit': true,
                                <?php endif; ?>
                                locale: {
                                    confirmOnGroupOperation: <?php echo $this->subscriptionsList->jsLmsg('confirmOnDelete'); ?>
                                },

                                isAjax: <?php echo $this->isHomonymSearchEnabled ? 'true' : 'false' ?>,
                                requestUrl: '/subscription/search-homonym-data/',
                                loadingTitle: <?php echo $this->jsLmsg('components.homonyms.loading'); ?>
                            });
                        }
                    }<?php endif; ?>
                    <?php if ($this->allowCreateSubscription && $this->isBillingAvailable && $this->isAllowExportToBilling): ?>
                    , {
                        componentType: 'Jsw.bar.Separator'
                    }, {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonExportSubscriptions',
                        title: <?php echo $this->subscriptionsList->jsLmsg('buttonExportSubscriptions');?>,
                        description: <?php echo $this->subscriptionsList->jsLmsg('hintExportSubscriptions'); ?>,
                        addCls: 'sb-export',
                        handler: function() {
                            Jsw.redirectPost('<?php echo $this->billingImportSubscriptionsURL; ?>', {panelGUID: <?php echo $this->jsEscape($this->panelGUID); ?>, panelSubscriptionIDs: Jsw.getComponent('subscriptions-list').getSelectedItemsIds()}, '_parent');
                        }
                    }<?php endif; ?>
                ],
                columns: [
                    Jsw.list.COLUMN_SELECTION,
                    {
                        header: <?php echo $this->subscriptionsList->jsLmsg('subscriptionName');?>,
                        sortable: true,
                        dataIndex: 'domainName',
                        renderer: function(item, isDisabled) {
                            if (0 == item.id) {
                                return '<a href="' + Jsw.baseUrl + item.link + '">' +
                                    item.name.escapeHTML() +
                                    '</a>';
                            }

                            var subscriptionStatus = 'ok';
                            var subscriptionStatusIcon = '<?php echo $this->skinUrl('/icons/16/plesk/subscription-status-ok.png') ?>';
                            var subscriptionStatusDescription = <?php echo $this->subscriptionsList->jsLmsg('statusOk');?>;

                            if (!item.subscriptionActive) {
                                subscriptionStatus = 'suspended';
                                subscriptionStatusIcon = '<?php echo $this->skinUrl('/icons/16/plesk/subscription-status-suspended.png') ?>';
                                if (item.subscriptionExpired) {
                                    subscriptionStatusDescription = <?php echo $this->subscriptionsList->jsLmsg('statusSuspendedReasonExpired');?>;
                                } else {
                                    subscriptionStatusDescription = <?php echo $this->subscriptionsList->jsLmsg('statusSuspended');?>;
                                }
                            } else if (item.subscriptionLocked) {
                                subscriptionStatus = 'locked';
                                subscriptionStatusIcon = '<?php echo $this->skinUrl('/icons/16/plesk/subscription-status-locked.png') ?>';
                                subscriptionStatusDescription = <?php echo $this->subscriptionsList->jsLmsg('statusLocked');?>;
                            } else if (!item.subscriptionSynchronized) {
                                subscriptionStatus = 'unsynchronized';
                                subscriptionStatusIcon = '<?php echo $this->skinUrl('/icons/16/plesk/subscription-status-unsynchronized.png') ?>';
                                subscriptionStatusDescription = <?php echo $this->subscriptionsList->jsLmsg('statusUnsynchronized');?>;
                            }

                            var link =
                                '<span class="b-indent-icon">' +
                                    '<span>' +
                                        '<span class="tooltipData">' + subscriptionStatusDescription.escapeHTML() + '</span>' +
                                        '<img src="' + subscriptionStatusIcon + '" alt="' + subscriptionStatus + '" title="">' +
                                    '</span>' +
                                    (item.webserverConfigurationStatus
                                        ? ''
                                        : '<span>' +
                                            '<span class="tooltipData">' +
                                                <?php echo $this->jsEscape($this->webserverConfigurationDomainHint) ?> +
                                            '</span>' +
                                            '<img src="<?php echo $this->skinUrl('/icons/16/plesk/webserver-domain-error.png') ?>" alt="" title="">' +
                                        '</span>') +
                                '</span>' +
                                ' ' +
                                '<a href="' + Jsw.baseUrl + '/subscription/overview/id/' + item.id + '">' +
                                    item.domainDisplayName.escapeHTML() +
                                    ' (' + item.planName.escapeHTML() + ')' +
                                '</a>';

                            var hintId = 'hint-item-' + item.id;

                            var description = '';
                            if (item.adminDescription) {
                                description += '<div class="hint">' + item.adminDescription.truncate(50, '...').escapeHTML() + '<span class="tooltipData">' + item.adminDescription.escapeHTML() + '</span></div>';
                            }
                            if (item.resellerDescription) {
                                var descriptionTitle = (item.hideResellerTitleDescription) ? '' : <?php echo $this->subscriptionsList->jsLmsg('resellerDescription');?> +  ': ';
                                description += '<div class="hint">'
                                    + descriptionTitle
                                    + item.resellerDescription.truncate(50, '...').escapeHTML() + '<span class="tooltipData">' + item.resellerDescription.escapeHTML() +'</span></div>';
                            }
                            if (item.ownerDescription) {
                                description += '<div class="hint">' + <?php echo $this->subscriptionsList->jsLmsg('ownerDescription');?>
                                    + ': ' + item.ownerDescription.truncate(50, '...').escapeHTML() + '<span class="tooltipData">' + item.ownerDescription.escapeHTML() +'</span></div>';
                            }

                            return '<div class="' +  (item.webserverConfigurationStatus ? 'b-indent' : 'b-indent-x2') + ' ' + (item.subscriptionOveruse ? 'status-overused' : 'status-ok') + '">'
                                    + link
                                    + '&nbsp;<span class="hint hint-info" id="' + hintId + '">'
                                        + <?php echo $this->subscriptionsList->jsLmsg('hintInfo');?>
                                    + '</span>'
                                    + description
                                + '</div>';
                        }
                    <?php if ($this->showSubscriberName): ?>
                    }, {
                        header: <?php echo $this->subscriptionsList->jsLmsg('subscriberName');?>,
                        sortable: true,
                        dataIndex: 'subscriberName',
                        renderer: function(item, isDisabled) {
                            var url = null;
                            if ('client' == item.subscriberType) {
                                url = '/customer/overview/id/' + item.subscriberId;
                            } else
                            if ('reseller' == item.subscriberType) {
                                <?php if ($this->isAdmin): ?>
                                    url = '/reseller/overview/id/' + item.subscriberId;
                                <?php else:?>
                                    url = '/reseller-self/subscription-overview';
                                <?php endif?>
                            }
                            var link =
                                (url ? '<a href="' + Jsw.baseUrl + url + '">' : '')
                                    + item.subscriberName.escapeHTML() +
                                (url ? '</a>' : '') +
                                (item.subscriberCompanyName ? ', ' + item.subscriberCompanyName.escapeHTML() : '');
                            return link;
                        }
                    <?php endif; ?>
                    }, {
                        header: <?php echo $this->subscriptionsList->jsLmsg('setupDate');?>,
                        sortable: true,
                        dataIndex: 'setupDate',
                        renderer: function(item, isDisabled) {
                            if (0 == item.id) return '-';
                            return item.setupDateString.escapeHTML();
                        }
                    <?php if ($this->isBillingAvailable): ?>
                    } ,{
                        header: <?php echo $this->subscriptionsList->jsLmsg('billingDataPrice'); ?>,
                        sortable: true,
                        dataIndex: 'billingPriceForSort',
                        renderer: function(item, isDisabled) {
                            if (item.billingPrice == '') {
                                return <?php echo $this->subscriptionsList->jsLmsg('noBilllingDataAvailable'); ?>;
                            } else {
                                return item.billingPrice;
                            }
                        }
                    }, {
                        header: <?php echo $this->subscriptionsList->jsLmsg('billingDataRenewal'); ?>,
                        sortable: true,
                        dataIndex: 'billingDateRenewal',
                        renderer: function(item, isDisabled) {
                            if (item.billingDateRenewal == '') {
                                return <?php echo $this->subscriptionsList->jsLmsg('noBilllingDataAvailable'); ?>;
                            } else {
                                return item.billingDateRenewal.escapeHTML();
                            }
                        }
                    <?php endif; ?>
                    }, {
                        header: '',
                        sortable: false,
                        renderer: function (item, isDisabled) {
                            if (item.subscriberId == <?php echo \Session::get()->auth()->getUser()->getClient()->getId() ?>) {
                                return '';
                            }

                            var href = '/admin' + (item.subscriberType == '<?php echo Client::TYPE_RESELLER ?>' ? '/reseller' : '/customer') + '/login/id/' + item.subscriberId +
                                '?pageUrl=' + encodeURIComponent('/smb/account/switch/all/false/id/' + item.id);

                            return '<a href="' + href + '" class="s-btn sb-login"><span>' +
                                (item.subscriberType == '<?php echo Client::TYPE_RESELLER ?>' ? <?php echo $this->subscriptionsList->jsLmsg('manageInResellerPanel') ?> : <?php echo $this->subscriptionsList->jsLmsg('manageInCustomerPanel') ?>) +
                                '</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->subscriptionsList->jsLmsg('buttonGoToBilling') ?> +
                                '</span></a>';
                        }
                    <?php endif; ?>
                    }
                ],
                isDisabledItem: function(item) {
                    return 0 == item.id;
                },
                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>
