<commit_before>Ext.define('Netresearch.widget.Admin', {
    extend: 'Ext.tab.Panel',

    requires: [
        'Netresearch.store.AdminCustomers',
        'Netresearch.store.AdminProjects',
        'Netresearch.store.AdminTeams',
        'Netresearch.store.AdminUsers',
        'Netresearch.store.AdminPresets',
        'Netresearch.store.TicketSystems',
    ],

    /* Load all necessary stores */
    customerStore: Ext.create('Netresearch.store.AdminCustomers'),
    projectStore: Ext.create('Netresearch.store.AdminProjects'),
    userStore: Ext.create('Netresearch.store.AdminUsers'),
    teamStore: Ext.create('Netresearch.store.AdminTeams'),
    ticketSystemStore: Ext.create('Netresearch.store.TicketSystems'),
    activityStore: Ext.create('Netresearch.store.Activities'),
    presetStore: Ext.create('Netresearch.store.AdminPresets'),

    /* Strings */
    _tabTitle: 'Administration',
    _nameTitle: 'Name',
    _teamsTitle: 'Teams',
    _activeTitle: 'Active',
    _globalTitle: 'Global',
    _addCustomerTitle: 'Add customer',
    _editTitle: 'Edit',
    _editCustomerTitle: 'Edit customer',
    _forAllTeamsTitle: 'for all teams',
    _saveTitle: 'Save',
    _deleteTitle: 'Delete',
    _seriousErrorTitle: 'A serious error occurred. Find more details in Firebug or the Chrome Developer Tools.',
    _customerTitle: 'Customer',
    _ticketPrefixTitle: 'Ticket prefix',
    _ticketSystemTitle: 'Ticket system',
    _internalJiraTicketSystem: 'internal JIRA Ticket-System',
    _projectTitle: 'Project',
    _addProjectTitle: 'Add project',
    _editProjectTitle: 'Edit project',
    _forAllCustomersTitle: 'for all customers',
    _userNameTitle: 'User name',
    _abbreviationTitle: 'Abbr',
    _typeTitle: 'Type',
    _addUserTitle: 'Add user',
    _editUserTitle: 'Edit user',
    _languageTitle: 'Language',
    _developerTitle: 'Developer',
    _projectManagerTitle: 'Project manager',
    _controllingTitle: 'Controlling',
    _teamTitle: 'Team',
    _teamLeadTitle: 'Team lead',
    _addTeamTitle: 'Add team',
    _editTeamTitle: 'Edit team',
    _teamSavedTitle: 'The team has been successfully saved.',
    _customerManagementTitle: 'Customer management',
    _projectManagementTitle: 'Project management',
    _userManagementTitle: 'User management',
    _teamManagementTitle: 'Team management',
    _presetManagementTitle: 'Preset management',
    _presetSavedTitle: 'The preset has been successfully saved.',
    _addPresetTitle: 'Add preset',
    _editPresetTitle: 'Edit preset',
    _activityTitle: 'Activity',
    _descriptionTitle: 'Description',
    _ticketSystemManagementTitle: 'Ticket system management',
    _ticketSystemSavedTitle: 'The ticket system has been successfully saved.',
    _urlTitle: 'URL',
    _ticketUrlTitle: 'Ticket URL',
    _ticketUrlHint: '"%s" as placeholder for ticket name',
    _timebookingTitle: 'Time booking',
    _loginTitle: 'Login',
    _passwordTitle: 'Password',
    _publicKeyTitle: 'Public key',
    _privateKeyTitle: 'Private key',
    _addTicketSystemTitle: 'Add ticket system',
    _errorsTitle: 'Errors',
    _errorTitle: 'Error',
    _successTitle: 'Success',
    _estimationTitle: 'Estimated Duration',
    _internalJiraProjectKey: 'internal JIRA Project Key',
    _offerTitle: 'Offer',
    _billingTitle: 'Billing',
    _costCenterTitle: 'Cost Center',
    _projectLeadTitle: 'Project Lead',
    _technicalLeadTitle: 'Technical Lead',
    _max31CharactersTitle: 'At maximum 31 characters are allowed here',
    _activityManagementTitle: 'Activity management',
    _addActivityTitle: 'Add activity',
    _editActivityTitle: 'Edit activity',
    _activitySavedTitle: 'The activity has been successfully saved.',
    _needsTicketTitle: 'Needs ticket',
    _factorTitle: 'Factor',
    _oauthConsumerKeyTitle: 'OAuth consumer key',
    _oauthConsumerSecretTitle: 'OAuth consumer secret',

    initComponent: function () {
        this.on('render', this.refreshStores, this);

        var panel = this;

        var billingStore = new Ext.data.ArrayStore({
            fields: ['value', 'displayname'],
            data: [
                [0, 'None'],
                [1, 'Time And Material'],
                [2, 'Fixed Price']
            ]
        });

        var customerGrid = Ext.create('Ext.grid.Panel', {
            store: this.customerStore,
            teamStore: this.teamStore,
            columns: [
                {
                    header: 'Id',
                    dataIndex: 'id',
                    hidden: true
                }, {
                    header: this._nameTitle,
                    dataIndex: 'name',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._teamsTitle,
                    dataIndex: 'teams',
                    flex: 1,
                    renderer: function(value) {
                        var output = '';
                        /* Display space separated list of related teams */
                        Ext.each(value, function(teamId) {
                            if (isNaN(teamId)) {
                              return;
                            }
                            var team = customerGrid.teamStore.getById(parseInt(teamId));
                            if (null == team) {
                              return;
                            }
                            if (output != '')
                                output += ', ';
                            output += team.data.name;
                        });
                        return output;
                    }
                },
                {
                    header: this._activeTitle,
                    dataIndex: 'active',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                },
                {
                    header: this._globalTitle,
                    dataIndex: 'global',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                }
            ],
            tbar: [
                {
                    text: this._addCustomerTitle,
                    iconCls: 'icon-add',
                    scope: this,
                    handler: function() {
                        customerGrid.editCustomer();
                    }
                }
            ],
            listeners: {
                /* Right-click menu */
                itemcontextmenu: function(grid, record, item, index, event, options) {
                    event.stopEvent();

                    var contextMenu = Ext.create('Ext.menu.Menu', {
                        items: [
                            {
                                text: panel._editTitle,
                                iconCls: 'icon-edit',
                                scope: this,
                                handler: function() {
                                    this.editCustomer(record.data);
                                }
                            }
                        ]
                    });

                    contextMenu.showAt(event.xy);
                }
            },
            editCustomer: function(record) {
                if(!record) record = {};

                var teamStore = Ext.create('Netresearch.store.AdminTeams', {
                    autoLoad: false
                });

                var window = Ext.create('Ext.window.Window', {
                    title: panel._editCustomerTitle,
                    modal: true,
                    width: 400,
                    id: 'edit-customer-window',
                    layout: 'fit',
                    listeners: {
                        /* Reload on destroy */
                        destroy: {
                            scope: this,
                            fn: function() {
                                this.getStore().load();
                            }
                        }
                    },
                    items: [
                        new Ext.form.Panel({
                            bodyPadding: 5,
                            defaultType: 'textfield',
                            items: [
                                new Ext.form.field.Hidden({
                                    name: 'id',
                                    value: record.id ? record.id : 0
                                }), {
                                    fieldLabel: panel._nameTitle,
                                    name: 'name',
                                    anchor: '100%',
                                    allowBlank: false,
                                    minLength: 3,
                                    value: record.name ? record.name : ''
                                },
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._activeTitle,
                                    name: 'active',
                                    inputValue: 1,
                                    checked: record.active ? record.active : 0
                                }),
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._globalTitle + '(' + panel._forAllTeamsTitle + ')',
                                    name: 'global',
                                    inputValue: 1,
                                    checked: record.global ? record.global : 0
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._teamsTitle,
                                    name: 'teams[]',
                                    store: teamStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    anchor: '100%',
                                    typeAhead: true,
                                    multiSelect: true,
                                    triggerAction: 'all',
                                    //disabled: record.global ? true : false,
                                    listeners: {
                                        /* Reload teams column to fit information */
                                        afterrender: function(field, value) {
                                            teamStore.load({
                                                params: {
                                                    team: field.getValue()
                                                }
                                            });
                                        },
                                        select: function(field, value) {
                                            teamStore.load({
                                                params: {
                                                    team: field.getValue()
                                                }
                                            });
                                        }
                                    },
                                    value: record.teams ? record.teams : []
                                })
                            ],
                            buttons: [
                                {
                                    text: panel._saveTitle,
                                    scope: this,
                                    handler: function(btn) {
                                        var form = btn.up('form').getForm();
                                        if (!form.isValid()) {
                                            var fields = form.getFields();
                                            var errors = [];

                                            /* Create Error-String and display Error-Window */
                                            for (i = 0; i < fields.length; i++) {
                                                errors.push(fields.items[i].getErrors().join(', '));
                                            }

                                            var errorsWindow = new Ext.Window({
                                                title: panel._errorsTitle,
                                                html: errors,
                                                width: 350
                                            });

                                            return errorsWindow.show();
                                        }

                                        var values = form.getValues();
                                        Ext.Ajax.request({
                                            url: url + 'customer/save',
                                            params: values,
                                            scope: this,
                                            success: function(response) {
                                                window.close();
                                            },
                                            failure: function(response) {
                                                /* If response text is less than 200 chars long (means not an exception
                                                 * stack trace), use response text. If not, show common help/error text
                                                 */
                                                message = response.responseText.length < 200
                                                    ? response.responseText
                                                    : panel._seriousErrorTitle;
                                                showNotification(panel._errorTitle, message, false);
                                            }
                                        });
                                    }
                                }
                            ]
                        })
                    ]
                });

                window.show();
            }
        });

        var projectGrid = Ext.create('Ext.grid.Panel', {
            ticketSystemStore: this.ticketSystemStore,
            customerStore: this.customerStore,
            ticketSystemStore: this.ticketSystemStore,
            store: this.projectStore,
            columns: [
                {
                    header: 'Id',
                    dataIndex: 'id',
                    hidden: true
                }, {
                    header: this._nameTitle,
                    dataIndex: 'name',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._customerTitle,
                    dataIndex: 'customer',
                    flex: 1,
                    field: {
                        xtype: 'textfield',
                        lazyRender: true,
                        queryMode: 'local',
                        displayField: 'name',
                        valueField: 'id',
                        anchor: '100%'
                    },
                    renderer: function(id) {
                        var record = this.customerStore.getById(id);
                        return record ? record.get('name') : id;
                    }
                },
                {
                    header: this._ticketPrefixTitle,
                    dataIndex: 'jiraId',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._ticketSystemTitle,
                    dataIndex: 'ticket_system',
                    flex: 1,
                    field: {
                        xtype: 'textfield',
                        lazyRender: true,
                        queryMode: 'local',
                        store: this.ticketSystemStore,
                        displayField: 'name',
                        valueField: 'id',
                        anchor: '100%'
                    },
                    renderer: function(id) {
                        if (1 > parseInt(id))
                            return '';

                        var record = this.ticketSystemStore.getById(id);
                        return record ? record.get('name') : id;
                    }
                },
                {
                    header: this._additionalInformationFromExternal,
                    dataIndex: 'additionalInformationFromExternal',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                },
                {
                    header: this._activeTitle,
                    dataIndex: 'active',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                },
                {
                    header: this._globalTitle,
                    dataIndex: 'global',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                },
                {
                    header: this._offerTitle,
                    dataIndex: 'offer',
                    width: 70,
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._costCenterTitle,
                    dataIndex: 'cost_center',
                    width: 70,
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._billingTitle,
                    dataIndex: 'billing',
                    width: 70,
                    flex: 1,
                    field: {
                        xtype: 'textfield',
                        queryMode: 'local',
                        store: billingStore,
                        displayField: 'displayname',
                        valueField: 'value',
                        anchor: '100%'
                    },
                    renderer: function(value) {
                        var record = billingStore.findRecord('value', value);
                        return record ? record.get('displayname') : value;
                    }
                },
                {
                    header: this._estimationTitle,
                    dataIndex: 'estimation',
                    width: 100,
                    flex: 1,
                    align: 'right',
                    field: {
                        xtype: 'textfield'
                    },
                    renderer: function(value) {
                        return formatDuration(value, true);
                    }
                }
            ],
            tbar: [
                {
                    text: this._addProjectTitle,
                    iconCls: 'icon-add',
                    scope: this,
                    handler: function() {
                        projectGrid.editProject();
                    }
                }
            ],
            listeners: {
                /* Right-click menu */
                itemcontextmenu: function(grid, record, item, index, event, options) {
                    event.stopEvent();

                    var contextMenu = Ext.create('Ext.menu.Menu', {
                        items: [
                            {
                                text: panel._editTitle,
                                iconCls: 'icon-edit',
                                scope: this,
                                handler: function() {
                                    this.editProject(record.data);
                                }
                            }
                        ]
                    });

                    contextMenu.showAt(event.xy);
                }
            }, // end listeners
            editProject: function(record) {
                var projectLeadStore = Ext.create('Netresearch.store.AdminUsers');
                var technicalLeadStore = Ext.create('Netresearch.store.AdminUsers');
                var projectStore = Ext.create('Netresearch.store.AdminProjects', {
                    autoLoad: false
                });

                projectLeadStore.load();
                technicalLeadStore.load();

                if (!record) {
                    record = {};
                }

                var window = Ext.create('Ext.window.Window', {
                    title: panel._editProjectTitle,
                    modal: true,
                    width: 400,
                    id: 'edit-project-window',
                    layout: 'fit',
                    listeners: {
                        destroy: {
                            scope: this,
                            fn: function() {
                                this.getStore().load();
                            }
                        }
                    },
                    items: [
                        new Ext.form.Panel({
                            bodyPadding: 5,
                            defaultType: 'textfield',
                            items: [
                                new Ext.form.field.Hidden({
                                    name: 'id',
                                    value: record.id ? record.id : 0
                                }), {
                                    fieldLabel: panel._nameTitle,
                                    name: 'name',
                                    anchor: '100%',
                                    value: record.name ? record.name : ''
                                },
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._customerTitle,
                                    name: 'customer',
                                    store: this.customerStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    anchor: '100%',
                                    disabled: record.customer ? true : false,
                                    listeners: {
                                        afterrender: function(field, value) {
                                            projectStore.load({
                                                params: {
                                                    customer: field.getValue()
                                                }
                                            });
                                        },
                                        select: function(field, value) {
                                            projectStore.load({
                                                params: {
                                                    customer: field.getValue()
                                                }
                                            });
                                        }
                                    },
                                    value: record.customer ? record.customer : ''
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._ticketSystemTitle,
                                    name: 'ticket_system',
                                    allowBlank: true,
                                    store: this.ticketSystemStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    anchor: '100%',
                                    value: record.ticket_system ? record.ticket_system : ''
                                }),
                                {
                                    fieldLabel: panel._ticketPrefixTitle,
                                    name: 'jiraId',
                                    anchor: '100%',
                                    value: record.jiraId ? record.jiraId : ''
                                },
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._additionalInformationFromExternal,
                                    name: 'additionalInformationFromExternal',
                                    inputValue: 1,
                                    checked: record.additionalInformationFromExternal ? record.additionalInformationFromExternal : 0
                                }),
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._activeTitle,
                                    name: 'active',
                                    inputValue: 1,
                                    checked: record.active ? record.active : 0
                                }),
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._globalTitle + '(' + panel._forAllCustomersTitle + ')',
                                    name: 'global',
                                    inputValue: 1,
                                    checked: record.global ? record.global : 0
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._projectLeadTitle,
                                    name: 'project_lead',
                                    allowBlank: true,
                                    store: projectLeadStore,
                                    queryMode: 'local',
                                    displayField: 'username',
                                    valueField: 'id',
                                    anchor: '100%',
                                    value: record.project_lead ? record.project_lead : ''
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._technicalLeadTitle,
                                    name: 'technical_lead',
                                    allowBlank: true,
                                    store: technicalLeadStore,
                                    queryMode: 'local',
                                    displayField: 'username',
                                    valueField: 'id',
                                    anchor: '100%',
                                    value: record.technical_lead ? record.technical_lead : ''
                                }),
                                {
                                    fieldLabel: panel._offerTitle,
                                    name: 'offer',
                                    anchor: '100%',
                                    enforceMaxLength: true,
                                    maxLength: 31,
                                    maxLengthText: panel._max31CharactersTitle,
                                    value: record.offer ? record.offer : ''
                                },
                                {
                                    fieldLabel: panel._costCenterTitle,
                                    name: 'cost_center',
                                    anchor: '100%',
                                    enforceMaxLength: true,
                                    maxLength: 31,
                                    maxLengthText: panel._max31CharactersTitle,
                                    value: record.cost_center ? record.cost_center : ''
                                }, new Ext.form.ComboBox({
                                    fieldLabel: panel._billingTitle,
                                    name: 'billing',
                                    store: billingStore,
                                    queryMode: 'local',
                                    lazyRenderer: true,
                                    displayField: 'displayname',
                                    valueField: 'value',
                                    multiSelect: false,
                                    typeAhead: true,
                                    triggerAction: 'all',
                                    anchor: '100%',
                                    value: record.billing ? record.billing : 0
                                }),
                                {
                                    fieldLabel: panel._estimationTitle,
                                    name: 'estimation',
                                    anchor: '100%',
                                    value: record.estimationText ? record.estimationText : ''
                                },
                                {
                                    fieldLabel: panel._internalJiraProjectKey,
                                    name: 'internalJiraProjectKey',
                                    anchor: '100%',
                                    value: record.internalJiraProjectKey ? record.internalJiraProjectKey : ''
                                },
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._internalJiraTicketSystem,
                                    name: 'internalJiraTicketSystem',
                                    allowBlank: true,
                                    store: this.ticketSystemStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    anchor: '100%',
                                    value: record.internalJiraTicketSystem ? record.internalJiraTicketSystem : ''
                                })
                            ],
                            buttons: [
                                {
                                    text: panel._saveTitle,
                                    scope: this,
                                    handler: function(btn) {
                                        var form = btn.up('form').getForm();
                                        var values = form.getValues();

                                        Ext.Ajax.request({
                                            url: url + 'project/save',
                                            params: values,
                                            scope: this,
                                            success: function(response) {
                                                window.close();
                                            },
                                            failure: function(response) {
                                                /*
                                                 * If responsetext is less than 200 chars long (means not an exception
                                                 * stack trace), use responsetext. If not, show common help/error text
                                                 */
                                                var message = response.responseText.length < 200
                                                    ? response.responseText
                                                    : panel._seriousErrorTitle;
                                                showNotification(panel._errorTitle, message, false);
                                            }
                                        });
                                    }
                                }
                            ]
                        })
                    ]
                });

                window.show();
            }
        });

        var userGrid = Ext.create('Ext.grid.Panel', {
            store: this.userStore,
            teamStore: this.teamStore,
            columns: [
                {
                    header: 'Id',
                    dataIndex: 'id',
                    hidden: true
                }, {
                    header: this._userNameTitle,
                    dataIndex: 'username',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._abbreviationTitle,
                    dataIndex: 'abbr',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._typeTitle,
                    dataIndex: 'type',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._teamsTitle,
                    dataIndex: 'teams',
                    flex: 1,
                    renderer: function(value) {
                        /* Display space seperated list of related teams */
                        var output = '';
                        Ext.each(value, function(teamId) {
                            if (isNaN(teamId)) {
                              return;
                            }
                            var team = userGrid.teamStore.getById(parseInt(teamId));
                            if (null == team) {
                              return;
                            }
                            if (output != '')
                                output += ', ';
                            output += team.data.name;
                        });
                        return output;
                    }
                }
            ],
            tbar: [
                {
                    text: this._addUserTitle,
                    iconCls: 'icon-add',
                    scope: this,
                    handler: function() {
                        userGrid.editUser();
                    }
                }
            ],
            listeners: {
                /* Right-click menu */
                itemcontextmenu: function(grid, record, item, index, event, options) {
                    event.stopEvent();

                    var contextMenu = Ext.create('Ext.menu.Menu', {
                        items: [
                            {
                                text: panel._editTitle,
                                iconCls: 'icon-edit',
                                scope: this,
                                handler: function() {
                                    this.editUser(record.data);
                                }
                            }
                        ]
                    });

                    contextMenu.showAt(event.xy);
                }
            },
            editUser: function(record) {
                if(!record) record = {};

                var teamStore = Ext.create('Netresearch.store.AdminTeams', {
                    autoLoad: false
                });

                var localesStore = new Ext.data.ArrayStore({
                    fields: ['value', 'displayname'],
                    data: [
                        ['de', 'Deutsch'],
                        ['en', 'English'],
                        ['es', 'Español'],
                        ['fr', 'Français'],
                        ['ru', 'Русский'],
                    ]
                });

                var window = Ext.create('Ext.window.Window', {
                    title: panel._editUserTitle,
                    modal: true,
                    width: 400,
                    layout: 'fit',
                    id: 'edit-user-window',
                    layout: 'fit',
                    listeners: {
                        destroy: {
                            scope: this,
                            fn: function() {
                                this.getStore().load();
                            }
                        }
                    },
                    items: [
                        new Ext.form.Panel({
                            bodyPadding: 5,
                            defaultType: 'textfield',
                            items: [
                                new Ext.form.field.Hidden({
                                    name: 'id',
                                    value: record.id ? record.id : 0
                                }), {
                                    fieldLabel: panel._userNameTitle,
                                    name: 'username',
                                    anchor: '100%',
                                    value: record.username ? record.username : ''
                                }, {
                                    fieldLabel: panel._abbreviationTitle,
                                    name: 'abbr',
                                    anchor: '100%',
                                    value: record.abbr ? record.abbr : ''
                                }, new Ext.form.ComboBox({
                                    fieldLabel: panel._languageTitle,
                                    name: 'locale',
                                    store: localesStore,
                                    queryMode: 'local',
                                    displayField: 'displayname',
                                    valueField: 'value',
                                    multiSelect: false,
                                    typeAhead: true,
                                    triggerAction: 'all',
                                    anchor: '100%',
                                    value: record.locale ? record.locale : 'de'
                                }), new Ext.form.ComboBox({
                                    fieldLabel: 'Typ',
                                    name: 'type',
                                    anchor: '100%',
                                    store: Ext.create('Ext.data.Store', {
                                        fields: ['type', 'name'],
                                        data: [
                                            { 'type':'DEV', 'name': panel._developerTitle},
                                            { 'type':'PL', 'name': panel._projectManagerTitle},
                                            { 'type':'CTL', 'name': panel._controllingTitle}
                                        ]
                                    }),
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'type',
                                    value: record.type ? record.type : ''
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._teamsTitle,
                                    name: 'teams[]',
                                    store: teamStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    multiSelect: true,
                                    typeAhead: true,
                                    triggerAction: 'all',
                                    anchor: '100%',
                                    listeners: {
                                        afterrender: function(field, value) {
                                            teamStore.load({
                                                params: {
                                                    team: field.getValue()
                                                }
                                            });
                                        },
                                        select: function(field, value) {
                                            teamStore.load({
                                                params: {
                                                    team: field.getValue()
                                                }
                                            });
                                        }
                                    },
                                    value: record.teams ? record.teams : ''
                                })
                            ],
                            buttons: [
                                {
                                    text: panel._saveTitle,
                                    scope: this,
                                    handler: function(btn) {
                                        var form = btn.up('form').getForm();
                                        var values = form.getValues();

                                        Ext.Ajax.request({
                                            url: url + 'user/save',
                                            params: values,
                                            scope: this,
                                            success: function(response) {
                                                window.close();
                                            },
                                            failure: function(response) {
                                                /*
                                                 * If responsetext is less than 200 chars long (means not an exception
                                                 * stack trace), use responsetext. If not, show common help/error text
                                                 */
                                                var message = response.responseText.length < 200
                                                    ? response.responseText
                                                    : panel._seriousErrorTitle;
                                                showNotification(panel._errorTitle, message, false);
                                            }
                                        });
                                    }
                                }
                            ]
                        })
                    ]
                });

                window.show();
            }
        });

        var teamGrid = Ext.create('Ext.grid.Panel', {
            userStore: this.userStore,
            store: this.teamStore,
            columns: [
                {
                    header: this._teamTitle,
                    dataIndex: 'name',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                }, {
                    header: this._teamLeadTitle,
                    dataIndex: 'lead_user_id',
                    flex: 1,
                    field: {
                        xtype: 'textfield',
                        lazyRender: true,
                        queryMode: 'local',
                        displayField: 'name',
                        valueField: 'lead_user_id',
                        anchor: '100%'
                    },
                    renderer: function(id) {
                        var record = this.userStore.getById(id);
                        return record ? record.get('username') : id;
                    }
                }
            ],
            tbar: [
                {
                    text: this._addTeamTitle,
                    iconCls: 'icon-add',
                    scope: this,
                    handler: function() {
                        teamGrid.editTeam();
                    }
                }
            ],
            listeners: {
                /* Right-click menu */
                itemcontextmenu: function(grid, record, item, index, event, options) {
                    event.stopEvent();

                    var contextMenu = Ext.create('Ext.menu.Menu', {
                        items: [
                            {
                                text: panel._editTitle,
                                iconCls: 'icon-edit',
                                scope: this,
                                handler: function() {
                                    this.editTeam(record.data);
                                }
                            }
                        ]
                    });

                    contextMenu.showAt(event.xy);
                }
            },
            editTeam: function(record) {
                var leadUserStore = Ext.create('Netresearch.store.AdminUsers');

                record = record || {};

                var window = Ext.create('Ext.window.Window', {
                    title: panel._editTeamTitle,
                    modal: true,
                    width: 400,
                    id: 'edit-team-window',<commit_msg>fix user loading in manage teams tab<commit_after>Ext.define('Netresearch.widget.Admin', {
    extend: 'Ext.tab.Panel',

    requires: [
        'Netresearch.store.AdminCustomers',
        'Netresearch.store.AdminProjects',
        'Netresearch.store.AdminTeams',
        'Netresearch.store.AdminUsers',
        'Netresearch.store.AdminPresets',
        'Netresearch.store.TicketSystems',
    ],

    /* Load all necessary stores */
    customerStore: Ext.create('Netresearch.store.AdminCustomers'),
    projectStore: Ext.create('Netresearch.store.AdminProjects'),
    userStore: Ext.create('Netresearch.store.AdminUsers'),
    teamStore: Ext.create('Netresearch.store.AdminTeams'),
    ticketSystemStore: Ext.create('Netresearch.store.TicketSystems'),
    activityStore: Ext.create('Netresearch.store.Activities'),
    presetStore: Ext.create('Netresearch.store.AdminPresets'),

    /* Strings */
    _tabTitle: 'Administration',
    _nameTitle: 'Name',
    _teamsTitle: 'Teams',
    _activeTitle: 'Active',
    _globalTitle: 'Global',
    _addCustomerTitle: 'Add customer',
    _editTitle: 'Edit',
    _editCustomerTitle: 'Edit customer',
    _forAllTeamsTitle: 'for all teams',
    _saveTitle: 'Save',
    _deleteTitle: 'Delete',
    _seriousErrorTitle: 'A serious error occurred. Find more details in Firebug or the Chrome Developer Tools.',
    _customerTitle: 'Customer',
    _ticketPrefixTitle: 'Ticket prefix',
    _ticketSystemTitle: 'Ticket system',
    _internalJiraTicketSystem: 'internal JIRA Ticket-System',
    _projectTitle: 'Project',
    _addProjectTitle: 'Add project',
    _editProjectTitle: 'Edit project',
    _forAllCustomersTitle: 'for all customers',
    _userNameTitle: 'User name',
    _abbreviationTitle: 'Abbr',
    _typeTitle: 'Type',
    _addUserTitle: 'Add user',
    _editUserTitle: 'Edit user',
    _languageTitle: 'Language',
    _developerTitle: 'Developer',
    _projectManagerTitle: 'Project manager',
    _controllingTitle: 'Controlling',
    _teamTitle: 'Team',
    _teamLeadTitle: 'Team lead',
    _addTeamTitle: 'Add team',
    _editTeamTitle: 'Edit team',
    _teamSavedTitle: 'The team has been successfully saved.',
    _customerManagementTitle: 'Customer management',
    _projectManagementTitle: 'Project management',
    _userManagementTitle: 'User management',
    _teamManagementTitle: 'Team management',
    _presetManagementTitle: 'Preset management',
    _presetSavedTitle: 'The preset has been successfully saved.',
    _addPresetTitle: 'Add preset',
    _editPresetTitle: 'Edit preset',
    _activityTitle: 'Activity',
    _descriptionTitle: 'Description',
    _ticketSystemManagementTitle: 'Ticket system management',
    _ticketSystemSavedTitle: 'The ticket system has been successfully saved.',
    _urlTitle: 'URL',
    _ticketUrlTitle: 'Ticket URL',
    _ticketUrlHint: '"%s" as placeholder for ticket name',
    _timebookingTitle: 'Time booking',
    _loginTitle: 'Login',
    _passwordTitle: 'Password',
    _publicKeyTitle: 'Public key',
    _privateKeyTitle: 'Private key',
    _addTicketSystemTitle: 'Add ticket system',
    _errorsTitle: 'Errors',
    _errorTitle: 'Error',
    _successTitle: 'Success',
    _estimationTitle: 'Estimated Duration',
    _internalJiraProjectKey: 'internal JIRA Project Key',
    _offerTitle: 'Offer',
    _billingTitle: 'Billing',
    _costCenterTitle: 'Cost Center',
    _projectLeadTitle: 'Project Lead',
    _technicalLeadTitle: 'Technical Lead',
    _max31CharactersTitle: 'At maximum 31 characters are allowed here',
    _activityManagementTitle: 'Activity management',
    _addActivityTitle: 'Add activity',
    _editActivityTitle: 'Edit activity',
    _activitySavedTitle: 'The activity has been successfully saved.',
    _needsTicketTitle: 'Needs ticket',
    _factorTitle: 'Factor',
    _oauthConsumerKeyTitle: 'OAuth consumer key',
    _oauthConsumerSecretTitle: 'OAuth consumer secret',

    initComponent: function () {
        this.on('render', this.refreshStores, this);

        var panel = this;

        var billingStore = new Ext.data.ArrayStore({
            fields: ['value', 'displayname'],
            data: [
                [0, 'None'],
                [1, 'Time And Material'],
                [2, 'Fixed Price']
            ]
        });

        var customerGrid = Ext.create('Ext.grid.Panel', {
            store: this.customerStore,
            teamStore: this.teamStore,
            columns: [
                {
                    header: 'Id',
                    dataIndex: 'id',
                    hidden: true
                }, {
                    header: this._nameTitle,
                    dataIndex: 'name',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._teamsTitle,
                    dataIndex: 'teams',
                    flex: 1,
                    renderer: function(value) {
                        var output = '';
                        /* Display space separated list of related teams */
                        Ext.each(value, function(teamId) {
                            if (isNaN(teamId)) {
                              return;
                            }
                            var team = customerGrid.teamStore.getById(parseInt(teamId));
                            if (null == team) {
                              return;
                            }
                            if (output != '')
                                output += ', ';
                            output += team.data.name;
                        });
                        return output;
                    }
                },
                {
                    header: this._activeTitle,
                    dataIndex: 'active',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                },
                {
                    header: this._globalTitle,
                    dataIndex: 'global',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                }
            ],
            tbar: [
                {
                    text: this._addCustomerTitle,
                    iconCls: 'icon-add',
                    scope: this,
                    handler: function() {
                        customerGrid.editCustomer();
                    }
                }
            ],
            listeners: {
                /* Right-click menu */
                itemcontextmenu: function(grid, record, item, index, event, options) {
                    event.stopEvent();

                    var contextMenu = Ext.create('Ext.menu.Menu', {
                        items: [
                            {
                                text: panel._editTitle,
                                iconCls: 'icon-edit',
                                scope: this,
                                handler: function() {
                                    this.editCustomer(record.data);
                                }
                            }
                        ]
                    });

                    contextMenu.showAt(event.xy);
                }
            },
            editCustomer: function(record) {
                if(!record) record = {};

                var teamStore = Ext.create('Netresearch.store.AdminTeams', {
                    autoLoad: false
                });

                var window = Ext.create('Ext.window.Window', {
                    title: panel._editCustomerTitle,
                    modal: true,
                    width: 400,
                    id: 'edit-customer-window',
                    layout: 'fit',
                    listeners: {
                        /* Reload on destroy */
                        destroy: {
                            scope: this,
                            fn: function() {
                                this.getStore().load();
                            }
                        }
                    },
                    items: [
                        new Ext.form.Panel({
                            bodyPadding: 5,
                            defaultType: 'textfield',
                            items: [
                                new Ext.form.field.Hidden({
                                    name: 'id',
                                    value: record.id ? record.id : 0
                                }), {
                                    fieldLabel: panel._nameTitle,
                                    name: 'name',
                                    anchor: '100%',
                                    allowBlank: false,
                                    minLength: 3,
                                    value: record.name ? record.name : ''
                                },
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._activeTitle,
                                    name: 'active',
                                    inputValue: 1,
                                    checked: record.active ? record.active : 0
                                }),
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._globalTitle + '(' + panel._forAllTeamsTitle + ')',
                                    name: 'global',
                                    inputValue: 1,
                                    checked: record.global ? record.global : 0
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._teamsTitle,
                                    name: 'teams[]',
                                    store: teamStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    anchor: '100%',
                                    typeAhead: true,
                                    multiSelect: true,
                                    triggerAction: 'all',
                                    //disabled: record.global ? true : false,
                                    listeners: {
                                        /* Reload teams column to fit information */
                                        afterrender: function(field, value) {
                                            teamStore.load({
                                                params: {
                                                    team: field.getValue()
                                                }
                                            });
                                        },
                                        select: function(field, value) {
                                            teamStore.load({
                                                params: {
                                                    team: field.getValue()
                                                }
                                            });
                                        }
                                    },
                                    value: record.teams ? record.teams : []
                                })
                            ],
                            buttons: [
                                {
                                    text: panel._saveTitle,
                                    scope: this,
                                    handler: function(btn) {
                                        var form = btn.up('form').getForm();
                                        if (!form.isValid()) {
                                            var fields = form.getFields();
                                            var errors = [];

                                            /* Create Error-String and display Error-Window */
                                            for (i = 0; i < fields.length; i++) {
                                                errors.push(fields.items[i].getErrors().join(', '));
                                            }

                                            var errorsWindow = new Ext.Window({
                                                title: panel._errorsTitle,
                                                html: errors,
                                                width: 350
                                            });

                                            return errorsWindow.show();
                                        }

                                        var values = form.getValues();
                                        Ext.Ajax.request({
                                            url: url + 'customer/save',
                                            params: values,
                                            scope: this,
                                            success: function(response) {
                                                window.close();
                                            },
                                            failure: function(response) {
                                                /* If response text is less than 200 chars long (means not an exception
                                                 * stack trace), use response text. If not, show common help/error text
                                                 */
                                                message = response.responseText.length < 200
                                                    ? response.responseText
                                                    : panel._seriousErrorTitle;
                                                showNotification(panel._errorTitle, message, false);
                                            }
                                        });
                                    }
                                }
                            ]
                        })
                    ]
                });

                window.show();
            }
        });

        var projectGrid = Ext.create('Ext.grid.Panel', {
            ticketSystemStore: this.ticketSystemStore,
            customerStore: this.customerStore,
            ticketSystemStore: this.ticketSystemStore,
            store: this.projectStore,
            columns: [
                {
                    header: 'Id',
                    dataIndex: 'id',
                    hidden: true
                }, {
                    header: this._nameTitle,
                    dataIndex: 'name',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._customerTitle,
                    dataIndex: 'customer',
                    flex: 1,
                    field: {
                        xtype: 'textfield',
                        lazyRender: true,
                        queryMode: 'local',
                        displayField: 'name',
                        valueField: 'id',
                        anchor: '100%'
                    },
                    renderer: function(id) {
                        var record = this.customerStore.getById(id);
                        return record ? record.get('name') : id;
                    }
                },
                {
                    header: this._ticketPrefixTitle,
                    dataIndex: 'jiraId',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._ticketSystemTitle,
                    dataIndex: 'ticket_system',
                    flex: 1,
                    field: {
                        xtype: 'textfield',
                        lazyRender: true,
                        queryMode: 'local',
                        store: this.ticketSystemStore,
                        displayField: 'name',
                        valueField: 'id',
                        anchor: '100%'
                    },
                    renderer: function(id) {
                        if (1 > parseInt(id))
                            return '';

                        var record = this.ticketSystemStore.getById(id);
                        return record ? record.get('name') : id;
                    }
                },
                {
                    header: this._additionalInformationFromExternal,
                    dataIndex: 'additionalInformationFromExternal',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                },
                {
                    header: this._activeTitle,
                    dataIndex: 'active',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                },
                {
                    header: this._globalTitle,
                    dataIndex: 'global',
                    field: {
                        xtype: 'checkbox'
                    },
                    renderer: function(value) {
                        return renderCheckbox(value);
                    }
                },
                {
                    header: this._offerTitle,
                    dataIndex: 'offer',
                    width: 70,
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._costCenterTitle,
                    dataIndex: 'cost_center',
                    width: 70,
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._billingTitle,
                    dataIndex: 'billing',
                    width: 70,
                    flex: 1,
                    field: {
                        xtype: 'textfield',
                        queryMode: 'local',
                        store: billingStore,
                        displayField: 'displayname',
                        valueField: 'value',
                        anchor: '100%'
                    },
                    renderer: function(value) {
                        var record = billingStore.findRecord('value', value);
                        return record ? record.get('displayname') : value;
                    }
                },
                {
                    header: this._estimationTitle,
                    dataIndex: 'estimation',
                    width: 100,
                    flex: 1,
                    align: 'right',
                    field: {
                        xtype: 'textfield'
                    },
                    renderer: function(value) {
                        return formatDuration(value, true);
                    }
                }
            ],
            tbar: [
                {
                    text: this._addProjectTitle,
                    iconCls: 'icon-add',
                    scope: this,
                    handler: function() {
                        projectGrid.editProject();
                    }
                }
            ],
            listeners: {
                /* Right-click menu */
                itemcontextmenu: function(grid, record, item, index, event, options) {
                    event.stopEvent();

                    var contextMenu = Ext.create('Ext.menu.Menu', {
                        items: [
                            {
                                text: panel._editTitle,
                                iconCls: 'icon-edit',
                                scope: this,
                                handler: function() {
                                    this.editProject(record.data);
                                }
                            }
                        ]
                    });

                    contextMenu.showAt(event.xy);
                }
            }, // end listeners
            editProject: function(record) {
                var projectLeadStore = Ext.create('Netresearch.store.AdminUsers');
                var technicalLeadStore = Ext.create('Netresearch.store.AdminUsers');
                var projectStore = Ext.create('Netresearch.store.AdminProjects', {
                    autoLoad: false
                });

                projectLeadStore.load();
                technicalLeadStore.load();

                if (!record) {
                    record = {};
                }

                var window = Ext.create('Ext.window.Window', {
                    title: panel._editProjectTitle,
                    modal: true,
                    width: 400,
                    id: 'edit-project-window',
                    layout: 'fit',
                    listeners: {
                        destroy: {
                            scope: this,
                            fn: function() {
                                this.getStore().load();
                            }
                        }
                    },
                    items: [
                        new Ext.form.Panel({
                            bodyPadding: 5,
                            defaultType: 'textfield',
                            items: [
                                new Ext.form.field.Hidden({
                                    name: 'id',
                                    value: record.id ? record.id : 0
                                }), {
                                    fieldLabel: panel._nameTitle,
                                    name: 'name',
                                    anchor: '100%',
                                    value: record.name ? record.name : ''
                                },
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._customerTitle,
                                    name: 'customer',
                                    store: this.customerStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    anchor: '100%',
                                    disabled: record.customer ? true : false,
                                    listeners: {
                                        afterrender: function(field, value) {
                                            projectStore.load({
                                                params: {
                                                    customer: field.getValue()
                                                }
                                            });
                                        },
                                        select: function(field, value) {
                                            projectStore.load({
                                                params: {
                                                    customer: field.getValue()
                                                }
                                            });
                                        }
                                    },
                                    value: record.customer ? record.customer : ''
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._ticketSystemTitle,
                                    name: 'ticket_system',
                                    allowBlank: true,
                                    store: this.ticketSystemStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    anchor: '100%',
                                    value: record.ticket_system ? record.ticket_system : ''
                                }),
                                {
                                    fieldLabel: panel._ticketPrefixTitle,
                                    name: 'jiraId',
                                    anchor: '100%',
                                    value: record.jiraId ? record.jiraId : ''
                                },
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._additionalInformationFromExternal,
                                    name: 'additionalInformationFromExternal',
                                    inputValue: 1,
                                    checked: record.additionalInformationFromExternal ? record.additionalInformationFromExternal : 0
                                }),
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._activeTitle,
                                    name: 'active',
                                    inputValue: 1,
                                    checked: record.active ? record.active : 0
                                }),
                                new Ext.form.field.Checkbox({
                                    fieldLabel: panel._globalTitle + '(' + panel._forAllCustomersTitle + ')',
                                    name: 'global',
                                    inputValue: 1,
                                    checked: record.global ? record.global : 0
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._projectLeadTitle,
                                    name: 'project_lead',
                                    allowBlank: true,
                                    store: projectLeadStore,
                                    queryMode: 'local',
                                    displayField: 'username',
                                    valueField: 'id',
                                    anchor: '100%',
                                    value: record.project_lead ? record.project_lead : ''
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._technicalLeadTitle,
                                    name: 'technical_lead',
                                    allowBlank: true,
                                    store: technicalLeadStore,
                                    queryMode: 'local',
                                    displayField: 'username',
                                    valueField: 'id',
                                    anchor: '100%',
                                    value: record.technical_lead ? record.technical_lead : ''
                                }),
                                {
                                    fieldLabel: panel._offerTitle,
                                    name: 'offer',
                                    anchor: '100%',
                                    enforceMaxLength: true,
                                    maxLength: 31,
                                    maxLengthText: panel._max31CharactersTitle,
                                    value: record.offer ? record.offer : ''
                                },
                                {
                                    fieldLabel: panel._costCenterTitle,
                                    name: 'cost_center',
                                    anchor: '100%',
                                    enforceMaxLength: true,
                                    maxLength: 31,
                                    maxLengthText: panel._max31CharactersTitle,
                                    value: record.cost_center ? record.cost_center : ''
                                }, new Ext.form.ComboBox({
                                    fieldLabel: panel._billingTitle,
                                    name: 'billing',
                                    store: billingStore,
                                    queryMode: 'local',
                                    lazyRenderer: true,
                                    displayField: 'displayname',
                                    valueField: 'value',
                                    multiSelect: false,
                                    typeAhead: true,
                                    triggerAction: 'all',
                                    anchor: '100%',
                                    value: record.billing ? record.billing : 0
                                }),
                                {
                                    fieldLabel: panel._estimationTitle,
                                    name: 'estimation',
                                    anchor: '100%',
                                    value: record.estimationText ? record.estimationText : ''
                                },
                                {
                                    fieldLabel: panel._internalJiraProjectKey,
                                    name: 'internalJiraProjectKey',
                                    anchor: '100%',
                                    value: record.internalJiraProjectKey ? record.internalJiraProjectKey : ''
                                },
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._internalJiraTicketSystem,
                                    name: 'internalJiraTicketSystem',
                                    allowBlank: true,
                                    store: this.ticketSystemStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    anchor: '100%',
                                    value: record.internalJiraTicketSystem ? record.internalJiraTicketSystem : ''
                                })
                            ],
                            buttons: [
                                {
                                    text: panel._saveTitle,
                                    scope: this,
                                    handler: function(btn) {
                                        var form = btn.up('form').getForm();
                                        var values = form.getValues();

                                        Ext.Ajax.request({
                                            url: url + 'project/save',
                                            params: values,
                                            scope: this,
                                            success: function(response) {
                                                window.close();
                                            },
                                            failure: function(response) {
                                                /*
                                                 * If responsetext is less than 200 chars long (means not an exception
                                                 * stack trace), use responsetext. If not, show common help/error text
                                                 */
                                                var message = response.responseText.length < 200
                                                    ? response.responseText
                                                    : panel._seriousErrorTitle;
                                                showNotification(panel._errorTitle, message, false);
                                            }
                                        });
                                    }
                                }
                            ]
                        })
                    ]
                });

                window.show();
            }
        });

        var userGrid = Ext.create('Ext.grid.Panel', {
            store: this.userStore,
            teamStore: this.teamStore,
            columns: [
                {
                    header: 'Id',
                    dataIndex: 'id',
                    hidden: true
                }, {
                    header: this._userNameTitle,
                    dataIndex: 'username',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._abbreviationTitle,
                    dataIndex: 'abbr',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._typeTitle,
                    dataIndex: 'type',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                },
                {
                    header: this._teamsTitle,
                    dataIndex: 'teams',
                    flex: 1,
                    renderer: function(value) {
                        /* Display space seperated list of related teams */
                        var output = '';
                        Ext.each(value, function(teamId) {
                            if (isNaN(teamId)) {
                              return;
                            }
                            var team = userGrid.teamStore.getById(parseInt(teamId));
                            if (null == team) {
                              return;
                            }
                            if (output != '')
                                output += ', ';
                            output += team.data.name;
                        });
                        return output;
                    }
                }
            ],
            tbar: [
                {
                    text: this._addUserTitle,
                    iconCls: 'icon-add',
                    scope: this,
                    handler: function() {
                        userGrid.editUser();
                    }
                }
            ],
            listeners: {
                /* Right-click menu */
                itemcontextmenu: function(grid, record, item, index, event, options) {
                    event.stopEvent();

                    var contextMenu = Ext.create('Ext.menu.Menu', {
                        items: [
                            {
                                text: panel._editTitle,
                                iconCls: 'icon-edit',
                                scope: this,
                                handler: function() {
                                    this.editUser(record.data);
                                }
                            }
                        ]
                    });

                    contextMenu.showAt(event.xy);
                }
            },
            editUser: function(record) {
                if(!record) record = {};

                var teamStore = Ext.create('Netresearch.store.AdminTeams', {
                    autoLoad: false
                });

                var localesStore = new Ext.data.ArrayStore({
                    fields: ['value', 'displayname'],
                    data: [
                        ['de', 'Deutsch'],
                        ['en', 'English'],
                        ['es', 'Español'],
                        ['fr', 'Français'],
                        ['ru', 'Русский'],
                    ]
                });

                var window = Ext.create('Ext.window.Window', {
                    title: panel._editUserTitle,
                    modal: true,
                    width: 400,
                    layout: 'fit',
                    id: 'edit-user-window',
                    layout: 'fit',
                    listeners: {
                        destroy: {
                            scope: this,
                            fn: function() {
                                this.getStore().load();
                            }
                        }
                    },
                    items: [
                        new Ext.form.Panel({
                            bodyPadding: 5,
                            defaultType: 'textfield',
                            items: [
                                new Ext.form.field.Hidden({
                                    name: 'id',
                                    value: record.id ? record.id : 0
                                }), {
                                    fieldLabel: panel._userNameTitle,
                                    name: 'username',
                                    anchor: '100%',
                                    value: record.username ? record.username : ''
                                }, {
                                    fieldLabel: panel._abbreviationTitle,
                                    name: 'abbr',
                                    anchor: '100%',
                                    value: record.abbr ? record.abbr : ''
                                }, new Ext.form.ComboBox({
                                    fieldLabel: panel._languageTitle,
                                    name: 'locale',
                                    store: localesStore,
                                    queryMode: 'local',
                                    displayField: 'displayname',
                                    valueField: 'value',
                                    multiSelect: false,
                                    typeAhead: true,
                                    triggerAction: 'all',
                                    anchor: '100%',
                                    value: record.locale ? record.locale : 'de'
                                }), new Ext.form.ComboBox({
                                    fieldLabel: 'Typ',
                                    name: 'type',
                                    anchor: '100%',
                                    store: Ext.create('Ext.data.Store', {
                                        fields: ['type', 'name'],
                                        data: [
                                            { 'type':'DEV', 'name': panel._developerTitle},
                                            { 'type':'PL', 'name': panel._projectManagerTitle},
                                            { 'type':'CTL', 'name': panel._controllingTitle}
                                        ]
                                    }),
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'type',
                                    value: record.type ? record.type : ''
                                }),
                                new Ext.form.ComboBox({
                                    fieldLabel: panel._teamsTitle,
                                    name: 'teams[]',
                                    store: teamStore,
                                    queryMode: 'local',
                                    displayField: 'name',
                                    valueField: 'id',
                                    multiSelect: true,
                                    typeAhead: true,
                                    triggerAction: 'all',
                                    anchor: '100%',
                                    listeners: {
                                        afterrender: function(field, value) {
                                            teamStore.load({
                                                params: {
                                                    team: field.getValue()
                                                }
                                            });
                                        },
                                        select: function(field, value) {
                                            teamStore.load({
                                                params: {
                                                    team: field.getValue()
                                                }
                                            });
                                        }
                                    },
                                    value: record.teams ? record.teams : ''
                                })
                            ],
                            buttons: [
                                {
                                    text: panel._saveTitle,
                                    scope: this,
                                    handler: function(btn) {
                                        var form = btn.up('form').getForm();
                                        var values = form.getValues();

                                        Ext.Ajax.request({
                                            url: url + 'user/save',
                                            params: values,
                                            scope: this,
                                            success: function(response) {
                                                window.close();
                                            },
                                            failure: function(response) {
                                                /*
                                                 * If responsetext is less than 200 chars long (means not an exception
                                                 * stack trace), use responsetext. If not, show common help/error text
                                                 */
                                                var message = response.responseText.length < 200
                                                    ? response.responseText
                                                    : panel._seriousErrorTitle;
                                                showNotification(panel._errorTitle, message, false);
                                            }
                                        });
                                    }
                                }
                            ]
                        })
                    ]
                });

                window.show();
            }
        });

        var teamGrid = Ext.create('Ext.grid.Panel', {
            userStore: this.userStore,
            store: this.teamStore,
            columns: [
                {
                    header: this._teamTitle,
                    dataIndex: 'name',
                    flex: 1,
                    field: {
                        xtype: 'textfield'
                    }
                }, {
                    header: this._teamLeadTitle,
                    dataIndex: 'lead_user_id',
                    flex: 1,
                    field: {
                        xtype: 'textfield',
                        lazyRender: true,
                        queryMode: 'local',
                        displayField: 'name',
                        valueField: 'lead_user_id',
                        anchor: '100%'
                    },
                    renderer: function(id) {
                        var record = this.userStore.getById(id);
                        return record ? record.get('username') : id;
                    }
                }
            ],
            tbar: [
                {
                    text: this._addTeamTitle,
                    iconCls: 'icon-add',
                    scope: this,
                    handler: function() {
                        teamGrid.editTeam();
                    }
                }
            ],
            listeners: {
                /* Right-click menu */
                itemcontextmenu: function(grid, record, item, index, event, options) {
                    event.stopEvent();

                    var contextMenu = Ext.create('Ext.menu.Menu', {
                        items: [
                            {
                                text: panel._editTitle,
                                iconCls: 'icon-edit',
                                scope: this,
                                handler: function() {
                                    this.editTeam(record.data);
                                }
                            }
                        ]
                    });

                    contextMenu.showAt(event.xy);
                }
            },
            editTeam: function(record) {
                var leadUserStore = Ext.create('Netresearch.store.AdminUsers');
                leadUserStore.load();
                record = record || {};

                var window = Ext.create('Ext.window.Window', {
                    title: panel._editTeamTitle,
                    modal: true,
                    width: 400,
                    id: 'edit-team-window',
{'fraction_non_alphanumeric': 0.0758954753784999, 'fraction_numerical': 0.0035189087038686273, 'mean_word_length': 0.6309738902469267, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 868, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2280482', 'n_tokens_mistral': 18123, 'n_tokens_neox': 16296, 'n_words': 3388}