<?php
// Copyright 1999-2015. Parallels IP Holdings GmbH. All Rights Reserved.
?>

<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {
    Smb.Database.List = new Smb.Database.ListOperations({
        locale: <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('smb.components.lists.databases')) ?>
    });

    <?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'); ?>
    });

    <?php if (!$this->subscriptionStatus): ?>
        Smb.Database.List.downloadDump = function() {
            Jsw.clearStatusMessages();
            Jsw.addStatusMessage('error', <?php echo $this->jsLmsg('smb.controllers.database.download-dump.subscriptionIsTurnedOff'); ?>);
        };
        Smb.Database.List.uploadDump = function() {
            Jsw.clearStatusMessages();
            Jsw.addStatusMessage('error', <?php echo $this->jsLmsg('smb.controllers.database.upload-dump.subscriptionIsTurnedOff'); ?>);
        };
    <?php endif; ?>
});
//]]>
</script>

<?php if ($this->databasesList->getActiveList()->isActiveListEnabled()): ?>
    <?php $bottomBar = $this->needShowAll ? $this->render('web/_list/bottom-bar.phtml') : ''; ?>
    <?php echo $this->partial('partials/active-list/active-list.phtml',
        ['list' => $this->databasesList->getActiveList(), 'showToolbar' => true, 'bottomBar' => $bottomBar, 'activeListNew' => $this->activeListNew]); ?>
    <script type="text/javascript">
        //<![CDATA[
        Jsw.onReady(function() {
            $$('.caption-head-action .toggler').each(function(element) {
                element.observe('click', function(e) {
                    e.preventDefault();
                    element.up('.caption-head-action').hide().next().show();
                });
            });
            $$('.caption-head-action .btn').each(function(element) {
                element.observe('click', function(e) {
                    e.preventDefault();
                    Jsw.redirectPost('/database/set-related-domain', {
                        id: element.up('.active-list-item').getAttribute('id').replace('active-list-item-', ''),
                        domainId: element.previous().getValue()
                    });
                });
            });
            new Ajax.Request(Jsw.baseUrl + '/database/list-size-data/', {
                method: 'post',
                onSuccess: function(transport) {
                    var result = transport.responseText.evalJSON();
                    for (var i = 0; i < result.length; i++) {
                        var activeListItemEl = $('active-list-item-' + result[i].id);
                        var hostItemEl = activeListItemEl.down('.summary-item:first');
                        if (hostItemEl.down('.state-error')) {
                            continue;
                        }
                        if (result[i].hasConnectionError) {
                            hostItemEl.insert('<span class="state-error">' + <?php echo $this->jsLmsg('smb.components.lists.databases.serverUnavailable'); ?> + '</span>');
                            activeListItemEl.down('.summary-item:last').remove();
                        } else {
                            activeListItemEl.down('.summary-item:last').replace(
                                '<span class="summary-item">' + <?php echo $this->jsLmsg('smb.components.lists.databases.tables') ?> + ': <b>' + result[i].tables + '</b></span>' +
                                '<span class="summary-item">' + <?php echo $this->jsLmsg('smb.components.lists.databases.size') ?> + ': <b>' + result[i].size + '</b></span>'
                            );
                        }
                    }
                }
            });
        });
        //]]>
    </script>
<?php else: ?>
    <?php echo $this->render('database/list-classic.phtml'); ?>
<?php endif; ?>
