<?php
// Copyright 1999-2021. Plesk International GmbH. All rights reserved.
?>

<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {

    new Jsw.Panel({
        cls: 'list-box',
        renderTo: 'main',
        items: [
            new Jsw.List({
                id: 'database-users-list',
                searchOveral: 'name',
                searchFilters: <?php echo Zend_Json::encode($this->databaseUsersList->getSearchFilterConfig()); ?>,
                data: <?php echo Zend_Json::encode($this->databaseUsersList->fetchData()); ?>,
                dataUrl: '/database/users-list-data',
                columns: [
                    Jsw.list.COLUMN_SELECTION,
                    {
                        header: <?php echo $this->databaseUsersList->jsLmsg('login'); ?>,
                        sortable: true,
                        dataIndex: 'login',
                        renderer: function(item) {
                            var login = (1 == item.isDefault) ? ('<b>' + item.login + '</b>') : item.login;
                            var hint = item.isUsedByApp ? (' (' + <?php echo $this->databaseUsersList->jsLmsg('disabledSelectHint');?> + ')') : '';

                            var orphanedRepairUrl = <?php echo $this->jsEscape($this->orphanedRepairUrl) ?>;

                            if ('orphaned' == item.status) {
                                var orphanedHint = ' (' + <?php echo $this->databaseUsersList->jsLmsg('orphanedHint') ?> + ')';
                                var orphanedRepairLink = '<a href="' + Jsw.prepareUrl('/database/repair-orphaned/id/' + item.id) + '">' +
                                    <?php echo $this->databaseUsersList->jsLmsg('orphanedRepairLink') ?> +
                                '</a>';

                                hint += orphanedHint.replace('%%orphanedRepairLink%%', orphanedRepairLink);
                            }

                            return ('orphaned' == item.status )
                                ? login + hint
                                : '<a href="' + Jsw.prepareUrl('/database/edit-user/id/' + item.id) + '">'
                            + login + '</a>' + hint;
                        }
                    }, {
                        header: <?php echo $this->databaseUsersList->jsLmsg('database');?>,
                        sortable: false,
                        dataIndex: 'databaseName',
                        renderer: function(item) {
                            var databaseName = (true === item.globalUser) ? ('<b>' + item.databaseName + '</b>') : item.databaseName;
                            return databaseName;
                        }
                    }, {
                        header: <?php echo $this->databaseUsersList->jsLmsg('server');?>,
                        sortable: false,
                        dataIndex: 'server',
                        renderer: function (item) {
                        	return item.serverHost + (0 < item.serverPort ? ':' + item.serverPort : '') + ' (' + item.serverTypeTitle + ')';
                        }
                    }
                ],
                operations: [
                <?php if ($this->allowCreateUser): ?>
                {
                    componentType: 'Jsw.SmallButton',
                    title: <?php echo $this->jsLmsg('buttonAddUser');?>,
                    description: <?php echo $this->jsLmsg('buttonAddUserHint'); ?>,
                    addCls: 'sb-add-user',
                    href: '/database/create-user'
                }, {
                    componentType: 'Jsw.bar.Separator'
                },
                <?php endif; ?>
                {
                    componentType: 'Jsw.SmallButton',
                    id: 'buttonRemoveUser',
                    title: <?php echo $this->databaseUsersList->jsLmsg('remove');?>,
                    description: <?php echo $this->databaseUsersList->jsLmsg('hintRemove'); ?>,
                    addCls: 'sb-remove-selected',
                    handler: function(event) {
                        Jsw.getComponent('database-users-list').execGroupOperation({
                            url: '/database/delete-users',
                            subtype: 'delete',
                            mouseEvent: event,
                            locale: {
                                confirmOnGroupOperation: <?php echo $this->databaseUsersList->jsLmsg('confirmOnDelete'); ?>
                            }
                        });
                    }
                }],
                isDisabledItem: function(item) {
                    return 1 == item.isUsedByApp || 1 == item.isDefault;
                }
            })
        ]
    });

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