/* 
File: rolemapManager.js

Summary:
This is the JavaScript file for role maps.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
Ext.onReady(function(){
	
	var rolemapStore = Ext.StoreMgr.get('rolemapStore');

	var roleStore = Ext.StoreMgr.get('roleStore');
	roleStore.load();

	roleStore.on('load', function(){
		rolemapStore.load();
	})

	var rolemapColumns = [
		{
			dataIndex: 'mapid',
			hidden: true
		},
		{
			header: "Role Name",
			sortable: true, 
			dataIndex: 'roleid',
			menuDisabled: true,
			renderer: renderRole
		},
		{
			header: "User ID",
			sortable: true, 
			dataIndex: 'userid',
			menuDisabled: true
		}
	];
	
	var rolemapSelectionModel = new Ext.grid.RowSelectionModel({
		singleSelect: true
	});
	
	var rolemapTbar = new Ext.Toolbar({
		id: 'rolemapTbar',
		items: [
			new Ext.Button({
				text: 'Add Role Map',
				tooltip:'Add a Security Role Map.',
				iconCls:'add',
				handler: function(){
					showroleWindow(1);
				}
			}),
			this.editrolemapMapButton = new Ext.Button({
				text: 'Edit Role Map',
				tooltip:'Edit a Role Map.',
				iconCls:'page_white_edit',
				disabled: true,
				handler: function(){
					showroleWindow(0);
				}
			}),
			this.deleteRoleMapButton = new Ext.Button({
				text: 'Delete Role',
				tooltip:'Delete a Role.',
				iconCls:'delete',
				disabled: true,
				handler: function(){
					Ext.MessageBox.confirm('Confirm', 'Are you sure you want to delete this role?', function (btn){
						if (btn == 'yes') {
							var r = rolemapSelectionModel.getSelected();
							mapidHidden.setValue(r.data.mapid);
							roleidCombo.setValue(r.data.roleid);
							useridTextField.setValue(r.data.userid);
							deletemapHidden.setValue(1);
							editrolemap(1);
						}
					});
				}
			})
		]
	});

	var rolemapGrid = new Ext.grid.GridPanel({
		store: rolemapStore,
		columns: rolemapColumns,
		sm: rolemapSelectionModel,
		autoHeight: true,
		viewConfig: {
			forceFit: true
		},
		id: 'rolemapPanel',
		renderTo: 'rolemapContainer',
		width: '100%',
		tbar: rolemapTbar
	});

	var rolemapFormPanel = new Ext.FormPanel({
		id: 'roleForm',
		region: 'center',
		margins: '3 3 3 3',
		bodyStyle: 'padding:5px 5px 5px',
		autoScroll: true,
		buttonAlign: 'left',
		defaultType: 'TextField',
		items:[
			this.deletemapHidden = new Ext.form.Hidden({
				name: 'deletemap',
				hideLabel: true
			}),
			this.mapidHidden = new Ext.form.Hidden({
				name: 'mapid',
				hideLabel: true
			}),
			this.roleidCombo = new Ext.form.ComboBox({
				fieldLabel: 'Role',
				name: 'roleid',
				store: roleStore,
				displayField: 'rolename',
				valueField: 'roleid',
				mode: 'local',
				editable: false,
				minChars: 1,
				typeAhead: true,
				typeAheadDelay: 100,
				allowBlank: false,
				triggerAction: 'all',
				width: 250
			}),
			this.useridTextField = new Ext.form.TextField({
				fieldLabel: 'User ID',
				name: 'userid',
				allowBlank: false,
				width: 250
			}),
			this.rolemapButton = new Ext.Button({
			text: 'Submit',
			handler: function(){
					editrolemap(0);
				}
			})
		]
	});	
	
	var rolemapWindow = new Ext.Window({
		modal: true,
		title: 'Security Role',
		closable: true,
		closeAction: 'hide',
		width: 400,
		height: 130,
		plain: false,
		layout: 'fit',
		items: [rolemapFormPanel]
	});




	rolemapSelectionModel.on('rowselect', changeButtonState);

	function changeButtonState(sm, rowindex, r){
		editrolemapMapButton.enable();
		deleteRoleMapButton.enable();
	};
	
	function showroleWindow(reset){
		rolemapWindow.show();
		deletemapHidden.setValue('');
		if(reset == 1){
			rolemapFormPanel.form.reset();
		}
		else{
			var r = rolemapSelectionModel.getSelected();
			rolemapFormPanel.form.loadRecord(r);
		}
	};
	
	function renderRole(value, cell, record, rowIndex, columnIndex, store){
		var rid = record.data.roleid;
		var r = roleStore.query('roleid', rid);
		var rn = r.items[0].data.rolename;
		return rn;
	};

	function editrolemap(isDelete) {
		var deletemapLocal = deletemapHidden.getValue();
		var mapidLocal = mapidHidden.getValue();
		var roleidLocal = roleidCombo.getValue();
		var useridLocal = useridTextField.getValue();
		var formPanel = rolemapFormPanel.form;

		if(formPanel.isValid()){
			Ext.Ajax.request({
				url: "./?event=editrolemap.action",
				params: {
					mapid: mapidLocal,
					roleid: roleidLocal,
					userid: useridLocal,
					deleteMap: deletemapLocal
				},
				success: function(response, options) {		
					var result = Ext.decode(response.responseText);
					Ext.MessageBox.alert('Success Message', result.msg, refreshrolemapStore);
					if(isDelete == 0){
						rolemapWindow.hide();
					}
				},
				failure: function(response, options) {
					var result = Ext.decode(response.responseText);
					Ext.MessageBox.alert('Failure Message', 'FAIL!');
				}
			});
		}
		else{
			Ext.MessageBox.alert('Missing Information', 'Please fill in the required fields.');
		}
	};
	
	function refreshrolemapStore() {
		rolemapStore.reload();
	};

});