<?php
// Copyright 1999-2024. WebPros International 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[
Jsw.namespace('Smb.Views.Apps');
Smb.Views.Apps.startUpdate = function(packageId) {
    Jsw.redirectPost(
        '/app-update/update' +
        '/resourceId/' + '<?php echo $this->service['resourceId'] . $this->domainIdParam ?>' +
        (packageId ? '/packageId/' + packageId : '')
    );
};

Smb.Views.Apps.removeService = function (resourceId, rootService) {
    Jsw.messageBox.show({
        type: Jsw.messageBox.TYPE_YESNO,
        subtype: Jsw.messageBox.SUBTYPE_DELETE,
        text: rootService
            ? <?php echo $this->jsLmsg('buttonUninstallApplicationConfirmationTitle')?>
            : <?php echo $this->jsLmsg('buttonUninstallServiceConfirmationTitle')?>,
        onYesClick: function () {
            var form = document.getElementById('serviceRemovalForm');
            form.querySelector('#service_removal_forgery_protection_token').value = document.getElementById('forgery_protection_token').content;
            form.querySelector('#ids').value = resourceId;
            form.submit();
        },
    });
};

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?>'
            },
            {
                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.prepareUrl('/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('forceUpdates', $this->generalInfo)): ?>
        sectionGeneralFields.push({
            componentType: 'Jsw.form.DisplayField',
            fieldLabel: <?php echo $this->jsLmsg('forceUpdates'); ?>,
            value: '<img src="<?php echo $this->skinUrl('/icons/16/plesk/' . ($this->generalInfo['forceUpdates'] ? 'on.png' : 'subscription-status-suspended.png')) ?>"> '
                + <?php echo ($this->generalInfo['forceUpdates']) ? $this->jsLmsg('forceUpdatesOn') : $this->jsLmsg('forceUpdatesOff'); ?>
                <?php if ($this->automaticUpdatesSettingChangeable): ?>
                + ' <a data-method="post" href="' + Jsw.prepareUrl('/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 elseif (isset($setting['password']) && $setting['password']): ?>
            '<span id="applicationAdminPasswordText__<?php echo  $setting['id'] ?>" style="display:none;">' + <?php echo $this->jsEscape($setting['value']); ?> + '</span>'
                + ' <a href="#" id="applicationAdminPasswordToggle__<?php echo  $setting['id'] ?>" class="toggler application-password-toggle">'
                + <?php echo $this->jsLmsg('showPassword') ?>
                + '</a>'
            <?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; ?>

        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.prepareUrl('/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.prepareUrl('/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; ?>
        ]
    });

    document.querySelectorAll('.application-password-toggle').forEach(function (element) {
        // use POST method for links with data-method="post"
        element.addEventListener('click', function (event) {
            event.stopPropagation();
            event.preventDefault();
            var elementId = event.target.id.split("__");
            var txtElement = document.getElementById('applicationAdminPasswordText__' + elementId[1]);
            if (txtElement.style.display !== 'none') {
                txtElement.style.display = 'none';
                event.target.innerHTML = <?php echo $this->jsLmsg('showPassword') ?>;
            } else {
                txtElement.style.display = ''
                event.target.innerHTML = <?php echo $this->jsLmsg('hidePassword') ?>;
            }
        });
    });
});
//]]>
</script>
