/**
 * @class SearchNamePanel
 * @extends Ext.form.FormPanel
 *
 * The SearchPlanIdPanel is located in the left bar of the page, allowing to search plans by name
 *
 * @author ssmeman
 * @version 1.0
 */

/**
 * @constructor
 * @param {Object} config Configuration options
 */
Roo.gui.SearchNamePanel = Ext.extend(Ext.form.FormPanel, {
	//texts used on elements of this panel
	titleText: messages.searchNamePanel.title()
	,comboLoadingText: messages.combo.loadingText()
	,buttonSearch: messages.button.searchText()
	,buttonErase: messages.button.eraseText()

	,id: 'searchNamePanel'

	,initComponent:function() {
		this.addEvents(
			'selectedPlan'
			,'foundPlans'
			,'searchedPlanName'
		);

		var config = {
			title: this.titleText
			,autoHeight: true

			,defaults: {
				allowBlank: true
				,hideLabel: true
				,width: 185
			}
			,items:[{
				xtype: 'label'
				,text: 'Zoek op plannaam'
			},{
				id: 'searchNamePanel_name'
				,xtype: 'textfield'
				,regex: /^(\S+\s?)*$/
				,validationDelay: 100
			}]
			,buttons: [{
				text: this.buttonSearch
				,handler: this.onSearch
			},{
				text: this.buttonErase
				,handler: function(t,e) {
					t.findParentByType('searchNamePanel').getForm().reset();
				}
			}]
			,keys: {
				key: Ext.EventObject.ENTER
				,scope: this
				,handler: function() {
					this.onSearch(this.buttons[0],null);
				}
			}
		};

		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);

		// call parent initComponent
		Roo.gui.SearchNamePanel.superclass.initComponent.apply(this, arguments);

		eventHub.on({
			scope: this
			,'searchedPlanCriteria': this.onClear
			,'searchedLocation': this.onClear
			,'searchedPlanId': this.onClear
			,'changedFilter': function(parameters) {
				if(parameters.filter != "NONE") {
					this.getForm().reset();
				}
			}
			,'expandedSearchCriteriaPanel': function() {
				this.collapse();
			}
			,'expandedSearchPlanIdPanel': function() {
				this.collapse();
			}
			,'expandedSearchLocationPanel': function() {
				this.collapse();
			}
		});

	} // e/o function initComponent

	,onSearch: function(t,e) {
		Ext.getCmp('searchNamePanel').fireEvent('searchedPlanName');

		var naamValue = '%' + Ext.getCmp('searchNamePanel_name').getRawValue() + '%';

		plansStore.load({
			params: {
				query: {nameLike: naamValue}
			}
			,callback: function(records, options, success) {
				if (success) {

					if(records.length == 1 && records[0].json.planTooMuch == true) {
						//do nothing
					} else if (records.length == 0) {
						Ext.Msg.alert(messages.msg.titleInfo(), messages.searchNamePanel.zeroPlansFound(Ext.getCmp('searchNamePanel_name').getRawValue()) );
					} else if (records.length == 1) {
						var plan = records[0].json;

						Ext.getCmp('headerTabs').activateByTabFilter(plan.tabFilter);

						Ext.getCmp('searchNamePanel').fireEvent('selectedPlan', plan);
					} else  {
						Ext.getCmp('searchNamePanel').fireEvent('foundPlans', records, Ext.getCmp('searchNamePanel_name').getRawValue());
					}
				} else {
					Ext.Msg.alert(messages.msg.titleError(), messages.msg.noPlansFound());
				}
			}
		});
	}
	,onClear: function() {
		this.getForm().reset();
	}
});	// e/o extend

// register xtype
Ext.reg('searchNamePanel', Roo.gui.SearchNamePanel);
