<?php
// Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
?>
<?php echo $this->partial('partials/tabs.phtml', array('tabs' => $this->tabs, 'renderTo' => 'main')); ?>

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

    new Jsw.List({
        id: 'mail-settings-list',
        renderTo: 'main',
        data: <?php echo Zend_Json::encode($this->mailSettingsList->fetchData()); ?>,
        dataUrl: '/mail-settings/list-data',
        searchFilters: <?php echo Zend_Json::encode($this->mailSettingsList->getSearchFilterConfig()); ?>,
        columns: [
            Jsw.list.COLUMN_SELECTION,
            {
                header: <?php echo $this->mailSettingsList->jsLmsg('name');?>,
                sortable: true,
                dataIndex: 'name',
                renderer: function(item) {
                    return '<a href="' + Jsw.baseUrl + '/mail-settings/edit/id/' + item.id + '/domainId/' + item.id + '">'
                        + item.name.escapeHTML() + '</a>';
                }
            }, {
                header: <?php echo $this->mailSettingsList->jsLmsg('mailService');?>,
                sortable: true,
                dataIndex: 'mailService',
                renderer: function(item) {
                    var statusText, statusIcon, statusAlt;
                    if (0 == item.mailService) {
                        statusText = <?php echo $this->mailSettingsList->jsLmsg('statusEnabled'); ?>;
                        statusIcon = '<?php echo $this->skinUrl('/icons/16/plesk/on.png') ?>';
                        statusAlt = 'enabled';
                    } else {
                        statusText = <?php echo $this->mailSettingsList->jsLmsg('statusDisabled'); ?>;
                        statusIcon = '<?php echo $this->skinUrl('/icons/16/plesk/off.png') ?>';
                        statusAlt = 'disabled';
                    }
                    return '<img src="' + statusIcon + '" alt="' + statusAlt + '" title=""> ' + statusText;
                }
            }, {
                header: <?php echo $this->mailSettingsList->jsLmsg('webmail');?>,
                sortable: true,
                dataIndex: 'webmail',
                renderer: function(item) {
                    return undefined == item.availableWebmails[item.webmail]
                        ? <?php echo $this->mailSettingsList->jsLmsg('webmailNone'); ?>
                        : item.availableWebmails[item.webmail].escapeHTML();
                }
            }, {
                header: <?php echo $this->mailSettingsList->jsLmsg('nonexist');?>,
                sortable: true,
                dataIndex:'nonexist',
                renderer: function(item) {
                    var nonExist = undefined == item.availableNonExist[item.nonexist] ? '' : item.availableNonExist[item.nonexist];
                    nonExist = nonExist.replace('%%catchAddress%%', undefined == item.catchAddress ? '' : item.catchAddress);
                    nonExist = nonExist.replace('%%catchIp%%', undefined == item.catchIp ? '' : item.catchIp);
                    return nonExist.escapeHTML();
                }
            }<?php if ($this->isOutgoingMailControlAllowed): ?>, {
                header: <?php echo $this->mailSettingsList->jsLmsg('numberOutgoingMessages');?>,
                renderer: function(item) {
                    var link = '';
                    if (item.showOutgoingMessagesLimit) {
                        link = '<a href="' + Jsw.baseUrl + '/mail-settings/outgoing-messages/id/' + item.id + '">'
                        + '<img src="<?php echo $this->skinUrl('/icons/16/plesk/protected.png') ?>" title="">'
                        + ' '
                        + <?php echo $this->mailSettingsList->jsLmsg('editOutgoingSpamSettings');?>
                        + '</a>';
                    }
                    return link;
                }
            }
            <?php endif ?>
        ],
        operations: [
            {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->jsLmsg('buttonSwitchService');?>,
                description: <?php echo $this->jsLmsg('hintSwitchService'); ?>,
                addCls: 'sb-switch-service',
                handler: function() {
                    showPopupForm(function() {
                        new Smb.MailSettingsPopupManager.SwitchServicePopupForm({
                            id: 'switchServicePopupForm',
                            handlerUrl: '/mail-settings/update-switch-service',
                            locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.mail-settings.list')); ?>,
                            isMailSettingsManagementAllowed: <?php if ($this->isMailSettingsManagementAllowed): ?> true <?php else: ?> false <?php endif;?>,
                            isMailListsManagementAllowed: <?php if ($this->isMailListsManagementAllowed): ?> true <?php else: ?> false <?php endif;?>,
                            isGreylistingManagementAllowed: <?php if ($this->isGreylistingManagementAllowed): ?> true <?php else: ?> false <?php endif;?>,
                            isSignOutgoingManagementAllowed: <?php if ($this->isSignOutgoingManagementAllowed): ?> true <?php else: ?> false <?php endif;?>,
                        });
                    });
                }
            }<?php if ($this->isMailSettingsManagementAllowed): ?>, {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->jsLmsg('buttonWebmail');?>,
                description: <?php echo $this->jsLmsg('hintWebmail'); ?>,
                addCls: 'sb-webmail',
                handler: function(event) {
                    showPopupForm(function() {
                        new Smb.MailSettingsPopupManager.WebmailPopupForm({
                            id: 'webmailPopupForm',
                            handlerUrl: '/mail-settings/update-webmail',
                            locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.mail-settings.list')); ?>
                        });
                    });
                }
            }<?php if ($this->isManageMailBounceAllowed): ?>, {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->jsLmsg('buttonBounceSettings');?>,
                description: <?php echo $this->jsLmsg('hintBounceSettings'); ?>,
                addCls: 'sb-bounce-settings',
                handler: function() {
                    showPopupForm(function() {
                        new Smb.MailSettingsPopupManager.BounceSettingsPopupForm({
                            id: 'bounceSettingsPopupForm',
                            handlerUrl: '/mail-settings/update-bounce-settings',
                            locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.controllers.mail-settings.list')); ?>,
                            isOutgoingMailControlAllowed: <?php if ($this->isOutgoingMailControlAllowed): ?> true <?php else: ?> false <?php endif; ?>,
                            catchAllAddressCanBeSpecified: <?php if ($this->catchAllAddressCanBeSpecified): ?> true <?php else: ?> false <?php endif; ?>
                        });
                    });
                }
            }<?php endif ?>
                <?php if ($this->isDomainOutgoingSpamSettingsManagementAllowed): ?>
                , {
                    componentType: 'Jsw.bar.Separator'
                }, {
                    componentType: 'Jsw.SmallButton',
                    title: <?php echo $this->jsLmsg('buttonOutgoingSpamSettings');?>,
                    description: <?php echo $this->jsLmsg('hintOutgoingSpamSettings'); ?>,
                    addCls: 'sb-outgoing-spam-settings',
                    handler: function() {
                        showPopupForm(function() {
                            new Smb.MailSettingsPopupManager.DomainOutgoingSpamSettingsPopupForm({
                                id: 'outgoingSpamSettingsPopupForm',
                                handlerUrl: '/mail-settings/update-outgoing-messages',
                                locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.components.confirmations.domain-outgoing-messages')); ?>
                            });
                        });
                    }
                }
                <?php endif ?>
            <?php endif ?>
        ]
    });

    var showPopupForm = function(callback) {
        var list = Jsw.getComponent('mail-settings-list');
        if (!list.checkNonEmptySelection()) {
            return;
        }

        callback();
    };
});
//]]>
</script>
