<?php
// Copyright 1999-2015. Parallels IP Holdings GmbH. All Rights Reserved.
?>
<?php $instancesList = $this->instancesList->fetchData(); ?>
<div class="objects-details-heading">
    <div class="objects-details-heading-area">
        <div class="toolbar" id="installed-apps-toolbar">
        </div>
        <h3>
            <span><?php echo $this->lmsg('tabTitle') ?></span>
        </h3>
    </div>
</div>
<div class="object-details-hint"><?php echo $this->lmsg('whereAreApps') ?></div>
<div class="objects-content">
    <div class="list-box" id="installed-apps-list"></div>
</div>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {
    Jsw.namespace('Smb.App');
    Smb.App.Instances = {
        updates: <?php echo Zend_Json::encode($this->updatesList) ?>,
        hasUpdate: function(registryUid) {
            var found = false;
            Smb.App.Instances.updates.each(function(update) {
               if (update.resourceId == registryUid) {
                   found = true;
                   throw $break;
               }
            });
            return found;
        },
        uninstallOnClick: function(event, resourceId) {

            Jsw.messageBox.show({
                'type': Jsw.messageBox.TYPE_YESNO,
                'subtype': 'delete',
                'text': <?php echo $this->jsLmsg('smb.controllers.service.index.buttonUninstallApplicationConfirmationTitle')?>,
                'description': <?php echo $this->jsLmsg('smb.controllers.service.index.buttonUninstallApplicationConfirmationDescription')?>,
                'onYesClick': function() {
                    var deleteUrl = '<?php echo CommonPanel_Controller_Helper_Url::getUrl('/smb/service/delete' . $this->domainIdParam) ?>';
                    Jsw.redirectPost(deleteUrl, { ids: [resourceId] });
                },
                'buttonTitles': {
                    'yes': <?php echo $this->jsLmsg('smb.controllers.service.index.buttonUninstallConfirmationYes')?>,
                    'no': <?php echo $this->jsLmsg('smb.controllers.service.index.buttonUninstallConfirmationNo')?>
                }
            });
        }
    };
    new Jsw.Panel({
        renderTo: 'installed-apps-list',
        cls: 'list-box',
        items: [
            new Jsw.List({
                id: 'instances-list',
                searchable: false,
                pageable: false,
                data: <?php echo Zend_Json::encode($instancesList); ?>,
                dataUrl: '/app/installed-list-data',
                columns: [
                    {
                        header: <?php echo $this->instancesList->jsLmsg('name') ?>,
                        sortable: true,
                        dataIndex: 'name',
                        cls: 'first',
                        renderer: function(item) {
                            var url, updates = '';
                            var name = item.name ? item.name.escapeHTML() : <?php echo $this->instancesList->jsLmsg('undefinedApp') ?>;
                            if (item.wordPressId) {
                                url = '/wordpress/detail/id/' + encodeURIComponent(item.wordPressId);
                            } else if (item.resourceId) {
                                url = '/service/index/resourceId/' + encodeURIComponent(item.resourceId) + '<?php echo $this->domainIdParam?>';
                                updates = '<div class="hint-attention update-available package-update-' + item.resourceId.escapeHTML() + '"' +
                                        (Smb.App.Instances.hasUpdate(item.resourceId) ? '' : ' style="display:none"') + '>' +
                                        '<a href="#" onclick="Jsw.redirectPost(\'/app-update/index/resourceId/' + encodeURIComponent(item.resourceId) + '<?php echo $this->domainIdParam?>' + '\');">' +
                                            <?php echo $this->instancesList->jsLmsg('update') ?> +
                                        '</a>' +
                                    '</div>';
                            } else if (item.composerId) {
                                url = '/composer/overview/id/' + encodeURIComponent(item.composerId);
                            }
                            return (url ? '<a href="' + Jsw.prepareUrl(url) + '">' : '') + name + (url ? '</a>' : '') + updates;
                        }
                    }, {
                        header: <?php echo $this->instancesList->jsLmsg('installedTo') ?>,
                        renderer: function(item) {
                            return item.url
                                ? '<a href="' + Jsw.escapeAttribute(item.url) + '" target="_blank">' + item.url.escapeHTML() + '</a>'
                                : (item.path
                                    ? item.path.escapeHTML()
                                    : <?php echo $this->instancesList->jsLmsg('noInstallationUrlAvailable') ?>
                                );
                        }
                    }, {
                        header: '',
                        cls: 'last minor t-r',
                        renderer: function(item) {
                            if (item.resourceId) {
                                return '<a href="#" onClick="Smb.App.Instances.uninstallOnClick(event, \'' + item.resourceId + '\');">' +
                                    (<?php echo $this->instancesList->jsLmsg('uninstall') ?>).escapeHTML() +
                                '</a>';
                            }
                            // TODO add button 'Remove' for non-aps apps
                            return '';
                        }
                    }
                ],
                operations: [
                    {
                        componentType: 'Jsw.SmallButton',
                        title: <?php echo $this->instancesList->jsLmsg('buttonScan'); ?>,
                        description: <?php echo $this->instancesList->jsLmsg('hintButtonScan'); ?>,
                        addCls: 'sb-refresh',
                        handler: function(e) {
                            var beginOffset = e.target.cumulativeOffset();
                            var progressBar = Jsw.getComponent('asyncProgressBarWrapper');
                            var itemId = progressBar.fly(beginOffset,
                                <?php echo $this->instancesList->jsLmsg('taskScan'); ?>,
                                function(){
                                    new Ajax.Request(Jsw.prepareUrl('/smb/app/scan'), {
                                        method: 'post',
                                        onSuccess: function(response) {
                                            progressBar.removePreparingItem(itemId);
                                            progressBar.update();
                                        }
                                    });
                                });
                        }
                    }
                ]
            })
        ]
    });
});
//]]>
</script>
<?php if (!count($instancesList['data'])): ?>
    <hr>
    <?php if (isset($this->catalogLinks['marketplace']) && isset($this->catalogLinks['apscatalog'])): ?>
    <div class="object-details-hint">
<?php
    foreach ($this->catalogLinks as &$link) {
        $link = $this->baseUrl($link);
    }
    $this->catalogLinks['_escape'] = false;
    echo $this->lmsg('installAppHint', $this->catalogLinks);
?>
    </div>
    <?php endif; ?>
    <?php echo $this->form; ?>
<?php endif; ?>
