Ext.ns('Rodium.Shop');

Rodium.Shop.ProvinceCombo = Ext.extend(Ext.form.ComboBox,{
	fieldLabel: 'województwo'
	,displayField : 'province'
	,mode : 'local'
	,valueField : 'province'
	,width : 250
	,store : new Ext.data.ArrayStore({
		fields: [
      'province'
   	]
   	,data : [['dolnośląskie'],['kujawsko-pomorskie'],['lubelskie'],['lubuskie'],['łódzkie'],
							['małopolskie'],['mazowieckie'],['opolskie'],['podkarpackie'],['podlaskie'],
							['pomorskie'],['śląskie'],['świętokrzyskie'],['warmińsko-mazurskie'],
							['wielkopolskie'],['zachodnio-pomorskie']]
	})
	,triggerAction : 'all'
	,editable : false
	,emptyText : 'Wybierz...'
});

Rodium.Shop.CityCombo = Ext.extend(Ext.ux.form.ForceQueryCombo, {
	fieldLabel: 'miasto'
	,forceQuery : true
	,mode : 'remote'
	,valueField : 'ca_ccity'
	,displayField : 'ca_ccity'
	,width : 250
	,store : new Ext.data.JsonStore({
		url : '/rodiumjson/getcities'
		,root : 'data'
		,fields: [
			{name : 'ca_ccity',type : 'string'}
   	]
	})
	,triggerAction : 'all'
	,editable : false
	,disabled : true
	,emptyText : 'Wybierz...'
});

Rodium.Shop.AddressCombo = Ext.extend(Ext.ux.form.ForceQueryCombo,{
	fieldLabel: 'sklep'
	,forceQuery : true
	,pageSize : 25
	,width : 250
	,displayField : 'c_name'
	,mode : 'remote'
	,valueField : 'c_id'
	,store : new Ext.data.JsonStore({
		url : '/rodiumjson/getaddresses'
		,root : 'data.items'
		,fields: [
      	{name : 'ca_zip_code'},{name : 'c_id', type : 'int'}
      	,{name : 'c_name'},{name : 'ca_city'}
      	,{name : 'ca_id',type:'int'},{name : 'ca_psj',type : 'boolean'}
      	,{name : 'ca_address'}, {name : 'ca_website'},{name : 'ca_email'},{name: 'ca_phone'}
   	]
   	,totalProperty : 'data.total'
   	,successProperty : 'success'
	})
	,tpl : new Ext.XTemplate(
		'<tpl for="."><div class="spec-combo-item">',
		'<strong>{c_name}</strong><br/>',
		'<em>{ca_city}, {ca_address}</em>',
		'</div></tpl>'
   )
  ,itemSelector: 'div.spec-combo-item'
	,triggerAction : 'all'
	,editable : false
	,disabled : true
	,emptyText : 'Wybierz...'
});

Rodium.Shop.FindPanel = Ext.extend(Ext.Panel, {
	layout : 'form'
	,labelAlign : 'top'
	,border : false
	,initComponent : function() {
		this.provinceCombo = new Rodium.Shop.ProvinceCombo();
		this.cityCombo = new Rodium.Shop.CityCombo();
		this.addressCombo = new Rodium.Shop.AddressCombo();
		this.phonePanel = new Ext.Panel({
			columnWidth : 0.5
			,border : false
		});
		
		this.psjPanel = new Ext.Panel({
			collapsed : true
			,html : '<img src="/img/psj.png" >'
			,width : 300
			,align : 'center'
			,border : false
		});
		
		Ext.apply(this,{
			items : [
				this.provinceCombo
				,this.cityCombo
				,this.addressCombo
				,this.psjPanel
			]
		});
		
		Rodium.Shop.FindPanel.superclass.initComponent.apply(this,arguments);		
	}
	,onRender : function(cmp) {
		Rodium.Shop.FindPanel.superclass.onRender.apply(this,arguments);
		
		this.provinceCombo.on('select',function() {
			this.cityCombo.enable();
			this.cityCombo.reset();
			this.addressCombo.reset();
			this.addressCombo.disable();
			this.psjPanel.collapse(false);
			this.phonePanel.removeAll();
			
			this.cityCombo.getStore().setBaseParam('province',this.provinceCombo.getValue());
		},this);
		
		this.cityCombo.on('select',function() {
			this.addressCombo.enable();
			this.addressCombo.getStore().setBaseParam('city',this.cityCombo.getValue());
			this.addressCombo.getStore().setBaseParam('province',this.provinceCombo.getValue());
			this.addressCombo.reset();
			this.psjPanel.collapse(false);
			this.phonePanel.removeAll();
		},this);
		
		this.addressCombo.on('select',function(combo,record,index) {
			record.get('ca_psj') == true ? this.psjPanel.expand(false) : this.psjPanel.collapse(false);
		},this);
	}
})
