Ext.ns("TM.dialogs");

function registerEventAttendees(eventId, autoRefreshPage) {
    new TM.dialogs.RegisterEventAttendees(eventId, autoRefreshPage).show();
}

TM.dialogs.RegisterEventAttendees = function(eventId, autoRefreshPage) {
    this.eventId = eventId;
    this.autoRefreshPage = autoRefreshPage
    TM.dialogs.RegisterEventAttendees.superclass.constructor.call(this, null);
}

Ext.extend(TM.dialogs.RegisterEventAttendees, Ext.Window, {
    initComponent: function() {
        var store = new Ext.data.JsonStore({
            proxy: new Ext.data.HttpProxy({
                url: "/cms/feedpage.asp?url=/bespoke/json/eventattendeesadd.html",
                method: "GET"
            }),
            baseParams: {itemid: this.eventId},
            root: "rows",
            totalProperty: "count",
            id: "userid",
            fields: ["userid", "fullnamex", "jobtitle", "companyname"],
            remoteSort: true,
            sortInfo: {
                field: "u.lastname", 
                direction: "ASC"
            }
        });

        var keywordSearch = new Ext.form.TextField({
            emptyText: "Keyword search...",
            width: 150
        });

        this.grid = new Ext.grid.GridPanel({
			border: false,
			autoScroll: true,
			height: 430,
			store: store,
			cm: new Ext.grid.ColumnModel({
                defaultSortable: true,
                columns: [
                    {header: 'Name', dataIndex: 'fullnamex', width: 150},
                    {header: 'Title', dataIndex: 'jobtitle', width: 150},
                    {header: 'Company', dataIndex: 'companyname', width: 150}
                ],
                defaults: {
                    sortable: true
                }
            }),
			selModel: new Ext.grid.RowSelectionModel({singleSelect: false, keepSelections: true}),
            viewConfig: { forceFit: true },
			enableColLock: false,
			loadMask: true,
			tbar: [keywordSearch],
			bbar: new Ext.PagingToolbar({
                pageSize: 25,
                store: store,
                displayInfo: true,
                displayMsg: 'Displaying {0} - {1} of {2}',
                emptyMsg: "No entries to display"
            }),
			listeners: {
				rowdblclick: this.grid_rowdblclick,
                scope: this
			},
			plugins: new Ext.grid.GridFilters({
                filters: [{ 
                    type: 'keyword', 
                    dataIndex: "u.title,u.firstname,u.lastname,c.name,u.jobtitle", 
                    field: keywordSearch, 
                    value: ""
                }]
            })
		});

        Ext.apply(this, {
		    layout: "fit",
		    resizable: false,
		    width: 700,
		    height: 500,
		    plain: true,
		    modal: true,
		    closeAction: "hide",
		    title: "Select Attendees",
		    items: this.grid,
		    buttons: [{
			    text: "Apply",
                scope: this,
			    handler: this.apply_onclick
		    },{
			    text: "Close",
                scope: this,
			    handler: this.close_onclick
		    }]
        });

        store.load({params:{start: 0, limit: 25}});

        TM.dialogs.RegisterEventAttendees.superclass.initComponent.apply(this, arguments);
    },

    grid_rowdblclick: function(grid, rowIndex, e) {
		this.sendAction(grid.getStore().getAt(rowIndex).id);
        
    },

    apply_onclick: function() {
        this.sendAction(this.grid.selModel.selections.keys);
    },

    close_onclick: function() {
        this.hide();
    },

    sendAction: function(userIds) {
        this.el.mask("Saving...");
        Ext.Ajax.request({
            url: "/bespoke/actions.asp",
            scope: this,
            params: {
                "action": "eventattendeeadd",
                "userids": userIds,
                "eventid": this.eventId
            },
            success: function (response, options) {
                var json = Ext.decode(response.responseText);
                if (!json.success) {
                    Ext.MessageBox.show({
                        title: "Error",
                        msg: json.error,
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.ERROR
                    });
                } else {
                    if (this.autoRefreshPage) {
                        window.location = window.location.href;
                    }

                    this.close_onclick();
                }

                this.el.unmask();
                
            },
            failure: function (response, options) {
                //most often "transport" errors eg page not found, page crashed on server side, etc
                Ext.MessageBox.show({
                    title: "Error",
                    msg: response.status + ' ' + response.statusText,
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.ERROR
                });
                this.el.unmask();
            }
        });
    }
});
