<?php
// Copyright 1999-2012. Parallels IP Holdings GmbH. All Rights Reserved.
?>
<form id="serviceRemovalForm" action="<?php echo $this->baseUrl('/service/delete' . $this->domainIdParam);?>" method="POST">
    <input type="hidden" id="ids" name="ids[]" value="" />
    <input type="hidden" id="service_removal_forgery_protection_token" name="forgery_protection_token" value="" />
</form>
<script type="text/javascript">
//<![CDATA[
function startUpdate()
{
    Jsw.redirectPost('/app-update/update/resourceId/' + '<?php echo $this->service['resourceId'] . $this->domainIdParam; ?>');
}

Jsw.namespace('Smb.Views.Apps');
Smb.Views.Apps.removeService = function(resourceId, rootService) {
    Jsw.messageBox.show({
        'type': Jsw.messageBox.TYPE_YESNO,
        'subtype': 'delete',
        'text': rootService
            ? <?php echo $this->jsLmsg('buttonUninstallApplicationConfirmationTitle')?>
            : <?php echo $this->jsLmsg('buttonUninstallServiceConfirmationTitle')?>,
        'description': rootService
            ? <?php echo $this->jsLmsg('buttonUninstallApplicationConfirmationDescription')?>
            : <?php echo $this->jsLmsg('buttonUninstallServiceConfirmationDescription')?>,
        'onYesClick': function() {
            var form = $('serviceRemovalForm');
            form.down('#service_removal_forgery_protection_token').value = $('forgery_protection_token').content;
            form.down('#ids').value = resourceId;
            form.submit();
        },
        'buttonTitles': {
            'yes':  <?php echo $this->jsLmsg('buttonUninstallConfirmationYes')?>,
            'no': <?php echo $this->jsLmsg('buttonUninstallConfirmationNo')?>
        }
    })
}

Jsw.onReady(function() {
    var getToolsPanel = function() {
        var tools = [
            <?php if ($this->service['rootService']): ?>
            {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->jsLmsg('buttonDetailedApplicationInfo') ?>,
                addCls: 'sb-app-info',
                href: '/service/info/resourceId/' + '<?php echo $this->service['resourceId']; ?>' + '<?php echo $this->domainIdParam?>'
            },
            <?php endif; ?>
            {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->jsLmsg('buttonChangeSettings') ?>,
                addCls: 'sb-app-configure',
                href: '/service/configure/resourceId/' + '<?php echo $this->service['resourceId']; ?>' + '<?php echo $this->domainIdParam?>'
            },
            <?php if ($this->activate): ?>
            {
                componentType: 'Jsw.SmallButton',
                attrs: { 'data-method': 'post' },
                title: <?php echo $this->jsLmsg('buttonActivate') ?>,
                addCls: 'sb-app-key-ok',
                href: '/service/activate/resourceId/' + '<?php echo $this->service['resourceId']; ?>' + '<?php echo $this->domainIdParam?>'
            },
            <?php elseif ($this->license): ?>
            {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->jsLmsg('buttonLicense') ?>,
                addCls: 'sb-app-key-ok',
                href: '/service/license/resourceId/' + '<?php echo $this->service['resourceId']; ?>' + '<?php echo $this->domainIdParam?>'
            },
            <?php endif; ?>
            {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->jsLmsg('buttonUninstallApplication') ?>,
                addCls: 'sb-app-delete',
                handler: function(event) {
                    Smb.Views.Apps.removeService(
                        <?php echo $this->jsEscape($this->service['resourceId']); ?>,
                        <?php echo ($this->service['rootService'] ? 'true' : 'false') ?>
                    );
                }
            }
        ];

        return tools;
    }

    var firstColumnItems = (function() {
        var firstColumnItems = [];
        <?php if ($this->generalInfo): ?>
            var sectionGeneralFields = [];
            <?php if (array_key_exists('installationUrl', $this->generalInfo)): ?>
            sectionGeneralFields.push({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsLmsg('installedAt'); ?>,
                value: <?php echo $this->jsEscape($this->generalInfo['installationUrl']); ?>
            });
            <?php endif; ?>

            <?php if (array_key_exists('users', $this->generalInfo) && 0 < count($this->generalInfo['users'])): ?>
                <?php if (1 == count($this->generalInfo['users'])): ?>
                <?php $currentUser = current($this->generalInfo['users']); ?>
                sectionGeneralFields.push({
                    componentType: 'Jsw.form.DisplayField',
                    fieldLabel: <?php echo $this->jsLmsg('userName'); ?>,
                    value: '<a href="' + Jsw.baseUrl + '/user/edit/id/'
                        + <?php echo $this->jsEscape($currentUser->id); ?>
                        + '">'
                        + <?php echo $this->jsEscape($currentUser->contactName); ?>
                        + '</a>'
                });
                <?php else: ?>
                sectionGeneralFields.push({
                    componentType: 'Jsw.form.DisplayField',
                    fieldLabel: <?php echo $this->jsLmsg('users'); ?>,
                    value: <?php echo $this->jsEscape(count($this->generalInfo['users'])); ?>
                });
                <?php endif; ?>
            <?php endif; ?>

            <?php if (array_key_exists('licenseKey', $this->generalInfo)): ?>
                sectionGeneralFields.push({
                    componentType: 'Jsw.form.DisplayField',
                    fieldLabel: <?php echo $this->jsLmsg('keyNumber'); ?>,
                    value: <?php echo $this->jsEscape((string)$this->generalInfo['licenseKey']->getId()); ?>
                });
                sectionGeneralFields.push({
                    componentType: 'Jsw.form.DisplayField',
                    fieldLabel: <?php echo $this->jsLmsg('keyExpiration'); ?>,
                    value: <?php echo $this->jsEscape((string)$this->generalInfo['licenseKey']->getExpiration()); ?>
                });
            <?php endif; ?>

            <?php if (array_key_exists('forceUpdates', $this->generalInfo)): ?>
                sectionGeneralFields.push({
                    componentType: 'Jsw.form.DisplayField',
                    fieldLabel: <?php echo $this->jsLmsg('forceUpdates'); ?>,
                    value: '<img src="' + Jsw.skinUrl + '/icons/16/plesk/'
                            + <?php if ($this->generalInfo['forceUpdates']): ?> 'on.png' <?php else: ?> 'subscription-status-suspended.png' <?php endif; ?> + '"> '
                            + <?php echo ($this->generalInfo['forceUpdates']) ? $this->jsLmsg('forceUpdatesOn') : $this->jsLmsg('forceUpdatesOff'); ?>
                            <?php if ($this->automaticUpdatesSettingChangeable): ?>
                                + ' <a data-method="post" href="' + Jsw.baseUrl + '/service/updates'
                                + <?php if ($this->generalInfo['forceUpdates']): ?> '/enable/0' <?php else: ?> '/enable/1' <?php endif; ?>
                                 + '/resourceId/' + <?php echo $this->jsEscape($this->service['resourceId']); ?>
                                + <?php echo $this->jsEscape($this->domainIdParam); ?>
                                + '">' + <?php echo ($this->generalInfo['forceUpdates'])
                                    ? $this->jsLmsg('stopAutomaticUpdates') : $this->jsLmsg('allowAutomaticUpdates') ?>
                                + '</a>'
                            <?php endif; ?>
                });
            <?php endif; ?>

            firstColumnItems.push({
                componentType: 'Jsw.Panel',
                'title': <?php echo $this->jsLmsg('generalSectionTitle') ?>,
                cls: 'form-box',
                items: sectionGeneralFields
            });
        <?php endif; ?>

        <?php if ($this->settingsInfo): ?>
            var sectionSettingsFields = [];
            <?php foreach ($this->settingsInfo as $setting): ?>
                sectionSettingsFields.push({
                    componentType: 'Jsw.form.DisplayField',
                    fieldLabel: <?php echo $this->jsEscape($setting['label']); ?>,
                    value:
                        <?php if (is_array($setting['value'])): ?>
                            <?php
                                $value = '';
                                foreach ($setting['value'] as $val) {
                                    if ('' !== $value) {
                                        $value .= '<br>';
                                    }
                                    $value .= Plesk_Base_Utils_String::safeForJs($this->escape($val));
                                }
                            ?>
                            '<?php echo $value; ?>'
                        <?php elseif ($setting['bool']): ?>
                            <?php echo $this->jsHtml($this->switchState('true' == $setting['value'])); ?>
                        <?php else: ?>
                            <?php echo $this->jsEscape($setting['value']); ?>
                        <?php endif; ?>
                });
            <?php endforeach; ?>

            firstColumnItems.push({
                componentType: 'Jsw.Panel',
                'title': <?php echo $this->jsLmsg('settingsSectionTitle') ?>,
                cls: 'form-box',
                items: sectionSettingsFields
            });
        <?php endif; ?>

        <?php if ($this->dnsRecords): ?>
            var sectionDnsRecordsFields = [];
            <?php foreach ($this->dnsRecords as $record): ?>
                sectionDnsRecordsFields.push({
                    componentType: 'Jsw.form.DisplayField',
                    fieldLabel: <?php echo $this->jsEscape($record->displayHost); ?>,
                    value: <?php echo $this->jsHtml('<strong>') . ' + ' .
                        $this->jsEscape($record->type . ($record->opt ? (' (' . $record->opt . ')') : '')) . ' + ' .
                        $this->jsHtml('</strong> ') . ' + ' . $this->jsEscape($record->displayVal); ?>
                });
            <?php endforeach; ?>
            firstColumnItems.push({
                componentType: 'Jsw.Panel',
                'title': <?php echo $this->jsLmsg('dnsRecordsSectionTitle') ?>,
                cls: 'form-box',
                items: sectionDnsRecordsFields
            });
        <?php endif; ?>

        return firstColumnItems;
    }());

    var secondColumnItems = (function() {
        var secondColumnItems = [];
        <?php if ($this->servicesInfo): ?>
            var sectionServices = [];
            <?php foreach ($this->servicesInfo as $service): ?>
                <?php if ($service['singular']): ?>
                    sectionServices.push({
                        componentType: 'Jsw.Hint',
                        hint:
                            '<div class="form-row"><div class="field-name">' +
                            <?php echo $this->jsEscape($service['name']); ?> +
                            '</div><div class="field-value">' +
                            <?php echo $this->jsHtml($this->accessState(0 < $service['count'])); ?>
                            <?php if (0 < $service['count']): ?>
                                <?php if ($service['configurable']): ?>
                                    + ' <a href="' + Jsw.baseUrl + '/service/configure/resourceId/'
                                    + <?php echo $this->jsEscape($service['resourceId']); ?>
                                    + '">[' + <?php echo $this->jsLmsg('serviceConfigure') ?> + ']</a>'
                                <?php endif; ?>
                                + ' <a href="#" onClick="Smb.Views.Apps.removeService(\''
                                + <?php echo $this->jsEscape($service['resourceId']); ?> + '\', false)">'
                                + '[' + <?php echo $this->jsLmsg('serviceUninstall') ?> + ']</a>'
                            <?php else: ?>
                                + ' <a href="' + Jsw.baseUrl + '/service/install/resourceId/'
                                + <?php echo $this->jsEscape($this->service['resourceId']); ?> + '/metaId/'
                                + <?php echo $this->jsEscape($service['metaId']); ?>
                                + '">[' + <?php echo $this->jsLmsg('serviceInstall') ?> + ']</a>'
                            <?php endif; ?>
                            + '</div></div>'
                    });
                <?php endif; ?>
            <?php endforeach; ?>

            if (sectionServices.length) {
                secondColumnItems.push({
                    componentType: 'Jsw.Panel',
                    'title': <?php echo $this->jsLmsg('servicesSectionTitle') ?>,
                    cls: 'form-box',
                    items: sectionServices
                });
            }
        <?php endif; ?>

        <?php foreach ($this->entryPoints as $entryPoint): ?>
            var sectionEntryPointUrls = [];
            <?php foreach ($entryPoint['urls'] as $url): ?>
                sectionEntryPointUrls.push({
                    componentType: 'Jsw.Hint',
                    hint: '<a href="' + Jsw.prepareUrl(<?php echo $this->jsEscape($url['href']); ?>) + '" target="_blank">'
                    + <?php echo $this->jsEscape($url['description']); ?>
                    + '</a>'
                });
            <?php endforeach; ?>
            <?php if (0 == count($entryPoint['urls'])): ?>
                sectionEntryPointUrls.push({
                    componentType: 'Jsw.Hint',
                    hint: <?php echo $this->jsLmsg('noEntryPointsHint'); ?>
                });
            <?php endif; ?>

            secondColumnItems.push({
                componentType: 'Jsw.Panel',
                'title': <?php echo $this->jsEscape($entryPoint['label']) ?>,
                cls: 'form-box',
                items: sectionEntryPointUrls
            });
        <?php endforeach; ?>

        return secondColumnItems;
    }());

    new Jsw.Container({
        renderTo: 'main',
        items: [
            new Jsw.Hint({
                hint: <?php echo $this->jsEscape($this->pageHint); ?>
            }),

            new Jsw.SmallTools({
                operations: getToolsPanel()
            }),

            new Jsw.layout.TwoColumns({
                items: [
                    new Jsw.Container({
                        items: firstColumnItems
                    }),
                    new Jsw.Container({
                        items: secondColumnItems
                    })
                ]
            })

        <?php foreach ($this->servicesInfo as $service):
            if ($service['singular']) {
                continue;
            }
            $getSubserviceInstancesList = $this->getSubserviceInstancesList;
            echo ', ' . $this->partial('service/index/subservice.phtml', array(
                'subserviceInstancesList' => $getSubserviceInstancesList($this->service['resourceId'], $service['metaId']),
                'registryResourceId' => $this->service['resourceId'],
                'apsMetaId' => $service['metaId'],
                'serviceName' => $service['name'],
            ));
        endforeach; ?>
        ]
    });
});
//]]>
</script>
