<?php
// Copyright 1999-2022. Plesk International GmbH. All rights reserved.
?>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {
    var getToolsPanel = function() {
        var tools = [
            <?php if ($this->userEditAllowed): ?>
            {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->jsLmsg('buttonEditUserProperties') ?>,
                description: <?php echo $this->jsLmsg('hintEditUserProperties') ?>,
                addCls: 'sb-my-profile',
                href: '/user/edit/id/' + '<?php echo $this->userInfo['id']; ?>'
            }
            <?php endif; ?>
            <?php if ($this->userEditAllowed && $this->userDeleteAllowed): ?>
            , {
                componentType: 'Jsw.bar.Separator'
            }
            <?php endif; ?>
            <?php if ($this->userDeleteAllowed): ?>
            , {
                componentType: 'Jsw.SmallButton',
                title: <?php echo $this->jsLmsg('buttonDeleteUser') ?>,
                description: <?php echo $this->jsLmsg('hintDeleteUser') ?>,
                addCls: 'sb-remove-selected',
                handler: function() {
                    Jsw.messageBox.show({
                        'type': Jsw.messageBox.TYPE_YESNO,
                        'subtype': 'delete',
                        'text': <?php echo $this->jsLmsg('removeUserConfirmationTitle') ?>,
                        'onYesClick': function() {
                            Jsw.redirectPost('/user/delete-current/', {
                                id: <?php echo $this->jsEscape($this->userInfo['id']) ?>
                            });
                        },
                        'buttonTitles': {
                            'yes': <?php echo $this->jsLmsg('removeUserConfirmationYes') ?>,
                            'no': <?php echo $this->jsLmsg('removeUserConfirmationNo') ?>
                        }
                    });
                }
            }
            <?php endif; ?>
        ];
        return tools;
    };

    var rolePermissions = (function() {
        var fields = [{
            componentType: 'Jsw.form.DisplayField',
            fieldLabel: <?php echo $this->jsLmsg('roleTitle') ?>,
            value: <?php echo $this->jsEscape($this->roleTitle) ?> + ' <a href="' + Jsw.prepareUrl(
                '/role/edit/id/<?php echo $this->roleId; ?>?returnUrl=/user/overview/id/<?php echo $this->userInfo['id']; ?>') + '">[' +
                <?php echo $this->jsLmsg('editRole') ?> + ']</a>'
        }];

        <?php if (isset($this->restrictedSubscriptionDomainName)): ?>
            fields.push({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsLmsg('limitSubscription') ?>,
                value: <?php echo $this->jsEscape($this->restrictedSubscriptionDomainName) ?>
            });
        <?php endif ?>

        <?php foreach ($this->rolePermissions as $permission): ?>
            fields.push({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsEscape($permission['label']) ?>,
                value: <?php echo $this->jsHtml($this->accessState($permission['value'])); ?>
            });
        <?php endforeach; ?>

        return {
            componentType: 'Jsw.Container',
            items: fields
        };
    }());

    var servicesPermissions = (function() {
        var fields = [];

        <?php foreach ($this->servicesPermissions as $permission): ?>
            fields.push({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsEscape($permission['label']) ?>,
                value: [
                    <?php if ($permission['serviceUrl']): ?>
                        <?php echo $this->jsLmsg('installedAt'); ?> +
                        ' <a href="<?php echo $this->escape($permission['serviceUrl']); ?>" target="_blank"><?php
                            echo $this->escape($permission['serviceUrl'])
                        ?></a>',
                    <?php endif; ?>
                    <?php echo $this->jsHtml($this->accessState($permission['value'], $permission['isConfigured'])) ?>
                    <?php if ($permission['value'] && $this->canManageApplications): ?>
                        <?php if ($permission['newAccountUrl']): ?>
                        + ' <a href="' + Jsw.prepareUrl(<?php echo $this->jsEscape($permission['newAccountUrl']) ?>)
                           + '">[' + <?php echo $this->jsLmsg('createNewService') ?> + ']</a>'
                        <?php elseif ($permission['configureAccountUrl']): ?>
                        + ' <a href="' + Jsw.prepareUrl(<?php echo $this->jsEscape($permission['configureAccountUrl']) ?>)
                           + '">[' + <?php echo $this->jsLmsg('configureService') ?> + ']</a>'
                        <?php endif; ?>
                    <?php endif; ?>
                ]
            });
        <?php endforeach; ?>

        return {
            componentType: 'Jsw.Container',
            items: fields
        };
    }());

    var firstColumnGeneralInfo = (function () {
        var fields = [{
            componentType: 'Jsw.form.DisplayField',
            fieldLabel: <?= $this->jsLmsg('contactName') ?>,
            value: <?= $this->jsEscape($this->userInfo['contactName']) ?>
        } , {
            componentType: 'Jsw.form.DisplayField',
            fieldLabel: <?= $this->jsLmsg('email') ?>,
            value: '<a href="mailto:' + <?= $this->jsEscape($this->userInfo['email']) ?> +'">'
                + <?= $this->jsEscape($this->userInfo['email']) ?> +'</a>',
        }];

        <?php if (!empty($this->userInfo['externalEmail']) && $this->userInfo['externalEmail'] !== $this->userInfo['email']) { ?>
            fields.push({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?= $this->jsLmsg('externalEmail') ?>,
                value: '<a href="mailto:' + <?= $this->jsEscape($this->userInfo['externalEmail']) ?> +'">'
                    + <?= $this->jsEscape($this->userInfo['externalEmail']) ?> +'</a>',
            });
        <?php } ?>

        return {
            componentType: 'Jsw.Container',
            items: fields
        };
    }());

    var secondColumnGeneralInfo = (function () {
        var fields = [];

        <?php if (!empty($this->userInfo['phone'])) { ?>
        fields.push({
            componentType: 'Jsw.form.DisplayField',
            fieldLabel: <?= $this->jsLmsg('phone') ?>,
            value: <?= $this->jsEscape($this->userInfo['phone']) ?>
        });
        <?php } ?>
        <?php if (!empty($this->userInfo['imNumber'])) { ?>
        fields.push({
            componentType: 'Jsw.form.DisplayField',
            fieldLabel: <?= $this->jsLmsg('imNumber') ?>,
            value: <?= $this->jsEscape($this->userInfo['imNumber'] ? $this->userInfo['imNumber'] . ' (' . $this->userInfo['imType'] . ')' : '')  ?>
        });
        <?php } ?>

        return {
            componentType: 'Jsw.Container',
            items: fields
        };
    }());

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

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

            new Jsw.Panel({
                title: <?php echo $this->jsLmsg('generalSectionTitle') ?>,
                cls: 'form-box',
                items: [{
                    componentType: 'Jsw.layout.TwoColumns',
                    cls: 'two-cols-block',
                    items: [firstColumnGeneralInfo, secondColumnGeneralInfo]
                }]
            }),

            new Jsw.Panel({
                title: <?php echo $this->jsLmsg('permissionsSectionTitle') ?>,
                cls: 'form-box',
                boxItems: [
                    new Jsw.Hint({
                        hint: <?php echo $this->jsLmsg('permissionsSectionHint') ?>
                    })
                ],
                items: [{
                    componentType: 'Jsw.layout.TwoColumns',
                    cls: 'two-cols-block',
                    items: [rolePermissions, servicesPermissions]
                }]
            })
        ]
    });
});

//]]>
</script>
