var <?php echo $filterName;?> = new Ext.form.FormPanel({
	id:'<?php echo $filterName;?>',
	bodyStyle:'padding: 20px',
	region: 'north',
	defaults:{
    	style: 'margin-right: 15px',
    	style: 'margin-bottom: 5px'
	},
    height: 360,
	collapsible: true,
	items:[{
    	xtype: 'container',
    	layout: 'column',
    	defaults:{
    		style: 'margin-right: 20px'
    	},
    	items:[{
			xtype: 'button',
			id: 'today',
        	name: 'today',
        	enableToggle: true,
        	text: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "today_button_text","Danes");?>',
        	handler: function(){
        		Ext.getCmp('time_from').setValue(new Date());
        		Ext.getCmp('time_to').setValue(new Date());
        	},
        	listeners: {
        		click: function(comp){
        			if(comp.pressed == true){
        				if(Ext.getCmp('yesterday').pressed == true){
        					Ext.getCmp('yesterday').toggle();	
        				}
        				if(Ext.getCmp('this_week').pressed == true){
        					Ext.getCmp('this_week').toggle();	
        				}
        				if(Ext.getCmp('last_week').pressed == true){
        					Ext.getCmp('last_week').toggle();	
        				}
        				if(Ext.getCmp('this_month').pressed == true){
        					Ext.getCmp('this_month').toggle();	
        				}
        				if(Ext.getCmp('last_month').pressed == true){
        					Ext.getCmp('last_month').toggle();	
        				} 
        			}
        			Ext.getCmp('export_statistic').disable();
        		}
        	}
		},{
			xtype: 'button',
			id: 'yesterday',
        	name: 'yesterday',
        	enableToggle: true,
        	text: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "yesterday_button_text","Včeraj");?>',
        	handler: function(){
        		<?php
        			$now = new DateTime();
					$now->modify("-1 day");
        		?>
        		Ext.getCmp('time_from').setValue('<?php echo $now->format("d.m.Y");?>');
        		Ext.getCmp('time_to').setValue('<?php echo $now->format("d.m.Y");?>');
        	},
        	listeners: {
        		click: function(comp){
        			if(comp.pressed == true){
        				if(Ext.getCmp('today').pressed == true){
        					Ext.getCmp('today').toggle();	
        				}
        				if(Ext.getCmp('this_week').pressed == true){
        					Ext.getCmp('this_week').toggle();	
        				}
        				if(Ext.getCmp('last_week').pressed == true){
        					Ext.getCmp('last_week').toggle();	
        				}
        				if(Ext.getCmp('this_month').pressed == true){
        					Ext.getCmp('this_month').toggle();	
        				}
        				if(Ext.getCmp('last_month').pressed == true){
        					Ext.getCmp('last_month').toggle();	
        				} 
        			}
        			Ext.getCmp('export_statistic').disable();
        		}
        	}
		},{
			xtype: 'button',
			id: 'this_week',
        	name: 'this_week',
        	enableToggle: true,
        	text: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "this_week_button_text","Toti teden");?>',
        	handler: function(){
        		<?php 
        			$d = new DateTime(); 
        			$weekday = $d->format('w'); 
        			$diff = ($weekday == 0 ? 6 : $weekday -1);
					$d->modify("-$diff day");   
        		?>		   		
        		
        		Ext.getCmp('time_from').setValue('<?php echo $d->format("d.m.Y");?>');
        		Ext.getCmp('time_to').setValue('<?php $d->modify('+6 day'); echo $d->format("d.m.Y");?>');
        	},
        	listeners: {
        		click: function(comp){
        			if(comp.pressed == true){
        				if(Ext.getCmp('yesterday').pressed == true){
        					Ext.getCmp('yesterday').toggle();	
        				}
        				if(Ext.getCmp('today').pressed == true){
        					Ext.getCmp('today').toggle();	
        				}
        				if(Ext.getCmp('last_week').pressed == true){
        					Ext.getCmp('last_week').toggle();	
        				}
        				if(Ext.getCmp('this_month').pressed == true){
        					Ext.getCmp('this_month').toggle();	
        				}
        				if(Ext.getCmp('last_month').pressed == true){
        					Ext.getCmp('last_month').toggle();	
        				} 
        			}
        			Ext.getCmp('export_statistic').disable();
        		}
        	}
		},{
			xtype: 'button',
			id: 'last_week',
        	name: 'last_week',
        	enableToggle: true,
        	text: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "last_week_button_text","Prejšnji teden");?>',
        	handler: function(){
        		<?php 
        			$d = new DateTime(); 
        			$weekday = $d->format('w'); 
        			$diff = 7 + ($weekday == 0 ? 6 : $weekday -1); 
        			$d->modify("-$diff day");  
        		?>        		
        		
        		Ext.getCmp('time_from').setValue('<?php echo $d->format("d.m.Y");?>');
        		Ext.getCmp('time_to').setValue('<?php $d->modify("+6 day"); echo $d->format("d.m.Y");?>');
        	},
        	listeners: {
        		click: function(comp){
        			if(comp.pressed == true){
        				if(Ext.getCmp('yesterday').pressed == true){
        					Ext.getCmp('yesterday').toggle();	
        				}
        				if(Ext.getCmp('this_week').pressed == true){
        					Ext.getCmp('this_week').toggle();	
        				}
        				if(Ext.getCmp('today').pressed == true){
        					Ext.getCmp('today').toggle();	
        				}
        				if(Ext.getCmp('this_month').pressed == true){
        					Ext.getCmp('this_month').toggle();	
        				}
        				if(Ext.getCmp('last_month').pressed == true){
        					Ext.getCmp('last_month').toggle();	
        				} 
        			}
        			Ext.getCmp('export_statistic').disable();
        		}
        	}
		},{
			xtype: 'button',
			id: 'this_month',
        	name: 'this_month',
        	enableToggle: true,
        	text: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "this_month_button_text","Toti mesec");?>',
        	handler: function(){
        		var date = new Date();
				var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
				var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);    		
        		
        		Ext.getCmp('time_from').setValue(firstDay);
        		Ext.getCmp('time_to').setValue(lastDay);
        	},
        	listeners: {
        		click: function(comp){
        			if(comp.pressed == true){
        				if(Ext.getCmp('yesterday').pressed == true){
        					Ext.getCmp('yesterday').toggle();	
        				}
        				if(Ext.getCmp('this_week').pressed == true){
        					Ext.getCmp('this_week').toggle();	
        				}
        				if(Ext.getCmp('last_week').pressed == true){
        					Ext.getCmp('last_week').toggle();	
        				}
        				if(Ext.getCmp('today').pressed == true){
        					Ext.getCmp('today').toggle();	
        				}
        				if(Ext.getCmp('last_month').pressed == true){
        					Ext.getCmp('last_month').toggle();	
        				} 
        			}
        			Ext.getCmp('export_statistic').disable();
        		}
        	}
		},{
			xtype: 'button',
			id: 'last_month',
        	name: 'last_month',
        	enableToggle: true,
        	text: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "last_month_button_text","Prejšnji mesec");?>',
        	handler: function(){
        		var date = new Date();
				var firstDay = new Date(date.getFullYear(), date.getMonth()-1, 1);
				var lastDay = new Date(date.getFullYear(), date.getMonth(), 0);    		
        		
        		Ext.getCmp('time_from').setValue(firstDay);
        		Ext.getCmp('time_to').setValue(lastDay);
        	},
        	listeners: {
        		click: function(comp){
        			if(comp.pressed == true){
        				if(Ext.getCmp('yesterday').pressed == true){
        					Ext.getCmp('yesterday').toggle();	
        				}
        				if(Ext.getCmp('this_week').pressed == true){
        					Ext.getCmp('this_week').toggle();	
        				}
        				if(Ext.getCmp('last_week').pressed == true){
        					Ext.getCmp('last_week').toggle();	
        				}
        				if(Ext.getCmp('this_month').pressed == true){
        					Ext.getCmp('this_month').toggle();	
        				}
        				if(Ext.getCmp('today').pressed == true){
        					Ext.getCmp('today').toggle();	
        				} 
        			}
        			Ext.getCmp('export_statistic').disable();
        		}
        	}
		}]
	},{
		xtype: 'checkbox',
		id: 'all_checkboxes',
    	name: 'all_checkboxes',
    	checked: true,
    	fieldLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "all_checkboxes_checkbox_text","Vse izbrano");?>',
    	listeners:{
    		check: function(comp, a){
    			Ext.each(Ext.getCmp('status_checkgroup').items.items,function(c){
					Ext.getCmp(c.id).setValue(a);
					var grid = Ext.getCmp('<?php echo $gridName;?>');
					grid.getColumnModel().setHidden(grid.getColumnModel().getIndexById("c_" + c.id), !a);
				})
    		}
    	}
	},{
		xtype: 'container',
		layout: 'column',
		labelWidth: 120,
    	items: [{
    		xtpye: 'container',
    		layout:'form',
    		border: false,
    		items:[{
                <?php
                    $d = new DateTime(); 
                    $weekday = $d->format('w'); 
                    $diff = ($weekday == 0 ? 6 : $weekday -1);
                    $d->modify("-$diff day");   
                ?>
    			xtype: 'datefield',
    			id: 'time_from',
    			format: 'd.m.Y',
    			value: '<?php echo $d->format('d.m.Y');?>',
	        	name: 'time_from',
	        	fieldLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "time_from_textbox","Čas od (od 0:00)");?>',
	        	listeners: {
	        		change: function(){
	        			Ext.getCmp('export_statistic').disable();
	        		}
	        	}
    		}]
    	},{
    		xtype:'container',
    		layout: 'form',
    		style: 'margin-left: 10px',
    		items:[{		
    			xtype: 'datefield',
    			id: 'time_to',
    			format: 'd.m.Y',
    			value: '<?php $d->modify('+6 day'); echo $d->format("d.m.Y");?>',
	        	name: 'time_to',
	        	fieldLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "time_to_textbox","Čas do (od 24:00)");?>',
	        	listeners: {
	        		change: function(){
	        			Ext.getCmp('export_statistic').disable();
	        		}
	        	}
    		}]
    	},{
    	    xtype: 'container',
            items: [{
                xtype: 'radiogroup',
                cls: 'x-check-group-alt',
                items: [{
                    name: 'DeliveryOrOrderDate',
                    id:'DeliveryOrOrderDate1',
                    boxLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "DeliveryOrOrderDate1_radio","Datum prezentacije");?>',
                    checked: true,
                    style: { marginLeft: '15px' },
                    width: 150 
                },{
                    name: 'DeliveryOrOrderDate',
                    id:'DeliveryOrOrderDate2',
                    boxLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "DeliveryOrOrderDate2_radio","Datum dostave");?>',
                    style: { marginLeft: '10px' },
                    width: 200                  
                }]
            }]
    	}]	
	},{
		xtype: 'radiogroup',
		id: 'based_on_group',
		fieldLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "based_on_radio_label","Temelji na");?>',
		width: 600,
		items:[{
			name: 'basedOn',
			inputValue: '1',
			checked: true,
			boxLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "based_on_location_radio_label","Temelji na lokacijo");?>',
			listeners: {
        		check: function(){
        			Ext.getCmp('export_statistic').disable();
        		}
        	}
		},{
			name: 'basedOn',
			inputValue: '2',
			boxLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "based_on_location_team_radio_label","Temelji na lokacijo - ekipo");?>',
			listeners: {
        		check: function(){
        			Ext.getCmp('export_statistic').disable();
        		}
        	}
		},{
			name: 'basedOn',
			inputValue: '3',
			boxLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "based_on_team_radio_label","Temelji na ekipo");?>',
			listeners: {
        		check: function(){
        			Ext.getCmp('export_statistic').disable();
        		}
        	}
		}]
	},
	{
		xtype: 'container',
		layout: 'column',
		labelWidth: 120,
    	items: [{
    		xtpye: 'container',
    		layout:'form',
    		border: false,
    		items:[new Ext.form.ComboBox({
				fieldLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "location_combo_select_label","Lokacija");?>',
				width: 250,
				mode: 'remote',
				triggerAction: 'all',
				id: 'locationFilter',
				name: 'locationFilter',
				hiddenName: 'locationFilter',
				lazyRender: true,
				store: new Ext.data.Store({
					url: '<?php echo $ajax;?>',
					baseParams: {
						action: 'getLocation'
					},
					reader: new Ext.data.JsonReader({		
							root:'rows',
							totalProperty : "results"
						},
						new Ext.data.Record.create([
							{ name:"id", type: 'id'},
						 	{ name:"value", type: 'string' }
						])
					)
				}),
			    valueField: 'id',
			    displayField: 'value' 
			})]
    	},{
    		xtype:'container',
    		layout: 'form',
    		style: 'margin-left: 10px',
    		items:[new Ext.form.ComboBox({
				fieldLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "team_combo_select_label","Ekipa");?>',
				mode: 'remote',
				triggerAction: 'all',
				id: 'teamFilter',
				name: 'teamFilter',
				hiddenName: 'teamFilter',
				lazyRender: true,
				store: new Ext.data.Store({
					url: '<?php echo $ajax;?>',
					baseParams: {
						action: 'getTeam'
					},
					reader: new Ext.data.JsonReader({		
							root:'rows',
							totalProperty : "results"
						},
						new Ext.data.Record.create([
							{ name:"id", type: 'id'},
						 	{ name:"value", type: 'string' }
						])
					)
				}),
			    valueField: 'id',
			    displayField: 'value' 
			})]
    	}]	
	},{
		xtype: 'checkboxgroup',
		fieldLabel: '<?php echo $LangPack->GetLabel("statistic_filter_grid", "show_cells_label","Prikaži celice");?>',
		id: 'status_checkgroup',
		listeners:{
			change: function(comp, a){
				var grid = Ext.getCmp('<?php echo $gridName;?>');
				Ext.each(comp.items.items,function(c){
					grid.getColumnModel().setHidden(grid.getColumnModel().getIndexById("c_" + c.id), !c.checked);
				});
			}
		},
		columns: 7,
    	items: [{
    		name:'team',
    		id: 'team',
        	checked: true,
    		boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_team_label","Ekipa");?>'
    	},{
        	name: 'team_name',
        	id: 'team_name',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_team_name_label","Ime ekipe");?>'
    	},{
        	name: 'city',
        	id: 'city',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_city_label","Kraj");?>'
    	},{
        	name: 'location',
        	id: 'location',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_location_label","Lokacija");?>'
    	},{
        	name: 'presentation',
        	id: 'presentation',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_presentation_label","Predstavitev");?>'
    	},{
        	name: 'invited',
        	id: 'invited',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_invited_label","Povabljeni");?>'
    	},{
        	name: 'not_present',
        	id: 'not_present',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "not_present_label","Ne prisotni");?>'
    	},{
        	name: 'attendance',
        	id: 'attendance',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_attendance_register_label","Udeleženi");?>',
    	},{
        	name: 'companions',
        	id: 'companions',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_companions_label","Spremjevalci");?>'
    	},{
    		name: 'total',
        	id: 'total',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_total_label","Vsi skupaj");?>'
    	},{
        	name: 'male',
        	id: 'male',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_male_label","Moški");?>'
    	},{
        	name: 'female',
        	id: 'female',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_female_label","Žensk");?>'
    	},{
        	name: 'undecidedly',
        	id: 'undecidedly',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_undecidedly_label","Neodločni");?>'
    	},{
        	name: 'alone',
        	id: 'alone',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_alone_label","Sami");?>'
    	},{
        	name: 'spouse',
        	id: 'spouse',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_spouse_label","Zakonec");?>'
    	},{
        	name: 'other',
        	id: 'other',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_other_label","Ostalo");?>'
    	},{
        	name: 'order',
        	id: 'order',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_order_label","Naročila");?>'
    	},{
        	name: 'delivery',
        	id: 'delivery',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_delivery_label","Dostava");?>'
    	},{
        	name: 'storno',
        	id: 'storno',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_storno_label","Storno");?>'
    	},{
        	name: 'delayed',
        	id: 'delayed',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_delayed_label","Zamuda");?>'
    	},{
        	name: 'corporate_card',
        	id: 'corporate_card',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_corporate_card_label","Podjetna kartica");?>'
    	},{
        	name: 'loan_credit',
        	id: 'loan_credit',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_loan_credit_label","Lon kredit");?>'
    	},{
        	name: 'appeared_average',
        	id: 'appeared_average',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_appeared average_label","Povprečna pojava");?>'
    	},{
        	name: 'customer_avarage',
        	id: 'customer_avarage',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_customer_avarage_label","Povprečnih kupcov");?>'
    	},{
        	name: 'traffic_avarage',
        	id: 'traffic_avarage',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_traffic_avarage_label","Povprečni promet");?>'
    	},{
        	name: 'realization',
        	id: 'realization',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_realization_label","Realizacija");?> %'
    	},{
        	name: 'storno_percent',
        	id: 'storno_percent',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_storno_label","Storno");?> %'
    	},{
        	name: 'order_amount',
        	id: 'order_amount',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_order_amount_label","Znesek naročila");?>'
    	},{
        	name: 'recognized_income',
        	id: 'recognized_income',
        	checked: true,
        	boxLabel:'<?php echo $LangPack->GetLabel("statistic_filter_grid", "radio_recognized_income_label","Priznani prihodek");?>'
    	}]
	},{
		xtype: 'button',
    	name: 'search',
    	width: 60,
    	text: '<?php echo $LangPack->GetLabel("CRM_all", "CRM_search_button","Iskanje");?>',
    	handler: function(){
    		var grid = Ext.getCmp('<?php echo $gridName;?>');
    		if(Ext.getCmp('time_from').isValid() && Ext.getCmp('time_to').isValid()){
    			grid.getEl().mask('<?php echo $LangPack->GetLabel("CRM_all", "loading_mask_text","Posodabljanje...");?>');
	    		grid.getStore().reload({ params: { 
	    		                                 fromDate: Ext.getCmp('time_from').getRawValue(), 
	    		                                 toDate: Ext.getCmp('time_to').getRawValue(), 
	    		                                 basedOn: Ext.getCmp('based_on_group').getValue().getGroupValue(), 
	    		                                 teamF: Ext.getCmp('teamFilter').getValue(), 
	    		                                 locationF: Ext.getCmp('locationFilter').getRawValue(),
	    		                                 datum_dostave: Ext.getCmp('DeliveryOrOrderDate2').getValue(), 
	    		                                 }, 
		                                 callback: function(resp, opt, success){
	    			if(success){
						grid.getEl().unmask();
					}
	    		}});
	    		<?php if(!$permissions->hasCRMPermission(28)) {?>
	    		Ext.getCmp('<?php echo $filterName;?>').collapse();
	    		Ext.getCmp('west-panel').collapse();
	    		<?php }?>
	    	}
    	},
    	listeners: {
    		click: function(){
    			Ext.getCmp('export_statistic').enable();
    		}
    	}
	}]
});