/* $Id */

Ext.namespace('NGMP.admin');

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

	this.sidebarSubStore = new Ext.data.SimpleStore({
		fields:['id','text'],
		data:[
			['maintenanceServerTools',LANG_server_tools]
		]
	});

	this.sidebarItemStore = new Ext.data.SimpleStore({
		fields:['sub', 'id', 'text','iconCls'],
		data:[
			['maintenanceServerTools','maintenanceServerTools_registration',LANG_registration,'icon-sub-registration'],
			['maintenanceServerTools','maintenanceServerTools_serverStat', LANG_server_stat,'icon-sub-server-stat'],
			['maintenanceServerTools','maintenanceServerTools_installModules', LANG_installed_modules,'icon-sub-installed-modules'],
			['maintenanceServerTools','maintenanceServerTools_whoIsOnline', LANG_who_is_online,'icon-sub-online-users'],
			['maintenanceServerTools','maintenanceServerTools_documentation', LANG_documentation,'icon-sub-documentation'],
			['maintenanceLogFiles','maintenanceLogFiles_statistics', LANG_statistics,'icon-sub-log-statistics'],
		]
	});

	this.navigationPanel = new NGMP.form.SideBar({
		width:210,
		region:'west',
		subs:this.sidebarSubStore,
		subItems:this.sidebarItemStore,
		handler:function(a,b) {
			switch (b) {
				case 'maintenanceServerTools_registration':
					this.centerPanel.getLayout().setActiveItem(this.registrationPanel);
					break;
				case 'maintenanceServerTools_installModules':
					this.centerPanel.getLayout().setActiveItem(this.installModulesPanel);
					break;
				case 'maintenanceServerTools_whoIsOnline':
					this.centerPanel.getLayout().setActiveItem(this.whoIsOnlinePanel);
					break;
				case 'maintenanceServerTools_serverStat':
					this.centerPanel.getLayout().setActiveItem(this.serverStatPanel);
					break;
				case 'maintenanceServerTools_documentation':
					window.open('http://www.prevantage.com/products/ngmp/doc');
					break;
				default:
					break;
			}
		},
		scope:this
	});

	this.regStore = new NGMP.data.JsonStore({
		url:'ngmp.php',
		baseParams:{
			func:'adminRegistration'
		},
		root:'results',
		fields:['name','value']
	});
	this.regGrid = new NGMP.grid.GridPanel({
		region:'center',
		store:this.regStore,
		border:false,
		disableSelection:true,
		enableHdMenu:false,
		columns:[{
			dataIndex:'name',
			width:40,
			sortable:false
		},{
			dataIndex:'value',
			sortable:false
		}],
		loadMask:{msg:LANG_loading},
		layout:'fit',
		view:new Ext.grid.GridView({
			autoFill:true,
			forceFit:true,
			emptyText:LANG_no_valid_license_file_available
		})
	});

	this.btnUploadLicense = new NGMP.form.BrowseButton({
		style:'margin-left:10px;',
		text:LANG_upload_license_file
	});

	this.formLicenseUpload = new Ext.FormPanel({
		border:false,
		region:'south',
		fileUpload:true,
		height:40,
		enctype:'multipart/form-data',
		items:[this.btnUploadLicense]
	});
	this.registrationPanel = new NGMP.Panel({
		title:LANG_registration,
		layout:'border',
		items:[this.regGrid, this.formLicenseUpload],
		buttons:[{
			text:LANG_save,
			handler:function() {
				this.formLicenseUpload.form.submit({
					url:'install.php?step=2&upload=true',
					method:'post',
					success:function(A,B) {
						Ext.Msg.alert(LANG_ok, LANG_license_upload_successfully);
					},
					scope:this
				});
			},
			scope:this
		}]
	});

	this.registrationPanel.on('show', function() {
		this.regStore.reload();
		this.registrationPanel.doLayout();
	},this);

	this.serverStatPanel = new Ext.Panel({
		region:'center',
		title:LANG_server_stat,
		tbar:[{
			text:LANG_day
		},{
			text:LANG_week
		},{
			text:LANG_month
		},'-', {
			text:LANG_prior
		},{
			text:LANG_next
		}]
	});


	this.installModulesStore = new NGMP.data.JsonStore({
		url:'ngmp.php',
		baseParams:{
			func:'adminInstalledModules'
		},
		root:'results',
		fields:['name','value']
	});
	this.installedModulesGrid = new NGMP.grid.GridPanel({
		region:'center',
		store:this.installModulesStore,
		border:false,
		disableSelection:true,
		enableHdMenu:false,
		columns:[{
			dataIndex:'name',
			sortable:false
		},{
			dataIndex:'value',
			sortable:false
		}],
		loadMask:{msg:LANG_loading},
		layout:'fit',
		view:new Ext.grid.GridView({
			autoFill:true,
			forceFit:true
		})
	});
	this.installModulesPanel = new Ext.Panel({
		region:'center',
		title:LANG_installed_modules,
		layout:'border',
		items:[this.installedModulesGrid]
	});

	this.installModulesPanel.on('show', function(){
		this.installModulesStore.reload();
		this.installModulesPanel.doLayout();
	},this);


	this.onlineUsersStore = new NGMP.data.JsonStore({
		url:'ngmp.php',
		baseParams:{
			func:'adminOnlineUsers'
		},
		root:'results',
		id:'uid',
		fields:['uid', 'account', 'real_name', {name:'last_login', type:'date', dateFormat:'Y-m-d H:i:s'}]
	});

	this.onlineUsersGrid = new NGMP.grid.GridPanel({
		region:'center',
		store:this.onlineUsersStore,
		border:false,
		columns:[{
			header:LANG_account,
			dataIndex: 'account',
			sortable:true
		},{
			header:LANG_real_name,
			dataIndex: 'real_name',
			sortable:true
		},{
			header:LANG_last_login,
			dataIndex: 'last_login',
			sortable:true,
			renderer: function(A) {
				if(A)
					return A.format(NGMP.preferences.settings.datetime_format);
				else
					return A;
			}
		}],
		loadMask:{msg:LANG_loading},
		layout:'fit',
		view:new Ext.grid.GridView({
			autoFill:true,
			forceFit:true
		})
	});

	this.whoIsOnlinePanel = new Ext.Panel({
		region:'center',
		border:false,
		title:LANG_who_is_online,
		layout:'fit',
		items:[this.onlineUsersGrid]
	});
	this.whoIsOnlinePanel.on('show', function() {
		this.onlineUsersStore.reload();
	},this);

	this.centerPanel = new NGMP.panel.CenterPanel({
		items:[this.registrationPanel, this.serverStatPanel, this.installModulesPanel,this.whoIsOnlinePanel]
	});

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

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

NGMP.moduleManager.addAdminModule('maintenance', NGMP.admin.MaintenanceModule, {
	title:LANG_maintenance,
	iconCls:'icon-maintenance'
});
