Ext.define('TP.PanletService', {
    extend: 'TP.Panlet',

    title: 'Service',
    height: 420,
    width:  480,
    menusnr: 0,
    type:   'service',
    initComponent: function() {
        this.callParent();
        this.xdata.url         = 'panorama.cgi?task=service_detail';
        if(this.xdata.host    == undefined) { this.xdata.host    = '' }
        if(this.xdata.service == undefined) { this.xdata.service = '' }

        this.refreshHandler = function() {
            TP.defaultSiteRefreshHandler(this);
        };

        /* load service data */
        this.loader = TP.ExtinfoPanelLoader(this),
        this.add(TP.ExtinfoPanel('service'));

        /* Settings */
        this.hosts_store = new Ext.data.Store({
                fields: ['name'],
                data: []
        });
        this.host_combo = new Ext.form.field.ComboBox({
            fieldLabel:     'Host',
            name:           'host',
            queryMode:      'local',
            valueField:     'name',
            displayField:   'name',
            store:          this.hosts_store,
            listeners: {
                select: function( combo, records, eOpts ) {
                    panel = combo.up('panel').up('panel').up('panel');
                    panel.xdata.service = '';
                    panel.updateService();
                },
                change: function( combo, newValue, oldValue, eOpts ) {
                    panel = combo.up('panel').up('panel').up('panel');
                    panel.updateService();
                }
            }
        });
        this.down('form').add(this.host_combo);

        this.services_store = Ext.create('Ext.data.Store', {
            fields: ['description'],
            data :  []
        });
        this.service_combo = new Ext.form.field.ComboBox({
            fieldLabel:     'Service',
            name:           'service',
            queryMode:      'local',
            displayField:   'description',
            valueField:     'description',
            forceSelection: true,
            editable:       false,
            store:          this.services_store
        });
        this.down('form').add(this.service_combo);
        this.gearInitCallback = function(This) {
            this.updateHost();
        };

        this.updateHost = function() {
            var panel  = this;
            var form   = this.down('form').getForm();
            var values = form.getValues();
            var url = 'panorama.cgi?task=host_list';
            Ext.Ajax.request({
                url: url,
                method: 'POST',
                callback: function(options, success, response) {
                    var data  = TP.getResponse(panel, response);
                    panel.host_combo.store.removeAll();
                    for(var nr in data.data) {
                        panel.host_combo.store.loadRawData(data.data[nr], true);
                    }
                    panel.host_combo.setValue(panel.xdata.host);
                }
            });
        };

        this.updateService = function() {
            var panel  = this;
            var form   = this.down('form').getForm();
            var values = form.getValues();
            if(values.host != this.lastHost) {
                this.lastHost = values.host;
                var url = 'panorama.cgi?task=service_list';
                url = Ext.urlAppend(url, Ext.Object.toQueryString({host: values.host}));
                Ext.Ajax.request({
                    url: url,
                    method: 'POST',
                    callback: function(options, success, response) {
                        var data  = TP.getResponse(panel, response);
                        panel.service_combo.store.removeAll();
                        for(var nr in data.data) {
                            panel.service_combo.store.loadRawData(data.data[nr], true);
                        }
                        panel.service_combo.setValue(panel.xdata.service);
                    }
                });
            }
        };

        /* auto load when host is set */
        this.addListener('afterrender', function() {
            this.setTitle(this.xdata.host + ' - ' + this.xdata.service);
            if(this.xdata.host == '') {
                this.gearHandler();
            } else {
                this.manualRefresh();
            }
        });

        this.formUpdatedCallback = function() {
            this.setTitle(this.xdata.host + ' - ' + this.xdata.service);
        }
    }
});

/* Add new downtime menu item */
TP.service_downtime_menu = function() {
    var fields = [{
            fieldLabel: 'Comment',
            xtype:      'textfield',
            name:       'com_data',
            emptyText:  'comment',
            width:      288
        }, {
            fieldLabel: 'Next Check',
            xtype:      'datetimefield',
            name:       'start_time'
        }, {
            fieldLabel: 'End Time',
            xtype:      'datetimefield',
            name:       'end_time'
        }, {
            xtype: 'hidden', name: 'com_author',  value: '[% remote_user %]'
        }, {
            xtype: 'hidden', name: 'fixed',       value: '1'
    }];
    return TP.ext_menu_command('Add', 56, fields);
}

/* Acknowledge menu item */
TP.service_ack_menu = function() {
    var fields = [{
            fieldLabel: 'Comment',
            xtype:      'textfield',
            name:       'com_data',
            emptyText:  'comment',
            width:      288
        }, {
            fieldLabel: 'Sticky Acknowledgement',
            xtype:      'checkbox',
            name:       'sticky_ack'
        }, {
            fieldLabel: 'Send Notification',
            xtype:      'checkbox',
            name:       'send_notification'
        }, {
            fieldLabel: 'Persistent Comment',
            xtype:      'checkbox',
            name:       'persistent'
        }, {
            xtype: 'hidden', name: 'com_author',  value: '[% remote_user %]'
    }];
    var defaults = {
        labelWidth: 140
    };
    return TP.ext_menu_command('Acknowledge', 34, fields, defaults);
}

/* Remove Acknowledge menu item */
TP.service_ack_remove_menu = function() {
    var fields = [{
        fieldLabel: '',
        xtype:      'displayfield',
        value:      'no options needed',
        name:       'display',
        width:      240
    }];
    return TP.ext_menu_command('Remove Acknowledgement', 52, fields);
}

/* Reschedule check menu item */
TP.service_reschedule_menu = function() {
    var fields = [{
        fieldLabel: 'Next Check',
        xtype:      'datetimefield',
        name:       'start_time',
        value:      new Date()
    }];
    return TP.ext_menu_command('Reschedule', 7, fields);
}

