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

<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {
    <?php if ($this->isCloneTaskRunning): ?>
    new Jsw.Task.StatusMessage({
        'id': <?php echo $this->cloneTaskId ?>,
        'status': <?php echo Zend_Json::encode($this->cloneTaskStatus) ?>,
        'gauge': false,
        'percent': false,
        'renderTo': 'main',
        'renderMode': 'top'
    });
    <?php endif; ?>

    new Jsw.Hint({
        renderTo: 'main',
        renderMode: 'top',
        hint: <?php echo $this->jsLmsg('hint'); ?>
    });
});

Plesk.require('app/database', function (Database) {
    var itemActions = new Database.ListOperations({
        dumpPathMaxLength: <?= $this->dumpPathMaxLength ?>,
        invalidPathChars: <?php echo $this->jsHtml(Os::factory()->invalidPathChars()); ?>,
    });
    <?php if (!$this->subscriptionStatus): ?>
    itemActions.downloadDump = function() {
        Jsw.clearStatusMessages();
        Jsw.addStatusMessage('error', <?php echo $this->jsLmsg('smb.controllers.database.download-dump.subscriptionIsTurnedOff'); ?>);
    };
    itemActions.uploadDump = function() {
        Jsw.clearStatusMessages();
        Jsw.addStatusMessage('error', <?php echo $this->jsLmsg('smb.controllers.database.upload-dump.subscriptionIsTurnedOff'); ?>);
    };
    <?php endif; ?>

    <?php if ($this->databasesActiveList): ?>
    Jsw.render(document.getElementById('main'), new Database.ActiveList(Object.assign(<?= Zend_Json::encode($this->databasesActiveList->getJsConfig()) ?>, {
        itemActions: itemActions,
    })));
    <?php else: ?>
    new Jsw.Panel({
        cls: 'list-box',
        renderTo: 'main',
        items: [
            new Jsw.List({
                id: 'databases-list',
                searchOveral: 'name',
                searchFilters: <?php echo Zend_Json::encode($this->databasesList->getSearchFilterConfig()); ?>,
                data: <?php echo Zend_Json::encode($this->databasesList->fetchData()); ?>,
                dataUrl: '/database/list-data',
                disabledSelectHint: <?php echo $this->databasesList->jsLmsg('disabledSelectHint'); ?>,
                columns: [
                    Jsw.list.COLUMN_SELECTION,
                    {
                        header: <?php echo $this->databasesList->jsLmsg('type'); ?>,
                        headerCls: 'icon',
                        sortable: true,
                        dataIndex: 'type',
                        cls: 'min',
                        renderer: function (item) {
                            var icon = '';
                            switch (item.fork) {
                                case 'mysql':
                                    icon = '<?php echo $this->skinUrl('/icons/16/plesk/mysql.png') ?>';
                                    break;
                                case 'mariadb':
                                    icon = '<?php echo $this->skinUrl('/icons/16/plesk/mariadb.png') ?>';
                                    break;
                                case 'percona':
                                    icon = '<?php echo $this->skinUrl('/icons/16/plesk/percona.png') ?>';
                                    break;
                                case 'mssql':
                                    icon = '<?php echo $this->skinUrl('/icons/16/plesk/mssql.png') ?>';
                                    break;
                                case 'postgresql':
                                    icon = '<?php echo $this->skinUrl('/icons/16/plesk/postgresql.png') ?>';
                                    break;
                            }
                            return '<img width="16" height="16" title="' + item.typeTitle + '" src="' + icon + '"/>';
                        }
                    }, {
                        header: <?php echo $this->databasesList->jsLmsg('name'); ?>,
                        sortable: true,
                        dataIndex: 'name',
                        renderer: function (item) {
                            var hint = item.isUsedByApp ? (' (' + <?php echo $this->databasesList->jsLmsg('disabledSelectHint');?> +')') : '';
                            return item.name + hint;
                        }
                    }, {
                        header: <?php echo $this->databasesList->jsLmsg('server');?>,
                        sortable: false,
                        dataIndex: 'server',
                        renderer: function (item) {
                            return item.serverHost + (0 < item.serverPort ? ':' + item.serverPort : '') + ' (' + item.typeTitle + ')';
                        }
                    }, {
                        header: <?php echo $this->databasesList->jsLmsg('users');?>,
                        sortable: false,
                        dataIndex: 'users',
                        renderer: function (item) {
                            var users = item.users.slice(0, <?php echo Smb_View_List_Databases::USERS_LIST_TRIM_LIMIT; ?>);
                            return users.map(function (user) {
                                return '<a href="' + Jsw.prepareUrl('/database/edit-user/id/' + user.id) + '">'
                                    + user.login + '</a>';
                            }).join(', ') + (item.users.length > users.length ? ',&nbsp;...' : '');
                        }
                    },
                    Jsw.list.COLUMN_ACTIONS
                ],
                operations: [
                    <?php if ($this->allowCreateDatabase): ?>
                    {
                        componentType: 'Jsw.SmallButton',
                        title: <?php echo $this->jsLmsg('buttonAddNewDatabase');?>,
                        description: <?php echo $this->jsEscape($this->addNewDatabaseHint); ?> + <?php echo $this->jsEscape(' ' . $this->resourceUsage('max_db')); ?>,
                        addCls: 'sb-add-db',
                        href: '/database/create'
                    }, {
                        componentType: 'Jsw.bar.Separator'
                    },
                    <?php endif; ?>
                    {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonRemoveDatabase',
                        title: <?php echo $this->databasesList->jsLmsg('remove');?>,
                        description: <?php echo $this->databasesList->jsLmsg('hintRemove'); ?>,
                        addCls: 'sb-remove-selected',
                        handler: function (event) {
                            Jsw.getComponent('databases-list').execGroupOperation({
                                url: '/database/delete',
                                subtype: 'delete',
                                mouseEvent: event,
                                locale: {
                                    confirmOnGroupOperation: <?php echo $this->databasesList->jsLmsg('confirmOnDelete'); ?>
                                }
                            });
                        }
                    }, {
                        componentType: 'Jsw.list.AdditionalActions',
                        cls: 'actions-menu list-type-switcher',
                        titleAddCls: 'sb-manage',
                        operations: [{
                            componentType: 'Jsw.SmallButton',
                            id: 'buttonActiveList',
                            title: <?php echo $this->databasesList->jsLmsg('buttonActiveList'); ?>,
                            description: <?php echo $this->databasesList->jsLmsg('buttonActiveListHint'); ?>,
                            cls: 'sb-btn',
                            addCls: 'sb-active-list',
                            handler: function () {
                                Jsw.redirect('?list-type=active');
                            }
                        }]
                    },
                ],
                isDisabledItem: function (item) {
                    return 1 === item.isUsedByApp;
                },
                itemActions: itemActions,
            })
        ]
    });
    <?php endif; ?>
});
//]]>
</script>
