[% USE date %]
Ext.define('TP.TabBar', {
    extend: 'Ext.tab.Panel',

    id:             'tabpan',
    region:         'center',
    minTabWidth:    80,
    deferredRender: false,
    stateful:       true,
    tabBar:{
        items:[{ xtype: 'tbfill' },{
            id:       'test_tab',
            closable: false,
            minWidth: 28,
            iconCls:  'test-tab',
            tooltip:  'Test',
            handler:  TP.test
        }, {
            xtype:    'label',
            text:     '[% date_format(c, date.now, "%H:%M") %]',
            id:       'server_time',
            tooltip:  'server time',
            style:    'margin-top: 3px',
            width:    35,
            height:   16
        }, {
            id:       'bug_report',
            closable:  false,
            minWidth:  28,
            iconCls:  'bug-tab',
            href:     '#',
            html:     'Report',
            tooltip:  'Send Bug Report'
        }, {
            iconCls:  'user-tab',
            closable:  false,
            tooltip:  'user menu',
            arrowCls: 'arrow x-btn-arrow-right x-btn-arrow',
            html:     '[% remote_user %]&nbsp;&nbsp;',
            menu: {
                items: [/*{
                        text: 'Switch User',
                        icon: '[% url_prefix %]thruk/plugins/panorama/images/cup_key.png',
                        handler: function() {}
                }*/
               {
                        text: 'About',
                        icon: '[% url_prefix %]thruk/plugins/panorama/images/information.png',
                        handler: function() { TP.aboutWindow() }
                    }, {
                        text: 'Settings',
                        icon: '[% url_prefix %]thruk/plugins/panorama/images/cog.png',
                        handler: function() { TP.settingsWindow() }
                    },
                    /* Exit */
                    '-',
                    [% IF cookie_auth %]
                    {
                        text:   'Logout',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/door_in.png',
                        handler: function() {
                            window.location = 'login.cgi?logout';
                        }
                    },
                    [% END %]
                    {
                        text:   'Exit Panorama View',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/exit.png',
                        handler: function() {
                            window.location = '../';
                        }
                    }
                ]
            }
        }, {
            title: '',
            closable: false,
            minWidth: 28,
            iconCls: 'gear-tab',
            tooltip:  'add panlets',
            arrowCls: 'arrow x-btn-arrow-right x-btn-arrow',
            html:     ' ',
            menu: {
                items: [{
                    /* Server */
                        text:   'Site Status',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/server.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridServer'}) }
                    }, {
                        text:   'Server Status',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/bricks.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridServerStats'}) }
                    }, {
                        text:   'Core Performance Metrics',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/table_lightning.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridCoreMetrics'}) }
                    }, {
                        text:   'Host / Service Performance',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/table_gear.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridCheckMetrics'}) }
                    },
                    /* Hosts */
                    '-', {
                        text:   'Hosts',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/server.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridHosts'}) }
                    }, {
                        text:   'Hosts Totals',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/application_view_columns.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridHostTotals'}) }
                    }, {
                        text:   'Hosts Graph',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/chart_pie.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletPieChartHosts'}) }
                    },
                    /* Services */
                    '-', {
                        text:   'Services',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/computer.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridServices'}) }
                    }, {
                        text:   'Services Totals',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/application_view_columns.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridServiceTotals'}) }
                    }, {
                        text:   'Services Graph',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/chart_pie.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletPieChartServices'}) }
                    }, {
                        text:   'Mine Map',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/minemap.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridServiceMineMap'}) }
                    },
                    /* Misc */
                    '-', {
                        text:   'Logfile',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/text_align_left.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridLogs'}) }
                    }, {
                        text:   'PNP Graph',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/chart_curve.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletPNP'}) }
                    },
                    /* Mod-Gearman */
                    '-', {
                        text:   'Mod-Gearman Metrics',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/modgearman.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletGridGearmanMetrics'}) }
                    }, {
                        text:   'Mod-Gearman Charts',
                        icon:   '[% url_prefix %]thruk/plugins/panorama/images/modgearman.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletChartGearman'}) }
                    },
                    /* Generics */
                    '-', {
                        text:    'Generic Url Panlet',
                        icon:    '[% url_prefix %]thruk/plugins/panorama/images/html_add.png',
                        handler: function() { TP.add_panlet({type:'TP.PanletUrl'}) }
                    }
                ]
            }
        }]
    },
    initComponent: function() {
        this.callParent();

        /* global default setttings */
        this.xdata = {
            refresh:        60,
            rotate_tabs:     0,
            backends:       [],
            server_time:    true,
            autohideheader: false
        }
        [%+ FOREACH b = backend_detail.keys %]
        [%+ IF backend_detail.$b.state == 0 || backend_detail.$b.state == 1 %]this.xdata.backends.push('[% b %]');[% END +%]
        [% END +%]

        /* create new tab */
        var tabhead = this.getTabBar().items.getAt(0);
        tabhead.addListener('click', function(This, eOpts) {
            return TP.add_pantab();
        });

        this.addListener('afterrender', function(This, eOpts) {
            TP.startServerTime();
        });
    },
    getState:   function() {
        var item_ids = [];
        this.items.each(function(item, idx, length) {
            var stateId = item.getStateId();
            if(stateId) {
                item_ids.push(stateId);
            }
        });
        return {
            item_ids:  item_ids,
            xdata:     this.xdata,
            activeTab: this.items.indexOf(this.getActiveTab())
        }
    },
    applyState: function(state) {
        try {
            if(state) {
                    this.xdata = state.xdata;
                    for(nr in state.item_ids) {
                        TP.add_pantab(state.item_ids[nr]);
                    };
                this.setActiveTab(state.activeTab);
                Ext.apply(this, state);
            }
            TP.startRotatingTabs();
            TP.startServerTime();
        } catch(e) {
            debug(e);
            if(confirm("Errors while loading your saved settings:\n\n"+e+"\n\nStart over with a clean view?\nAll panorama view settings will be deleted.")) {
                [%+ IF stateprovider == 'cookie' +%]
                try { cp.clearAll(); } catch(e) {}
                [%+ END +%]
                window.location = 'panorama.cgi?clean=1';
            }
        }
    },
    items: [{
        title: '',
        closable: false,
        iconCls: 'new-tab',
        tabConfig: {
            minWidth: 28
        },
        tooltip: 'Add new Dashboard',
        listeners: {
            beforeactivate: function() { tabpan = Ext.getCmp('tabpan'); if(tabpan.items.length == 1) { TP.add_pantab(); } return false; }
        }
    }]
});
