/* $Id */

Ext.namespace('NGMP.admin');

NGMP.admin.UserDialog = function() {
	this.addEvents({
		save:true
	});

	this.formDomainNameField = new Ext.form.Hidden({
		name:'domain',
		id:'domain_name',
		value:''
	});

	this.formUserNameField = new Ext.form.TextField({
		fieldLabel:LANG_username,
		allowBlank:false,
		name:'username',
		validateValue:function(value) {
			var domainNameField = Ext.getCmp('domain_name');
			domainNameField.setValue(Ext.getCmp('domain').lastSelectionText);
			var _mail = value + '@' + domainNameField.value;
			if (!validateEmail(_mail)) {
				this.markInvalid(LANG_invalid_email);
				return false;
			}
			Ext.Ajax.request({
				url:'ngmp.php',
				params:{
					func:'checkAccountExists',
					account:_mail
				},
				success:function(A,B) {
					var C = Ext.decode(A.responseText);
					if (C.exists === true) {
						this.markInvalid(LANG_account_exists);
						return false;
					}
				},
				scope:this
			});
			this.markInvalid('');
			delete domainNameField;
			delete _mail;
			return true;
		},
		scope:this
	});

	this.formDomainField = new Ext.form.ComboBox({
		fieldLabel:LANG_domain,
		width:220,
		hiddenName:'domain_id',
		id:'domain',
		value:'',
		store: new NGMP.data.JsonStore({
			url:'ngmp.php',
			baseParams:{
				func:'adminGetDomains'
			},
			root:'results',
			id:'id',
			fields:['id', 'name']
		}),
		displayField:'name',
		valueField:'id',
		mode:'local',
		triggerAction:'all',
		editable:false,
		selectOnFocus:true,
		forceSelection:true,
		allowBlank:false
	});

	this.formPasswordField = new Ext.form.TextField({
		fieldLabel:LANG_password,
		allowBlank:false,
		inputType:'password',
		name:'password'
	});


	this.formFirstNameField = new Ext.form.TextField({
		fieldLabel:LANG_name,
		name:'first_name',
		maxLength:20,
		allowBlank:false,
		width:80
	});

	this.formMiddleNameField = new Ext.form.TextField({
		fieldLabel:'',
		labelSeparator:'',
		hideLabel:true,
		name:'middle_name',
		maxLength:20,
		allowBlank:true,
		width:50,
		style:'margin-left:3px;'
	});
	this.formLastNameField = new Ext.form.TextField({
		fieldLabel:'',
		labelSeparator:'',
		hideLabel:true,
		width:100,
		name:'last_name',
		maxLength:80,
		allowBlank:false,
		style:'margin-left:3px;'
	});

	this.basicFieldset = {
		xtype:'fieldset',
		title:LANG_basic,
		autoHeight: true,
		border:true,
		collapsed:false,
		defaults:{border:false, layout:'form'},
		items:[
			this.formDomainField,
			this.formUserNameField,
			this.formPasswordField
		]
	};

	this.personalFieldset = {
		xtype:'fieldset',
		title:LANG_personal_details,
		autoHeight:true,
		border:true,
		collapsed:false,
		defaults:{border:false, layout:'table'},
		items:[{
			defaults:{border:false, layout:'form'},
		 	items:[{
				labelWidth:40,
				items:[this.formFirstNameField]
			},{
				items:[this.formMiddleNameField]
			},{
				items:[this.formLastNameField]
			}]
		}]
	};
	

	this.formProperties = new Ext.form.FormPanel({
		title:LANG_properties,
		url:'ngmp.php',
		border:false,
		cls:'ngmp-form-panel',
		baseParams:{
			func:'adminUser'
		},
		titlebar:false,
		items:[this.basicFieldset,this.formDomainNameField, this.personalFieldset]
	});

	this.tabPanel = new Ext.TabPanel({
		activeTab:0,
		deferredRender:false,
		border:false,
		anchor:'100% 100%',
		items:[this.formProperties]
	});

	this.buttons = [{
		text:LANG_save,
		handler:function() {
			this.formProperties.baseParams.save = 'true';
			this.formProperties.form.submit({
				success:function() {
					delete this.formProperties.baseParams.save;
					this.fireEvent('save', this);
				},
				scope:this
			});
			this.hide();
		},
		scope:this
	},{
		text:LANG_cancel,
		handler:function() {
			this.hide();
		},
		scope:this
	}];

	NGMP.admin.UserDialog.superclass.constructor.call(this, {
		title:LANG_user,
		layout:'fit',
		modal:true,
		minWidth:300,
		minHeight:300,
		height:600,
		width:400,
		closeAction:'hide',
		items:[this.tabPanel]
	});
};

Ext.extend(NGMP.admin.UserDialog, Ext.Window, {
	show:function(A) {
		this.formProperties.form.reset();
		if (!A)
			A = {};
		if (A.id)
			this.id = A.id;
		else
			this.id = 0;
		this.formProperties.baseParams.id = this.id;
		this.formProperties.load();
		if (this.id > 0) {
			this.formUserNameField.disable();
			this.formDomainField.disable();
		}
		this.tabPanel.setActiveTab(0);
		this.formDomainField.store.reload();
		NGMP.admin.UserDialog.superclass.show.call(this);
	}
});


NGMP.admin.UserModule = function(A) {
	if (!A) {
		A = {};
	}
	Ext.apply(this, A);


	this.usersStore = new Ext.data.GroupingStore({
		reader:new Ext.data.JsonReader({
			root:'results',
			id:'uid',
			fields:['uid','domain','account','first_name', 'middle_name', 'last_name', {name:'status', type:'int'}]
		}),
		proxy: new Ext.data.HttpProxy({
			url:'ngmp.php'
		}),
		baseParams:{
			func:'adminGetUsers'
		},
		groupField:'domain',
		sortInfo:{
			field:'account',
			direction:'ASC'
		},
		remoteSort: false
	});

	this.btnEnable = new Ext.Button({
		iconCls:'icon-enable',
		text:LANG_enable,
		cls:'x-btn-text-icon',
		disabled: true,
		handler:function() {
			this.changeUserStatus(1);
		},
		scope:this
	});

	this.btnDisable = new Ext.Button({
		iconCls:'icon-disable',
		text:LANG_disable,
		cls:'x-btn-text-icon',
		disabled: true,
		handler:function() {
			this.changeUserStatus(0);
		},
		scope:this
	});

	this.btnApprove = new Ext.Button({
		iconCls:'icon-approve',
		text:LANG_approve,
		cls:'x-btn-text-icon',
		disabled: true,
		handler:function() {
			this.changeUserStatus(1);
		},
		scope:this
	});

	this.btnEdit = new Ext.Button({
		iconCls:'icon-edit-user',
		text:LANG_edit,
		cls:'x-btn-text-icon',
		disabled: true,
		handler:function() {
			if (this.usersList.selModel.selections.keys.length ===1)
				this.editUser();
		},
		scope:this
	});

	this.btnDelete = new Ext.Button({
		iconCls:'icon-delete-user',
		text:LANG_delete,
		cls:'x-btn-text-icon',
		disabled: true,
		handler:function() {
			if (this.usersList.selModel.selections.keys.length === 1)
				this.deleteUser();
		},
		scope:this
	});

	this.tbar = [{
		text:LANG_add,
		iconCls:'icon-add-user',
		handler:function() {
			if (!this.userDialog)
				this.userDialog = new NGMP.admin.UserDialog();
			this.userDialog.on('save', function(){
				this.usersStore.reload();
			},this);
			this.userDialog.show({id:0});
		},
		scope:this
	},
		this.btnEdit,
		this.btnDelete,
		'-',
		this.btnEnable,
		this.btnDisable,
		this.btnApprove
	];

	this.usersList = new NGMP.grid.GridPanel({
		region:'center',
		titlebar:false,
		border:false,
		store:this.usersStore,
		paging:true,
		enableColumnHide:true,
		enableColumnMove:true,
		autoScroll:true,
		columns:[{
			header:LANG_account,
			sortable:true,
			dataIndex:'account'
		},{
			header:LANG_domain,
			sortable:true,
			dataIndex:'domain'
		},{
			header:LANG_first_name,
			sortable:true,
			dataIndex:'first_name'
		},{
			header:LANG_middle_name,
			sortable:true,
			dataIndex:'middle_name'
		},{
			header:LANG_last_name,
			sortable:true,
			dataIndex:'last_name'
		},{
			header:LANG_status,
			sortable:true,
			dataIndex:'status',
			renderer:function(A) {
				switch (A) {
					case 0:
						return LANG_disabled;
						break;
					case 1:
						return LANG_enabled;
						break;
					default:
						return LANG_wait_for_approval;
						break;
				}
			}
		}],
		view: new Ext.grid.GroupingView({
			autoFit:true,
			hideGroupedColumn:true,
			showGroupName:false,
			forceFit:true
		}),
		loadMask:{
			msg:LANG_loading
		},
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		layout:'fit'
	});

	this.usersList.selModel.on('selectionchange', function(A) {
		this.btnApprove.disable();
		this.btnEdit.disable();
		this.btnDelete.disable();
		this.btnEnable.disable();
		this.btnDisable.disable();
		if (A.selections.length === 1) {
			this.btnEdit.enable();
			this.btnDelete.enable();
			switch (A.selections.items[0].data.status) {
				case 0:
					this.btnEnable.enable();
					break;
				case 1:
					this.btnDisable.enable();
					break;
				default:
					this.btnApprove.enable();
					break;
			}
		}
	},this);

	this.usersList.on('dblclick', function() {
		if (this.usersList.selModel.selections.keys.length ===1)
			this.editUser();
	},this);

	NGMP.admin.UserModule.superclass.constructor.call(this, {
		layout:'border',
		items:[this.usersList]
	});
};

Ext.extend(NGMP.admin.UserModule, Ext.Panel, {
	onShow:function() {
		this.reloadUsers();
		NGMP.admin.UserModule.superclass.onShow.call(this);
	},
	afterRender:function() {
		this.reloadUsers();
		NGMP.admin.UserModule.superclass.afterRender.call(this);
	},
	reloadUsers:function() {
		this.usersStore.reload();
	},
	deleteUser:function() {
		this.usersStore.baseParams.delete_id = this.usersList.selModel.selections.keys[0];
		this.usersStore.reload();
		delete this.usersStore.baseParams.delete_id;
	},
	changeUserStatus:function(A) {
		this.usersStore.baseParams.update_id = this.usersList.selModel.selections.keys[0];
		this.usersStore.baseParams.update_status = A;
		this.usersStore.reload();
		delete this.usersStore.baseParams.update_id;
		delete this.usersStore.baseParams.update_status;
	},
	editUser:function() {
		if (!this.userDialog)
			this.userDialog = new NGMP.admin.UserDialog();
		this.userDialog.on('save', function() {
			this.usersStore.reload();
		},this);
		this.userDialog.show({id:this.usersList.selModel.selections.keys[0]});
	}
});

NGMP.moduleManager.addAdminModule('user', NGMP.admin.UserModule, {
	title:LANG_user,
	iconCls:'icon-user'
});
