<?php
// Copyright 1999-2012. Parallels IP Holdings GmbH. All Rights Reserved.
if ($this->tabs) {
    echo $this->partial('partials/tabs.phtml', array('tabs' => $this->tabs, 'renderTo' => 'main'));
}
?>

<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {
	Jsw.namespace('Admin.App');
    Admin.App.Usage = {
        checkOnClick: function(event) {
            new Ajax.Request(Jsw.baseUrl + '/app/get-updates-data/', {
                method: 'post',
                onSuccess: Admin.App.Usage.checkOnSuccess,
                onFailure: Admin.App.Usage.checkOnFailure
            });
        },
        checkOnSuccess: function(transport) {
            Jsw.clearStatusMessages();

            Admin.App.Usage.updates = transport.responseText.evalJSON();
            if (Admin.App.Usage.updates.length) {
                var message = <?php echo $this->appsList->jsLmsg('foundUpdates') ?>;
            } else {
                var message = <?php echo $this->appsList->jsLmsg('noUpdates') ?>;
            }
            Admin.App.Usage.updates.each(function(update) {
                message += '<br>' + update.name + ' ' + update.version;
            }, this);
            Jsw.addStatusMessage('info', message);

        },
        checkOnFailure: function(transport) {
        	Jsw.addStatusMessage('error', <?php echo $this->appsList->jsLmsg('updateFailed') ?>);
        }
    };

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

    new Jsw.Panel({
        cls: 'list-box',
        renderTo: 'main',
        items: [
            new Jsw.List({
                id: 'admin-app-usage-list',
                data: <?php echo Zend_Json::encode($this->appsList->fetchData()); ?>,
                dataUrl: '/app/usage-data',
                searchFilters: <?php echo Zend_Json::encode($this->appsList->getSearchFilterConfig()); ?>,
                operations: [
                    <?php if (!$this->resellerId): ?>
                    {
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonCleanApplicationCache',
                        attrs: { 'data-method': 'post' },
                        title: <?php echo $this->appsList->jsLmsg('buttonCleanApplicationCache');?>,
                        description: <?php echo $this->appsList->jsLmsg('buttonCleanApplicationCacheDescription'); ?>,
                        addCls: 'sb-clean-app-cache',
                        href: '/app/clear-cache'
                    }
                    <?php endif; ?>
                    <?php if (!$this->resellerId && $this->appsList->getPaginator()->getTotalItemCount() > 0):?>
                    ,{
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonCheckForUpdates',
                        title: <?php echo $this->appsList->jsLmsg('buttonCheckForUpdates');?>,
                        description: <?php echo $this->appsList->jsLmsg('buttonCheckForUpdatesDescription'); ?>,
                        addCls: 'sb-upload-new-app',
                        handler: Admin.App.Usage.checkOnClick
                    }
                    <?php endif;?>
                    <?php if (!$this->resellerId): ?>
                    ,{
                        componentType: 'Jsw.bar.Separator'
                    },{
                        componentType: 'Jsw.SmallButton',
                        id: 'buttonUpdateSettings',
                        title: <?php echo $this->appsList->jsLmsg('buttonUpdateSettings');?>,
                        description: <?php echo $this->appsList->jsLmsg('buttonUpdateSettingsDescription'); ?>,
                        addCls: 'sb-app-configure',
                        href: '/app/update-settings'
                    }
                    <?php endif; ?>
                ],
                columns: [
                    {
                        header: <?php echo $this->appsList->jsLmsg('name');?>,
                        sortable: true,
                        dataIndex: 'name',
                        renderer: function(item, isDisabled) {
                            var link = '<a href="' + Jsw.baseUrl + '/app/info/id/' + item.id + '">'
                                + item.name.escapeHTML() + ' (' + item.version.escapeHTML() + '-'
                                + item.release.escapeHTML() + ')</a>';
                            return link;
                        }
                    }, {
                        header: <?php echo $this->appsList->jsLmsg('numberOfInstances');?>,
                        sortable: true,
                        dataIndex: 'numberOfInstances',
                        renderer: function(item, isDisabled) {
                            return item.numberOfInstances.escapeHTML();
                        }
                    }
                ]
            })
        ]
    });
});
//]]>
</script>
