Ext.namespace('NextLogic');

// Custom FormPanel supporting different behaviour for show/edit/new operating modes
NextLogic.FormPanel = Ext.extend(Ext.FormPanel, {
	
	//
	// properties (all Ext.FormPanel properties may be given as well)
	//

	// read-only properties
	okButton: null,
	cancelButton: null,
	currentMode: '',

	// the button texts for each mode, ok property may be false to hide okButton
	modeButtonTexts: {
		'show': { ok: false, cancel: 'Close' },
		'edit': { ok: 'Save', cancel: 'Cancel' },
		'new':	{ ok: 'Create', cancel: 'Cancel' }
	},
	
	// callbacks (default to no-ops)
	onCancel: Ext.emptyFn,
	onOk: Ext.emptyFn,
	
	//default form submission failure handling code
	defaultFailureHandler: function(form, action) {
		console.log('Default failure handler');
		switch (action.failureType) {
			case Ext.form.Action.CLIENT_INVALID:
			case Ext.form.Action.SERVER_INVALID:
				// validation errors are handled by the form, so we ignore them here
			break;
			case Ext.form.Action.CONNECT_FAILURE:
			case Ext.form.Action.LOAD_FAILURE:
				// these might be 404 Not Found or some 5xx Server Error
				Ext.Msg.alert("Couldn't update profile", "Error saving profile");
				break;
		}
	},
	defaultSuccessHandler: function(form, action) {
		
	},
	
	// defaults for (superclass) config
	cls:					 'ext-scaffold-form-panel',
	labelWidth:		100,							 // default label width
	defaults:			{ width: 220 },	// field width
	buttonAlign:	 'right',
	waitMsgTarget: true,
	bodyStyle:		 'padding:10px 10px 0',
	defaultType:	 'textfield',
	lazyRender: true,
	hideMode: 'offsets',

	//
	// initComponent
	//
	initComponent : function() {
		var scaffoldPanel = this;

		Ext.apply(this, {
		});
		
		NextLogic.FormPanel.superclass.initComponent.apply(this, arguments);
		var formPanel = this; // save scope for later reference
		
		// add buttons to FormPanel -> accessible as properties
		this.cancelButton = this.addButton({ text: 'Cancel', handler: formPanel.onCancel });
		this.okButton		 = this.addButton({ text: 'Save', handler: formPanel.onOk, type: 'submit' });
		//this.okButton.on('click', formPanel.onOk, this)
	},

	//
	// public instance methods
	//
	setFieldsDisabled: function(fieldsDisabled) {
		this.getForm().items.each(function(item) {
			item.setDisabled(fieldsDisabled);
		});
	},

	clearAllStoreFilters: function() {
		this.getForm().items.each(function(item) {
			if (item.store && item.store.clearFilter) item.store.clearFilter(true);
		});
	},
	
	setFormMode: function(mode) {
		if (mode === 'new') this.getForm().reset(); // empty all fields

		// disable fields and hide okButton when modeButtonTexts for ok is false
		//this.setFieldsDisabled(!this.modeButtonTexts[mode].ok);
		this.okButton.setVisible(this.modeButtonTexts[mode].ok);

		// set button texts according to mode
		this.okButton.setText(this.modeButtonTexts[mode].ok || '[disabled]');
		this.cancelButton.setText(this.modeButtonTexts[mode].cancel);
		
		// update currentMode property
		this.currentMode = mode;
	}
});

// register xtype
Ext.reg('nlform', NextLogic.FormPanel);


NextLogic.FormMessage = Ext.extend(Ext.form.FieldSet, {
	border: false,
	height: 'auto',
	width: 'auto',

	cls: 'form_message',
	bodyStyle: 'padding: 0 0 0 0',
	lazyRender: true,
	hideMode: 'offsets',
	tplMarkup: [
		'{message}'
	],
	
	setNotice: function(message) {
		this.tpl.overwrite(this.body, '<h4>' + message + '</h4>');
	},

	initComponent : function() {
		this.tpl = new Ext.Template(this.tplMarkup);
		
		NextLogic.FormMessage.superclass.initComponent.apply(this, arguments);
		this.on('render', function(panel){panel.tpl.overwrite(panel.body, {message: 'test'});});
	}

});

// register xtype
Ext.reg('form_message', NextLogic.FormMessage);
