<?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[
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.prepareUrl('/mail-settings/edit/id/' + item.id + '/domainId/' + item.id) + '">'
                        + Jsw.escapeHtml(item.name) + '</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'); ?>
                        : Jsw.escapeHtml(item.availableWebmails[item.webmail]);
                }
            }, {
                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 Jsw.escapeHtml(nonExist);
                }
            }<?php if ($this->isOutgoingMailControlAllowed): ?>, {
                header: <?php echo $this->mailSettingsList->jsLmsg('numberOutgoingMessages');?>,
                renderer: function(item) {
                    var link = '';
                    if (item.showOutgoingMessagesLimit) {
                        link = '<a href="' + Jsw.prepareUrl('/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 () {
                        Plesk.require('app/mail-settings-popup-manager', function (MailSettingsPopupManager) {
                            new MailSettingsPopupManager.SwitchServicePopupForm({
                                id: 'switchServicePopupForm',
                                handlerUrl: '/mail-settings/update-switch-service',
                                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 () {
                    showPopupForm(function () {
                        Plesk.require('app/mail-settings-popup-manager', function (MailSettingsPopupManager) {
                            new MailSettingsPopupManager.WebmailPopupForm({
                                id: 'webmailPopupForm',
                                handlerUrl: '/mail-settings/update-webmail',
                            });
                        });
                    });
                }
            }<?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 () {
                        Plesk.require('app/mail-settings-popup-manager', function (MailSettingsPopupManager) {
                            new MailSettingsPopupManager.BounceSettingsPopupForm({
                                id: 'bounceSettingsPopupForm',
                                handlerUrl: '/mail-settings/update-bounce-settings',
                                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 () {
                            Plesk.require('app/mail-settings-popup-manager', function (MailSettingsPopupManager) {
                                new MailSettingsPopupManager.DomainOutgoingSpamSettingsPopupForm({
                                    id: 'outgoingSpamSettingsPopupForm',
                                    handlerUrl: '/mail-settings/update-outgoing-messages',
                                });
                            });
                        });
                    }
                }
                <?php endif ?>
            <?php endif ?>
        ]
    });

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

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