/* $Id */

Ext.namespace('NGMP.admin');

NGMP.admin.DomainDialog = function() {
	this.addEvents({
		save:true
	});
	this.formDomainNameField = new Ext.form.TextField({
		fieldLabel:LANG_domain_name,
		name:'name',
		allowBlank:false,
		vtype:'domainname',
		vtypeText:LANG_not_valid_domain_name

	});

	this.formSignupEnable = new Ext.form.Checkbox({
		fieldLabel:LANG_enable + ' ' + LANG_sign_up,
		name:'signup'
	});

	this.basicFieldset = {
		xtype:'fieldset',
		title:LANG_basic,
		autoHeight:true,
		border:true,
		collapsed:false,
		defaults:{border:false, layout:'table'},
		items:[this.formDomainNameField,this.formSignupEnable]
	};

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

	this.mailboxQuotaField = new Ext.form.NumberField({
		fieldLabel:LANG_mailbox_quota + ' (MB)',
		name:'mailbox_quota'
	});
	this.filesQuotaField = new Ext.form.NumberField({
		fieldLabel:LANG_files_quota + ' (MB)',
		name:'files_quota'
	});
	this.allowSendMailToOtherDomain = new Ext.form.Checkbox({
		fieldLabel:LANG_allow_send_mail_to_other_domain,
		name:'allow_send_mail_to_other_domain'
	});
	this.allowReceivedMailFromOtherDomain = new Ext.form.Checkbox({
		fieldLabel:LANG_allow_received_mail_from_other_domain,
		name:'allow_received_mail_from_other_domain'
	});

	this.domainUsersStore = new NGMP.data.JsonStore({
		url:'ngmp.php',
		baseParams:{
			func:'adminGetDomainUsers'
		},
		id:'uid',
		root:'results',
		fields:['uid','account']
	});

	this.formNewAccountDefaults = new Ext.form.FormPanel({
		title:LANG_new_account_defaults,
		url:'ngmp.php',
		border:false,
		cls:'ngmp-form-panel',
		labelWidth:170,
		baseParams:{
			func:'adminDomainNewAccountDefaults'
		},
		items:[
			this.mailboxQuotaField, 
			this.filesQuotaField
		]
	});

	this.formNewAccountDefaults.on('show', function() {
		this.domainUsersStore.reload();
	},this);

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

	this.buttons = [{
		text:LANG_save,
		handler:function() {
			this.formNewAccountDefaults.form.submit({
				url:'ngmp.php',
				params:{
					save:true,
					id:this.id
				},
				scope:this
			});
			this.formPanel.form.submit({
				url:'ngmp.php',
				params:{
					save:true,
					id:this.id
				},
				success:function() {
					this.fireEvent('save', this);
					this.hide();
				},
				failure:function(B,C) {
					Ext.Msg.alert(LANG_error, C.result.feedback);	
				},
				scope:this
			});
		},
		scope:this
	},{
		text:LANG_cancel,
		handler:function() {
			this.hide();
		},
		scope:this
	}];
	NGMP.admin.DomainDialog.superclass.constructor.call(this, {
		title:LANG_domain,
		layout:'fit',
		modal:true,
		minWidth:300,
		minHeight:300,
		height:600,
		width:400,
		closeAction:'hide',
		items:[this.tabPanel]
	});
};

Ext.extend(NGMP.admin.DomainDialog, Ext.Window, {
	show:function(A) {
		this.acl = 0;
		if (!A)
			A = {};
		if (A.id) {
			this.id = A.id;
			this.formPanel.form.reset();
			this.formPanel.load({
				params:{
					id:A.id
				},
				waitMsg:LANG_loading,
				success:function(B,C) {
					this.acl = C.result.data.acl;
					this.setPermission();
					delete this.formPanel.baseParams.acl;
				},
				failure:function(B,C) {
				},
				scope:this
			});
			this.domainUsersStore.baseParams.domain_id = this.id;
			this.formNewAccountDefaults.form.reset();
			this.formNewAccountDefaults.load({
				params:{
					id:A.id
				},
				scope:this
			});
		} else {
			this.id = 0;
			this.formPanel.form.reset();
			this.formNewAccountDefaults.form.reset();
		}
		this.tabPanel.setActiveTab(0);
		this.setPermission();
		NGMP.admin.DomainDialog.superclass.show.call(this);
	},
	setPermission:function() {
		if (this.id === 0)
			this.formNewAccountDefaults.disable();
		else
			this.formNewAccountDefaults.enable();
	}
});

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

	this.domainListStore = new NGMP.data.JsonStore({
		url:'ngmp.php',
		baseParams:{
			func:'adminGetDomains'
		},
		root:'results',
		id:'id',
		fields:['id','name',{name:'enable', type:'boolean'},{name:'user_count',type:'int'}],
		remoteSort:false
	});

	this.domainList = new NGMP.grid.GridPanel({
		region:'center',
		titlebar:false,
		border:false,
		store:this.domainListStore,
		columns:[{
			header:LANG_name,
			sortable:true,
			width:80,
			dataIndex:'name'
		},new NGMP.grid.CheckColumn({
			header:LANG_enabled,
			dataIndex:'enable',
			width:30,
			sortable:true
		}),{
			header:LANG_user_count,
			width:20,
			dataIndex:'user_count',
			sortable:true
		}],
		view: new Ext.grid.GridView({
			autoFit:true,
			forceFit:true
		}),
		loadMask: {
			msg:LANG_loading
		},
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		layout:'fit'	
	});

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


	this.btnAdd = new Ext.Button({
		text:LANG_add,
		iconCls:'icon-add',
		cls:'x-btn-text-icon',
		handler:function() {
			if (!this.domainDialog)
				this.domainDialog = new NGMP.admin.DomainDialog();
			this.domainDialog.on('save', function() {
				this.reloadDomainList();
			},this);
			this.domainDialog.show({id:0});
		},
		scope:this
	});

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

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

	this.btnEnable = new Ext.Button({
		text:LANG_enable,
		iconCls:'icon-enable',
		cls:'x-btn-text-icon',
		disabled:true,
		handler:function() {
			this.domainListStore.baseParams.enable_id = this.domainList.selModel.selections.keys[0];
			this.domainListStore.reload();
			delete this.domainListStore.baseParams.enable_id;
		},
		scope:this
	});

	this.btnDisable = new Ext.Button({
		text:LANG_disable,
		cls:'x-btn-text-icon',
	 	iconCls:'icon-disable',
		disabled:true,
		handler:function() {
			this.domainListStore.baseParams.disable_id = this.domainList.selModel.selections.keys[0];
			this.domainListStore.reload();
			delete this.domainListStore.baseParams.disable_id;
		},
		scope:this
	});

	this.tbar = [
		this.btnAdd,
		this.btnEdit,
		this.btnDelete,
		'-',
		this.btnEnable,
		this.btnDisable
	];

	this.domainList.selModel.on('selectionchange', function(A){
		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.enable) {
				case true:
					this.btnDisable.enable();
					break;
				default:
					this.btnEnable.enable();
					break;
			}
		}
	},this);

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

Ext.extend(NGMP.admin.DomainModule, Ext.Panel, {
	onShow:function() {
		this.reloadDomainList();
		NGMP.admin.DomainModule.superclass.onShow.call(this);
	},
	afterRender:function() {
		this.reloadDomainList();
		NGMP.admin.DomainModule.superclass.afterRender.call(this);
	},
	reloadDomainList:function() {
		this.domainListStore.reload();
	},
	editDomain:function() {
		if (!this.domainDialog)
			this.domainDialog = new NGMP.admin.DomainDialog();
		this.domainDialog.show({id:this.domainList.selModel.selections.keys[0]});
	},
	deleteDomain:function() {
		//check user empty
		var user_count = this.domainList.selModel.selections.items[0].data.user_count;
		if (user_count > 0) {
			Ext.Msg.show({
				title:LANG_error,
				msg: LANG_domain_is_not_empty,
				buttons:Ext.Msg.OK,
				animEl: 'elId',
				icon: Ext.Msg.ERROR
			});
		} else {
			this.domainListStore.baseParams.delete_id = this.domainList.selModel.selections.keys[0];
			this.domainListStore.reload();
			delete this.domainListStore.baseParams.delete_id;
		}
	}
});

NGMP.moduleManager.addAdminModule('domain', NGMP.admin.DomainModule, {
	title:LANG_domain,
	iconCls:'icon-domain'
});
