/**
 * @class Roo.gui.SearchPlanIdPanel
 * @extends Ext.form.FormPanel
 *
 * The SearchPlanIdPanel is located in the left bar of the page, allowing to search plans by planIdn
 *
 * @author ssmeman
 * @version 1.0
 */

/**
 * @constructor
 * @param {Object} config Configuration options
 */
Roo.gui.SearchPlanIdPanel = Ext.extend(Ext.form.FormPanel, {
	//texts used on elements of this panel
	titleText: messages.searchPlanIdPanel.title()
	,comboLoadingText: messages.combo.loadingText()
	,buttonSearch: messages.button.searchText()
	,buttonErase: messages.button.eraseText()
	,defaultValue: 'NL.IMRO.'

	//soft config (can be changed from outside)
	,id: 'searchPlanIdPanel'

	,initComponent:function() {
		this.addEvents(
			'selectedPlan'
			,'foundPlans'
			,'searchedPlanId'
		);

		var planStore = new Ext.ux.data.CachingStore({
			proxy: new Ext.ux.data.DwrProxy({
				api: {read: SearchService.findPlannenBySearchCriteria }
				,listeners: {
					'beforeload': function(dataProxy, params) {
						if(params.query.indexOf('%') == -1) {
							params.query = params.query + '%';
						}

						params[dataProxy.loadArgsKey] = [{planId: params.query},Roo.currentTabFilter.name];
					}
				}
			})
			,reader: new Ext.data.JsonReader({}, Ext.data.Record.create([
				{name: 'id', mapping:'identificatie' }
				,{name: 'name', mapping:'identificatie' }
			]))
		});

		var config = {
			title: this.titleText
			,defaultType: 'combo'
			,autoHeight: true

			,defaults: {
				allowBlank: true
				,hideLabel: true
				,width: 185
				,loadingText: this.comboLoadingText
				,listWidth: 185
			}
			,items:[{
				xtype: 'label'
				,text: 'Zoek op planid'
			},{
				id: 'searchPlanIdPanel_id'
				,valueField: 'id'
				,displayField: 'name'
				,hideTrigger: true
				,regex: /^(\S+\s?)*$/
				,value: this.defaultValue
				,validationDelay: 100
				,queryDelay: 250
				,minChars: 10 // length of "NL.IMRO." + 2
				,store: planStore
				,listeners: {
					scope: this
					,'beforequery' : function(event) {
						if(!event.combo.validate()) {
							event.cancel = true;
						}
					}
				}
			}]
			,buttons: [{
				text: this.buttonSearch
				,handler: this.onSearch
			},{
				text: this.buttonErase
				,handler: function(t,e) {
					t.findParentByType('searchPlanIdPanel').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.SearchPlanIdPanel.superclass.initComponent.apply(this, arguments);

		this.nameCombo = this.items.itemAt(1);

		eventHub.on({
			scope: this
			,'receivedUrlParameters': function(parameters) {
				if (parameters.planidn) {
					this.expand();

					plansStore.load({
						params: {
							query: {planId: parameters.planidn, tabFilter: "NONE"}
						}
						,callback: function(records, options, success) {
							if (success) {
								if (records.length == 0) {
									Ext.Msg.alert(messages.msg.titleInfo(), messages.searchPlanIdPanel.zeroPlansFound(options.params.query.planId) );
								} else if (records.length == 1) {
									var plan = records[0].json;

									Ext.getCmp('headerTabs').activateByTabFilter(plan.tabFilter);
									Ext.getCmp('searchPlanIdPanel').fireEvent('selectedPlan', plan);
								}
							} else {
								Ext.Msg.alert(messages.msg.titleError(), messages.msg.noPlansFound());
							}
						}
					});

				}
			}
			,'searchedPlanCriteria': this.onClear
			,'searchedLocation': this.onClear
			,'searchedPlanName': this.onClear
			,'changedFilter' : function(parameters) {
				if(parameters.filter != "NONE") {
					this.getForm().reset();
					planStore.reset();

					this.nameCombo.lastQuery = '';
					this.nameCombo.reset();
					this.nameCombo.setValue(this.defaultValue);
				}
			}
		});

	} // e/o function initComponent

	,onSearch: function(b,e) {
		Ext.getCmp('searchPlanIdPanel').fireEvent('searchedPlanId');

		plansStore.load({
			params: {
				query: {planId: Ext.getCmp('searchPlanIdPanel_id').getRawValue()}
			}
			,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.searchPlanIdPanel.zeroPlansFound(Ext.getCmp('searchPlanIdPanel_id').getRawValue()) );
					} else if (records.length == 1) {
						var plan = records[0].json;

						Ext.getCmp('headerTabs').activateByTabFilter(plan.tabFilter);
						Ext.getCmp('searchPlanIdPanel').fireEvent('selectedPlan', plan);
					} else  {
						Ext.getCmp('searchPlanIdPanel').fireEvent('foundPlans', records, Ext.getCmp('searchPlanIdPanel_id').getRawValue());
					}
				} else {
					Ext.Msg.alert(messages.msg.titleError(), messages.msg.noPlansFound());
				}
			}
		});
	}

	,onClear: function() {
		this.getForm().reset();
	}

});	// e/o extend

// register xtype
Ext.reg('searchPlanIdPanel', Roo.gui.SearchPlanIdPanel);
