/* $Id */

Ext.namespace('NGMP.admin');

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

	this.sidebarSubs = new Ext.data.SimpleStore({
		fields:['id', 'text'],
		data:[
			['filtersAntiSpam', LANG_anti_spam + ' ' + LANG_configuration],
			['filtersAntiVirus', LANG_anti_virus],
			['filtersLogs', LANG_logs]
		]
	});

	this.sidebarItems = new Ext.data.SimpleStore({
		fields:['sub', 'id', 'text','iconCls'],
		data:[
			['filtersAntiSpam', 'filtersAntiSpam_antiSpam', LANG_anti_spam + ' ' + LANG_settings,'icon-sub-antispam'],
			['filtersAntiSpam', 'filtersAntiSpam_denySenders',LANG_deny_senders,'icon-sub-deny-senders'],
			['filtersAntiSpam', 'filtersAntiSpam_allowSenders',LANG_allow_senders, 'icon-sub-allow-senders'],
			['filtersAntiVirus', 'filtersAntiVirus_anitVirus',LANG_anti_virus + ' ' + LANG_settings,'icon-sub-antivirus'],
			['filtersLogs', 'filtersLogs_spamToday', LANG_spam_received_today, 'icon-sub-log'],
			['filtersLogs', 'filtersLogs_spamTotal', LANG_spam_received_in_total, 'icon-sub-log'],
			['filtersLogs', 'filtersLogs_antivirusToday', LANG_antivirus_log_today, 'icon-sub-log'],
			['filtersLogs', 'filtersLogs_antivirusTotal', LANG_antivirus_log_total,'icon-sub-log']
			]
	});

	this.navPanel =  new NGMP.form.SideBar({
		width:210,
		region:'west',
		border:false,
		subs:this.sidebarSubs,
		subItems: this.sidebarItems,
		handler:function(a,b) {
			switch (b) {
				case 'filtersAntiSpam_antiSpam':
					this.centerPanel.getLayout().setActiveItem(this.antispamSettingsPanel);
					break;
				case 'filtersAntiSpam_denySenders':
					this.centerPanel.getLayout().setActiveItem(this.saDenySendersPanel);
					break;
				case 'filtersAntiSpam_allowSenders':
					this.centerPanel.getLayout().setActiveItem(this.saAllowSendersPanel);
					break;
				case 'filtersAntiVirus_anitVirus':
					this.centerPanel.getLayout().setActiveItem(this.avPanel);
					break;
				case 'filtersLogs_spamToday':
					this.centerPanel.getLayout().setActiveItem(this.logsAntiSpamToday);
					break;
				case 'filtersLogs_spamTotal':
					this.centerPanel.getLayout().setActiveItem(this.logsAntiSpamTotal);
					break;
				case 'filtersLogs_antivirusToday':
					this.centerPanel.getLayout().setActiveItem(this.logsAntiVirusToday);
					break;
				case 'filtersLogs_antivirusTotal':
					this.centerPanel.getLayout().setActiveItem(this.logsAntiVirusTotal);
					break;
				default:
					console.log('todo + '+b);
					break;
			}
		},
		scope:this
	});

	this.saFormEnableAntSpam = new Ext.form.Checkbox({
		fieldLabel:LANG_enable + ' ' + LANG_anti_spam,
		name:'filter_zeusd_enable'
	});
	this.saFormResetAntiSpam = new Ext.form.Checkbox({
		fieldLabel:LANG_reset_antispam,
		name:'reset_antispam'
	});

	this.saFormEnableAttachmentCheck = new Ext.form.Checkbox({
		fieldLabel:LANG_enable_attachment_check,
		fieldDescription: LANG_enable_attachment_check_description,
		name:'filter_attach_check'
	});

	this.saFormBlockedAttachments = new Ext.form.TextField({
		fieldLabel:LANG_blocked_attachments,
		fieldDescription:LANG_blocked_attachments_description,
		name:'filter_blocked_attachments'
	});

	this.saFormEnableRBL = new Ext.form.Checkbox({
		fieldLabel:LANG_enable_rbl_server,
		fieldDescription:LANG_enable_rbl_server_description,
		name:'filter_rbl_support'
	});
	this.saFormRBL = new Ext.form.TextField({
		fieldLabel:LANG_rbl_servers,
		fieldDescription:LANG_rbl_servers_description,
		name:'filter_rbl_servers'
	});
	this.antispamSettingsPanel = new NGMP.form.FormPanel({
		region:'center',
		title:LANG_anti_virus + ' ' + LANG_settings,
		url:'ngmp.php',
		baseParams:{
			func:'adminAntiSpam'
		},
		items:[this.saFormEnableAntSpam,this.saFormResetAntiSpam, this.saFormEnableAttachmentCheck,this.saFormBlockedAttachments,this.saFormEnableRBL, this.saFormRBL],
		buttons:[{
			text:LANG_save,
			handler:function() {
				this.antispamSettingsPanel.form.submit({
					params:{
						save:true
					}
				});
			},
			scope:this
		}]
	});

	this.antispamSettingsPanel.on('show', function(){
		this.antispamSettingsPanel.load();
	},this);
	this.saDenySendersGrid = new NGMP.grid.GridPanel({
		region:'center',
		border:'false',
		store: new NGMP.data.JsonStore({
			url:'ngmp.php',
			baseParams:{
				func:'adminAntiSpamDenySenders'
			},
			id:'expression',
			root:'results',
			fields:['expression']
		}),
		columns:[{
			header:LANG_expression,
			dataIndex:'expression'
		}],
		view: new Ext.grid.GridView({
			forceFit:true,
			autoFit:true
	 	}),
		loadMask:{
			msg:LANG_loading
		},
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		layout:'fit'
	});	
	this.saDenySendersFormExpression = new Ext.form.TextField({
		fieldLabel:LANG_expression,
		name:'expression'
	});
	this.btnSADenySendersDelete = new Ext.Button({
		text:LANG_delete,
		iconCls:'icon-delete',
		cls:'x-btn-text-icon',
		disabled:true,
		handler:function() {
			this.saDenySendersGrid.store.baseParams.delete_id = this.saDenySendersGrid.selModel.selections.keys[0];
			this.saDenySendersGrid.store.reload();
			delete this.saDenySendersGrid.store.baseParams.delete_id;
		},
		scope:this
	});
	this.saDenySendersTB = [ this.btnSADenySendersDelete,
		'-', this.saDenySendersFormExpression, {
		text:LANG_add,
		iconCls:'icon-add',
		cls:'x-btn-text-icon',
		handler:function() {
			this.saDenySendersGrid.store.baseParams.add_id = this.saDenySendersFormExpression.getValue();
			this.saDenySendersGrid.store.reload();
			delete this.saDenySendersGrid.store.baseParams.add_id;
		},
		scope:this
	}];
	this.saDenySendersGrid.selModel.on('selectionchange', function(A) {
		this.btnSADenySendersDelete.disable();
		if (A.selections.length === 1) {
			this.btnSADenySendersDelete.enable();
		}
	},this);

	this.saDenySendersPanel = new Ext.Panel({
		autoScroll:true,
		layout:'border',
		title:LANG_deny_senders,
		tbar:this.saDenySendersTB,
		items:[this.saDenySendersGrid]
	});
	this.saDenySendersPanel.on('show',function() {
		this.saDenySendersGrid.store.reload();
	},this);


	this.saAllowSendersGrid = new NGMP.grid.GridPanel({
		border:'false',
		region:'center',
		store: new NGMP.data.JsonStore({
			url:'ngmp.php',
			baseParams:{
				func:'adminAntiSpamAllowSenders'
			},
			id:'expression',
			root:'results',
			fields:['expression']
		}),
		columns:[{
			header:LANG_expression,
			dataIndex:'expression',
			sortable:true
		}],
		view: new Ext.grid.GridView({
			forceFit:true,
			autoFit:true
	 	}),
		loadMask:{
			msg:LANG_loading
		},
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		layout:'fit'
	});	
	this.saAllowSendersFormExpression = new Ext.form.TextField({
		fieldLabel:LANG_expression,
		name:'expression'
	});
	this.btnSAAllowSendersDelete = new Ext.Button({
		text:LANG_delete,
		iconCls:'icon-delete',
		cls:'x-btn-text-icon',
		disabled:true,
		handler:function() {
			this.saAllowSendersGrid.store.baseParams.delete_id = this.saAllowSendersGrid.selModel.selections.keys[0];
			this.saAllowSendersGrid.store.reload();
			delete this.saAllowSendersGrid.store.baseParams.delete_id;
		},
		scope:this
	});
	this.saAllowSendersTB = [ this.btnSAAllowSendersDelete,
		'-', this.saAllowSendersFormExpression, {
		text:LANG_add,
		iconCls:'icon-add',
		cls:'x-btn-text-icon',
		handler:function() {
			this.saAllowSendersGrid.store.baseParams.add_id = this.saAllowSendersFormExpression.getValue();
			this.saAllowSendersGrid.store.reload();
			delete this.saAllowSendersGrid.store.baseParams.add_id;
		},
		scope:this
	}];
	this.saAllowSendersGrid.selModel.on('selectionchange', function(A) {
		this.btnSAAllowSendersDelete.disable();
		if (A.selections.length === 1) {
			this.btnSAAllowSendersDelete.enable();
		}
	},this);
	this.saAllowSendersPanel = new Ext.Panel({
		autoScroll:true,
		layout:'border',
		title:LANG_allow_senders,
		items:[this.saAllowSendersGrid],
		tbar:this.saAllowSendersTB
	});
	this.saAllowSendersPanel.on('show',function() {
		this.saAllowSendersGrid.store.reload();
	},this);



	this.avPanelFormEnableAV = new Ext.form.Checkbox({
		fieldLabel:LANG_enable + ' ' +LANG_anti_virus,
		name:'filter_antivirus_enable'
	});

	this.avPanelNorth = new Ext.FormPanel({
		region:'north',
		items:[this.avPanelFormEnableAV]
	});

	this.avPanelGrid = new NGMP.grid.GridPanel({
		region:'center',
		cls:'ngmp-form-panel',
		title:LANG_installed_av_modules,
		store:new NGMP.data.JsonStore({
			url:'ngmp.php',
			baseParams:{
				func:'adminAV'
			},
			root:'results',
			fields:['name','status','last_update', 'expire_date']
		}),
		columns:[{
			header:LANG_av_name,
			dataIndex:'name'
		},{
			header:LANG_status,
			dataIndex:'status'
		},{
			header:LANG_last_update,
			dataIndex:'last_update',
			width:130
		},{
			header:LANG_expire_date,
			dataIndex:'expire_date'
		}]
	});
	this.avPanel = new NGMP.Panel({
		title:LANG_anti_virus + ' ' + LANG_settings,
		layout:'border',
		items:[this.avPanelNorth,this.avPanelGrid],
		buttons:[{
			text:LANG_save,
			handler:function() {
				this.avPanelGrid.store.baseParams.save = true;
				if (this.avPanelFormEnableAV.checked)
					this.avPanelGrid.store.baseParams.filter_antivirus_enable = true;
				this.avPanelGrid.store.reload();
				delete this.avPanelGrid.store.baseParams.filter_antivirus_enable;
				delete this.avPanelGrid.store.baseParams.save;
			},
			scope:this
		}]
	});

	this.avPanel.on('show', function() {
		this.avPanelGrid.store.reload();
	},this);
	this.avPanelGrid.store.on('load', function() {
		this.avPanelFormEnableAV.setValue(this.avPanelGrid.store.reader.jsonData.filter_antivirus_enable===true?true:false);
	},this);


	this.logsAntiSpamTodayGrid = new NGMP.grid.GridPanel({
		region:'center',	
		store: new NGMP.data.JsonStore({
			url:'ngmp.php',
			baseParams:{
				func:'adminLogsAntiSpamToday'
			},
			root:'results',
			id:'id',
			fields:['id', 'account', 'log_date', 'log_msg']
	   	}),
		columns:[{
			header:LANG_id,
			dataIndex:'id',
			sortable:true
		},{
			header:LANG_account,
			dataIndex:'account',
			sortable:true
		},{
			header:LANG_log_date,
			dataIndex:'log_date',
			sortable:true
		},{
			header:LANG_log_msg,
			dataIndex:'log_msg',
			sortable:true
		}],
		loadMask:{msg:LANG_loading},
		layout:'fit',
		view: new Ext.grid.GridView({
			autoFill:true,
			forceFit:true,
			emptyText: LANG_no_items
		})
	});
	this.logsAntiSpamToday = new NGMP.Panel({
		region:'center',		
		title:LANG_spam_received_today,
		items:[this.logsAntiSpamTodayGrid]
	});
	this.logsAntiSpamToday.on('show', function() {
		this.logsAntiSpamTodayGrid.store.reload();
	},this);

	this.logsAntiSpamTotalGrid = new NGMP.grid.GridPanel({
		region:'center',	
		store: new NGMP.data.JsonStore({
			url:'ngmp.php',
			baseParams:{
				func:'adminLogsAntiSpamTotal'
			},
			root:'results',
			id:'id',
			fields:['id', 'account', 'log_date', 'log_msg']
	   	}),
		columns:[{
			header:LANG_id,
			dataIndex:'id',
			sortable:true
		},{
			header:LANG_account,
			dataIndex:'account',
			sortable:true
		},{
			header:LANG_log_date,
			dataIndex:'log_date',
			sortable:true
		},{
			header:LANG_log_msg,
			dataIndex:'log_msg',
			sortable:true
		}],
		loadMask:{msg:LANG_loading},
		layout:'fit',
		view: new Ext.grid.GridView({
			autoFill:true,
			forceFit:true,
			emptyText: LANG_no_items
		})
	});

	this.logsAntiSpamTotal = new NGMP.Panel({
		region:'center',		
		title:LANG_spam_received_in_total,
		items:[this.logsAntiSpamTotalGrid]
	});
	this.logsAntiSpamTotal.on('show', function() {
		this.logsAntiSpamTotalGrid.store.reload();
	},this);

	this.logsAntiVirusTodayGrid = new NGMP.grid.GridPanel({
		region:'center',	
		store: new NGMP.data.JsonStore({
			url:'ngmp.php',
			baseParams:{
				func:'adminLogsAntiVirusToday'
			},
			root:'results',
			id:'id',
			fields:['id', 'account', 'log_date', 'log_msg']
	   	}),
		columns:[{
			header:LANG_id,
			dataIndex:'id',
			sortable:true
		},{
			header:LANG_account,
			dataIndex:'account',
			sortable:true
		},{
			header:LANG_log_date,
			dataIndex:'log_date',
			sortable:true
		},{
			header:LANG_log_msg,
			dataIndex:'log_msg',
			sortable:true
		}],
		loadMask:{msg:LANG_loading},
		layout:'fit',
		view: new Ext.grid.GridView({
			autoFill:true,
			forceFit:true,
			emptyText: LANG_no_items
		})
	});

	this.logsAntiVirusToday = new NGMP.Panel({
		region:'center',
		title:LANG_antivirus_log_today,
		items:[this.logsAntiVirusTodayGrid]
	});
	this.logsAntiVirusToday.on('show', function() {
		this.logsAntiVirusTodayGrid.store.reload();
	},this);

	this.logsAntiVirusTotalGrid = new NGMP.grid.GridPanel({
		region:'center',	
		store: new NGMP.data.JsonStore({
			url:'ngmp.php',
			baseParams:{
				func:'adminLogsAntiVirusTotal'
			},
			root:'results',
			id:'id',
			fields:['id', 'account', 'log_date', 'log_msg']
	   	}),
		columns:[{
			header:LANG_id,
			dataIndex:'id',
			sortable:true
		},{
			header:LANG_account,
			dataIndex:'account',
			sortable:true
		},{
			header:LANG_log_date,
			dataIndex:'log_date',
			sortable:true
		},{
			header:LANG_log_msg,
			dataIndex:'log_msg',
			sortable:true
		}],
		loadMask:{msg:LANG_loading},
		layout:'fit',
		view: new Ext.grid.GridView({
			autoFill:true,
			forceFit:true,
			emptyText: LANG_no_items
		})
	});

	this.logsAntiVirusTotal = new NGMP.Panel({
		region:'center',
		title:LANG_antivirus_log_total,
		items:[this.logsAntiVirusTotalGrid]
	});
	this.logsAntiVirusTotal.on('show', function() {
		this.logsAntiVirusTotalGrid.store.reload();
	},this);

	this.centerPanel = new NGMP.panel.CenterPanel({
		items:[this.logsAntiSpamToday,this.saDenySendersPanel, this.saAllowSendersPanel,this.avPanel,this.antispamSettingsPanel,this.logsAntiSpamTotal, this.logsAntiVirusToday, this.logsAntiVirusTotal]
	});


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


Ext.extend(NGMP.admin.FilterModule, Ext.Panel, {
});

NGMP.moduleManager.addAdminModule('filter', NGMP.admin.FilterModule, {
	title:LANG_filter,
	iconCls:'icon-filter'
});
