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

<script type="text/javascript">
    //<![CDATA[
    Jsw.onReady(function () {
        var operationLocale = new Jsw.Locale(<?= json_encode(Zend_Registry::get('translate')->getSection('admin.controllers.services.operation')) ?>);

        var doAction = function (action, service) {
            var actionUrl = Jsw.prepareUrl('/admin/services/operation/id/' + encodeURIComponent(service.id) + '/act/' + action);
            Jsw.getComponent('admin-services-list').disable();
            Jsw.api.post(actionUrl)
                .then(function (status) {
                    if (status.redirect) {
                        Jsw.redirect(status.redirect);
                    } else {
                        Jsw.getComponent('admin-services-list').reload();
                    }

                    status.statusMessages.forEach(function (message) {
                        Jsw.addStatusMessage(message.status, message.content);
                    });
                })
                .catch(function (e) {
                    Jsw.getComponent('admin-services-list').reload();
                    if (['nginx', 'web'].indexOf(service.id) !== -1) {
                        // No response from server when start/stop/restart web and nginx services is considered as success
                        Jsw.addStatusMessage('info', operationLocale.lmsg(action + 'ServiceMessage'));
                    } else {
                        Jsw.addStatusMessage('error', operationLocale.lmsg(action + 'ActionError', { error: e.message }));
                    }
                });
        };

        new Jsw.Panel({
            cls: 'list-box',
            renderTo: 'main',
            items: [
                new Jsw.GroupsList({
                    id: 'admin-services-list',
                    pageable: false,
                    locale: <?php echo Zend_Json::encode($this->servicesList->getLocale()); ?>,
                    data: <?php echo Zend_Json::encode($this->servicesList->fetchData()); ?>,
                    dataUrl: '/admin/services/list-data',
                    searchFilters: <?php echo Zend_Json::encode($this->servicesList->getSearchFilterConfig()); ?>,
                    searchOverall: 'name',
                    operations: [
                        {
                            componentType: 'Jsw.SmallButton',
                            id: 'buttonRefreshComponents',
                            title: <?php echo $this->servicesList->jsLmsg('buttonRefreshComponents');?>,
                            description: <?php echo $this->servicesList->jsLmsg('hintRefreshComponents'); ?>,
                            addCls: 'sb-refresh',
                            handler: function () {
                                Jsw.getComponent('admin-services-list').reload();
                            }
                        }
                    ],
                    <?php if ($this->servicesList->showGroupedData()): ?>
                        groupBy: 'group',
                        groupsConfig: <?php echo Zend_Json::encode($this->servicesList->getGroupsDescriptions());?>,
                        getGroupRowClass: function (group) {
                            return 'group-header-row';
                        },
                    <?php endif; ?>
                    itemActions: {
                        changeStartupType: function (service) {
                            new Jsw.AjaxPopupForm({
                                cls: 'popup-panel',
                                title: service.name,
                                url: '/admin/services/change-startup-type/id/' + encodeURIComponent(service.id) + '/current-startup-type/' + service.startupType,
                                locale: {
                                    waitMsg: '<?php $this->servicesList->jsLmsg('waitMessage');?>'
                                }
                            });
                        },
                        start: doAction.bind(null, 'start'),
                        stop: doAction.bind(null, 'stop'),
                        restart: doAction.bind(null, 'restart')
                    },
                    columns: [{
                        header: <?php echo $this->servicesList->jsLmsg('nameColumnHeader');?>,
                        sortable: false,
                        dataIndex: 'name',
                        renderer: function (service) {
                            return '<span class="tooltipData">' + this.lmsg(service.state + 'State') + '</span>' +
                                '<i class="icon-' + (service.state === 'active' ? 'on' : 'off')+ '"></i> ' +
                                service.name;
                        }
                    }, {
                    <?php if ($this->servicesList->showStartupTypeColumn()): ?>
                        header: <?php echo $this->servicesList->jsLmsg('startupTypeColumnHeader');?>,
                        sortable: false,
                        dataIndex: 'startupType',
                        renderer: function (service) {
                            return '<a href="#" data-action-name="changeStartupType">' +
                                '<span class="tooltipData">' + this.lmsg('changeStartupTypeTooltip') + '</span>' +
                                this.lmsg(service.startupType + 'StartupType') + '</a>';
                        }
                    }, {
                    <?php endif; ?>
                        header: '',
                        sortable: false,
                        dataIndex: 'status',
                        cls: 'action-icon-set t-r',
                        renderer: function (item) {
                            var renderButton = function (action) {
                                var description, disabled;
                                if (['notInstalled', 'notConfigured'].indexOf(item.state) !== -1) {
                                    description = this.lmsg('serviceDisabled');
                                    disabled = true;
                                } else {
                                    description = this.lmsg(item.status + action.capitalize());
                                    disabled = item.status === 'running' ? action === "start" : action !== "start";
                                }

                                return '<span>' +
                                        '<span class="tooltipData">' + description + '</span>' +
                                        '<a' + (disabled ? ' class="disabled"' : '') +
                                            (disabled ? '' : ' data-action-name="' + action + '"') + '>' +
                                            '<i class="icon-' + action +  '"></i>' +
                                        '</a>' +
                                    '</span>';
                            };

                            return ["start", "stop", "restart"].map(renderButton.bind(this)).join('');
                        }
                    }]
                })
            ]
        });
    });
    //]]>
</script>
