Ext.namespace('CC');
Ext.namespace('CC.b');
Ext.namespace('CC.b.mod');

CC.b.mod.currency = function(){

	return{
	
		config : {},
	
		init : function(){
			CC.f.mod.currency.init();
		}
	}

}();

Ext.namespace('CC.f');
Ext.namespace('CC.f.mod');

CC.f.mod.currency = function(){
	
	return{
		
		ajaxUrl : CC_baseURL +'currency/',
		
		grids : {},
		formItems : [],
		windows: [],
		
		warehouse_changed : false,
		last_movement_id : null,
				
		init : function(){
			eval( 'this.'+ CC.b.mod.currency.config.start_event +'();' );
		},
		
		list : function () {
			var tbarConfig = [ 
	        		{
	        			text : CC.b.enter.config.gridNewRecord,
	        			handler : CC.f.mod.currency.showAddForm
	        		}
        		];
        	
        	//						modulNameParam, gridName, ajaxUrl, primaryField, tbarConfig
			CC.help.grid.initGrid(
				'currency', 
				{
					dataIndex : 'price_currency',
					ajaxUrl : 'extBase',
					primaryField : 'ID'
				}, 
				tbarConfig
			);
			CC.main.layout.center.add(this.grids.price_currency.grid);
			CC.main.layout.center.doLayout();
			this.grids.price_currency.grid.on('rowdblclick', this.displayEditorWindowByDblClick );
			
		},
		
		displayEditorWindow : function( dbId ){
			
			var request_params = null;
			request_params = {};
			if ( dbId != null ) {
				request_params.ID = dbId;
			}
			
			var param ={
				singleInstance : true,
				url: CC.f.mod.currency.ajaxUrl +'extBaseform',
				params: request_params,
				success: function(result){
					CC.b.mod.currency.config.formData.editForm = result;
					//makeitems(formleíró adatok, modul neve, formon belüli index, gridconfig helye helye)
					CC.help.form.makeItems( CC.b.mod.currency.config.formData.editForm.items, 'currency', 'edit', CC.b.mod.currency.config.grids.price_currency);
					CC.help.form.showWindow( CC.b.mod.currency.config.formData.editForm.items, 'currency', 'edit', 'doSaveRecord', CC.b.mod.currency.config.formData.editForm.window.edit);
					CC.f.mod.currency.windows.edit.dbId = dbId;
				}
			};	
			CC.help.ajax.doAjax(param);
		},
		
		displayEditorWindowByDblClick : function( gridObject, rowIndex ){
			var key = null;
			var gridObject = CC.f.mod.currency.grids.price_currency.grid;
			if ( typeof gridObject.selModel.selections.keys == 'object' ) {
				key = gridObject.selModel.selections.keys[0];
			} else {
				key = gridObject.selModel.selections.keys;
			}
			CC.f.mod.currency.displayEditorWindow( key );
		},
		
		showAddForm : function(){
			CC.f.mod.currency.displayEditorWindow( null )
		},
		
		doSaveRecord : function(){
			var ok = CC.help.form.validateForm(CC.f.mod.currency.formItems.edit);
			if ( ok ) {
				var data = CC.help.form.getFormData(CC.f.mod.currency.formItems.edit);
				data['default']['ID'] = CC.f.mod.currency.windows.edit.dbId;
				
				//CCdebug( 'data', data );
				
				var param ={
					url: CC.f.mod.currency.ajaxUrl +'extBasesave',
					params: {
						formdata: Ext.encode( data )
					},
					success: function(result){
						if ( result.success ) {
							//CC.f.mod.currency.windows.edit.close();
						} else {
							Ext.MessageBox.alert( CC.b.enter.config.msgError, result.message );
						}
						CC.f.mod.currency.grids.price_currency.gridStore.reload();
					}
				};	
				CC.help.ajax.doAjax(param);
			} else {
				Ext.MessageBox.alert( CC.b.enter.config.msgError, CC.b.enter.config.msgCheckField );
			}
			
			return ok;
		},
		
		doDelete : function( key ) {
			Ext.MessageBox.confirm('', 
				CC.b.mod.currency.config.deleteQuestion, 
				function( btn )
				{
					if (btn == 'yes')
					{
						CC.f.mod.currency.deleteFromDatabase( key );
					}
				}
			);
		},
		
		deleteFromDatabase : function( pDbId ) {
			var param ={
				url: CC.f.mod.currency.ajaxUrl +'extBasedelete',
				params: {
					ID: pDbId
				},
				success: function(result){
					if ( !result.success ) {
						Ext.MessageBox.alert( CC.b.enter.config.msgError, result.message );
					}
					CC.f.mod.currency.grids.price_currency.gridStore.reload();
				}
			};	
			CC.help.ajax.doAjax(param);
		}
		
	}
	
}();
