<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>
<?php echo $this->partial('partials/tabs.phtml', array('tabs' => $this->tabs, 'renderTo' => 'main')); ?>

<script type="text/javascript">
//<![CDATA[
    new Jsw.Hint({
        renderTo: 'main',
        hint: <?php echo $this->jsLmsg('hint'); ?>
    });

    new Jsw.Panel({
        cls: 'list-box',
        renderTo: 'main',
        items: [
            new Jsw.List({
                id: 'customer-service-plan-list',
                data: <?php echo Zend_Json::encode($this->plansList->fetchData()); ?>,
                dataUrl: '/customer-service-plan/list-data',
                disabledSelectHint: <?php echo $this->plansList->jsLmsg('disabledSelectHint');?>,
                searchFilters: <?php echo Zend_Json::encode($this->plansList->getSearchFilterConfig()); ?>,
        		<?php if ($this->ownerColumnRequired) : ?>
                searchOveral: 'planName',
                <?php endif; ?>
                operations: [
                    <?php if ($this->hasChangeLimitsPermission): ?>
                    {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonAddNewPlan',
                        title: <?php echo $this->plansList->jsLmsg('buttonAddNewPlan');?>,
                        description: <?php echo $this->plansList->jsLmsg('hintAddNewPlan'); ?>,
                        addCls: 'sb-add-new-plan',
                        href: '/customer-service-plan/create'
                    }, {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonDuplicatePlans',
                        title: <?php echo $this->plansList->jsLmsg('buttonDuplicatePlans');?>,
                        description: <?php echo $this->plansList->jsLmsg('hintDuplicatePlans'); ?>,
                        addCls: 'sb-copy-selected',
                        handler: function(event) {
                            Jsw.getComponent('customer-service-plan-list').execGroupOperation({
                                url: '/customer-service-plan/duplicate-plans/?redirect=/customer-service-plan/list/',
                                skipConfirmation: true
                            });
                        }
                    }, {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonAddNewAddonPlan',
                        title: <?php echo $this->plansList->jsLmsg('buttonAddNewAddonPlan');?>,
                        description: <?php echo $this->plansList->jsLmsg('hintAddNewAddonPlan'); ?>,
                        addCls: 'sb-add-new-addon',
                        href: '/customer-service-plan/create-addon'
                    },
                    {
                        componentType: 'Jsw.bar.Separator'
                    }, {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonRemovePlan',
                        title: <?php echo $this->plansList->jsLmsg('buttonRemove');?>,
                        description: <?php echo $this->plansList->jsLmsg('hintRemove'); ?>,
                        addCls: 'sb-remove-selected',
                        handler: function(event) {
                            Jsw.getComponent('customer-service-plan-list').execGroupOperation({
                                url: '/customer-service-plan/delete/?redirect=/customer-service-plan/list/',
                                subtype: 'delete',
                                mouseEvent: event,
                                locale: {
                                    confirmOnGroupOperation: <?php echo $this->plansList->jsLmsg('confirmOnDelete'); ?>
                                }
                            });
                        }
                    }
                    <?php endif; ?>
                ],
                columns: [
                    Jsw.list.COLUMN_SELECTION,
                    {
                        header: <?php echo $this->plansList->jsLmsg('planName');?>,
                        sortable: true,
                        dataIndex: 'planName',
                        renderer: function (item, isDisabled) {
                            var link = (
                                '<span><a href="' + Jsw.prepareUrl('/customer-service-plan/edit/id/' + item.id) + '">' +
                                    Jsw.escapeHtml(item.planName) +
                                '</a></span>'
                            );

                            var syncStatus = '';
                            if ('done' != item.syncStatus) {
                                var statusElementId = 'service-plan-sync-status-' + item.id;
                                var statusClass = '';
                                if ('error' == item.syncStatus) {
                                    statusClass = 'hint-warning';
                                } else {
                                    statusClass = 'hint-wait';
                                    link = '<span>' + Jsw.escapeHtml(item.planName) + '</span>';
                                }
                                syncStatus = '<div id="' + statusElementId + '" class="hint-sub ' + statusClass + '">' + item.syncStatusDescription + '</div>';
                            }

                            return link +
                                ('domain_addon' === item.planType
                                    ? ' <span class="hint">' + <?php echo $this->plansList->jsLmsg('addonPlanType');?> + '</span>'
                                    : ''
                                ) + syncStatus;

                        },
                    }, {
                        header: <?php echo $this->plansList->jsLmsg('planTraffic');?>,
                        sortable: true,
                        dataIndex: 'traffic',
                        renderer: function (item, isDisabled) {
                            return Jsw.escapeHtml(item.trafficString);
                        },
                    }, {
                        header: <?php echo $this->plansList->jsLmsg('planDiskSpace');?>,
                        sortable: true,
                        dataIndex: 'diskSpace',
                        renderer: function (item, isDisabled) {
                            return Jsw.escapeHtml(item.diskSpaceString);
                        },
<?php if ($this->ownerColumnRequired) : ?>
                    }, {
                        header: <?php echo $this->plansList->jsLmsg('planOwner');?>,
                        sortable: true,
                        dataIndex: 'ownerContactName',
                        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.ownerContactName) +
                                (linkHref ? '</a>' : '') +
                                (item.ownerCompanyName ? ', ' + Jsw.escapeHtml(item.ownerCompanyName) : '');
                            return link;
                        },
<?php endif; ?>
                    }, {
                        header: <?php echo $this->plansList->jsLmsg('planSubscriptions');?>,
                        sortable: true,
                        dataIndex: 'subscriptions',
                        renderer: function (item, isDisabled) {
                            return (
                                '<a href="' + Jsw.prepareUrl('/subscription/list?force-show-search=true&searchFilter[planName][searchText]=' + item.id) + '">' +
                                    Jsw.escapeHtml(item.subscriptions) +
                                '</a>'
                            );
                        },
                    },
                ],
                onRedraw: function () {
                    if (!this._data) {
                        return ;
                    }
                    var randomId = this._getRandomNumber();
                    this._randomId = randomId;
                    var context = this;

                    this._data.each(function (item) {
                        if (!item.syncStatus || item.syncStatus != 'running') {
                            return;
                        }

                        setTimeout(function (){
                            context.checkSyncStatus(
                                'service-plan-sync-status-' + item.id,
                                Jsw.prepareUrl('/customer-service-plan/sync-status/servicePlanId/' + item.id),
                                function (statusElement, result) {
                                    statusElement.update(result.message);
                                    statusElement.classList.remove('hint-wait');
                                    if ('done' === result.status) {
                                        statusElement.classList.add('hint-ok');
                                    } else {
                                        statusElement.classList.add('hint-warning');
                                    }
                                    statusElement.parentNode.querySelector('span').innerHTML = (
                                        '<a href="' + Jsw.prepareUrl('/customer-service-plan/edit/id/' + item.id) + '">' +
                                            Jsw.escapeHtml(item.planName) +
                                        '</a>'
                                    );
                                },
                                randomId
                            );
                        }, 5000);
                    });
                }
            })
        ]
    });
//]]>
</script>
