<?php 
include_once("_dirinfo.php");

include_once(PATH_TO_ROOT."/mystery/init.php");
	
include_once(PATH_TO_ROOT."_class/class.Extjs_tree.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_grid.php");

// here we define projects tree	
	$treeWCprojects = new MakeTree('GroupTree');
	$treeName = $treeWCprojects->getName();
	$treeWCprojects->SetTable('_Groups',DB_WARPIT_MYSTERY);
	$treeWCprojects->SetRootVisible('false','Mystery')
	//  ->setTitle('Help'); // this is not used because we combine in one tab two trees and title is set in combined panel
	  ->setSort('idLab ASC')
	  	  ->setReqUrl('_ajax/ajax.statistics.php')
	 /* ->setContDelete('Delete')
	  ->setContAdd('Add child')
	  ->setContAddMain('Add sibling')*/
	  //we create data for the language combobox
	  ;

  		
	  $treeWCprojects  
	 ->setOneLevel()
	 ->setContAddGroup()
	 ->addContItem("Add project","function(item){
	 	var t = Ext.getCmp('MysteryProjects').getChecked('id');
	 	var n;
	    if(item)
	    {
			n = item.parentMenu.contextNode;
			if(n.isLeaf())
				n = n.parentNode;
		}
		else
			{
				n = $treeWCprojects.getSelectionModel().getSelectedNode();
				if(!n)
					n = $treeWCprojects.getRootNode();
			}
	
			
				
		if(t.length <1 ) return;
		$treeWCprojects.RMconn.request({
			params: {
				parentId : n.id,
				projects:t.join(),
				action:'addProjects'
			},
			success: function(){
				n.reload();
			}
		});
	 	}
	 ")
	 ->setContDelete()
	 ->activateDD('wupGrouplink');
	 
	 //grid with old webcati projects (available only if specific config is set)
	 
	  $treeWCprojects->addtBarItem("'->'")
	  ->setSearchField('','top',100)
	 // ->addtBarItem("{text:'Expand All',handler:function(){WCprojects.expandAll();}} ")
	  ->addConfig('region',"'east'")
	  ->setHeight(500)
	  ->baseParams = "checked:true";
	 ;
	
	$treeQuestion = new MakeTree('StatQuestionTree');
	$treeQuestion->SetTable('nimaVeze',DB_WARPIT_MYSTERY)
	->SetRootVisible('true','Questions')
	 ->addConfig('region',"'west'")
	 ->setWidth(300)
	   	  ->setReqUrl('_ajax/ajax.statistics.php')
	 ->addConfig('enableDrag',"true")
	  ->addConfig('ddGroup',"'statisticForm'")
	 
	 
	 ;
	 
	 
	 
	echo $treeWCprojects->CreateTree();
	echo $treeQuestion->CreateTree();
?>
var statisticsForm = new Ext.form.FormPanel({
		region:'center',
		padding:5, 
			tbar:[{
				xtype:'button',
				text:'Create table',
				//fieldLabel:'&nbsp;',
				handler:function(){
					 var cols = statisticsForm.getForm().findField('statisticColumns');
					 var c = new Array();
					 for(var i=0;i< cols.store.data.items.length;i++)
					 	c.push( cols.store.data.items[i].data.name);
					 	
					 var vals = statisticsForm.getForm().getValues();
					 vals.columns = c.join(',');
					 vals.action = 'createStatistic';
					<?php echo "vals.projTable = $treeQuestion.getLoader().baseParams.RMtable;
				//	".$treeQuestion;?>.RMconn.request({
				//		params:vals
				//	});
					mainTabPanel.add({
						title: 'Report',
						html: '<iframe src=_forms/stat.html.php?'+ Ext.urlEncode(vals) +' height=100% width=100% />',
						closable:true
					}).show();
				}
		},{
			text:'Reset',
			handler:function(){
				statisticsForm.getForm().reset();
				statisticsForm.getForm().findField('statisticColumns').store.removeAll();
			}
		}],
		items:[{
			xtype:'fieldset',
			title:'Options',
			items:[{
				xtype:'checkboxgroup',
				fieldLabel:'Show',
				columns:1,
				items:[{
					boxLabel:'Variable name',
					name:'showVarName'
				},
				{
					boxLabel:'Variable value',
					name:'showVarValue'
				}]
			},{xtype:'container',height:15},{
				xtype:'radiogroup',
				columns:1,
				fieldLabel:'Split view by',
				items:[{
					boxLabel:'Row variable',
					name:'splitBy',
					inputValue:1,
					checked:true
				},{
					boxLabel:'Group',
					name:'splitBy',
					inputValue:2
				}]
			}]
		},{
			fieldLabel:'Filter',
			xtype:'textfield',
			width: 450,
			name:'filter'			
		},{
			fieldLabel:'Columns',
			xtype:'multiselect', 
			width: 450,
			displayField: 'name',
            valueField: 'id',
            id:'statisticColumns',
            ddReorder: true,
			store: new Ext.data.ArrayStore({ 
					fields:['id','name'],
					data:[]
                    
                    }),
			listeners:{
				afterrender:function(fld){
					fld.RMdd = new Ext.dd.DropTarget(fld.getId(),
						{
							ddGroup:'statisticForm',
							notifyDrop:function(dds,e,data)
							{
								fld.store.loadData([[data.node.attributes.qName,data.node.attributes.qName]],true);
								//fld.store.reload();
								
								return true;
							},
							fld:this
						});	
					
				},
				beforedestroy:function(cmp)
					{
						if(cmp.RMdd) cmp.RMdd.destroy();
					}
			}
		},{
			xtype:'button',
			text:'Remove',
			fieldLabel:'&nbsp',
			labelWidth:400,
			labelSeparator:'',
			handler:function()
			{
				var c = statisticsForm.getForm().findField('statisticColumns');
				var del = c.view.getSelectedIndexes();
				for(var i=del.length;i>=0;i--)
				{
					c.store.removeAt(del[i]);
				}
			}
		},{
			fieldLabel:'Row',
			xtype:'textfield',
			width: 450,
			readOnly:true,
			name:'row',
			listeners:{
				afterrender:function(fld){
					fld.RMdd = new Ext.dd.DropTarget(fld.getId(),
						{
							ddGroup:'statisticForm',
							notifyDrop:function(dds,e,data)
							{
								fld.setValue(data.node.attributes.qName);
								return true;
							},
							fld:this
						});	
					
				},
				beforedestroy:function(cmp)
					{
						if(cmp.RMdd) cmp.RMdd.destroy();
					}
			}
		}]

});


var statisticsPanel = new Ext.Panel({
		id:'statisticsPanel',
		closable:true,
		title:'<?php echo $languagePack['statistics']; ?>',
		defaults:{split:true,border:false},
		iconCls:'icon-webcati-tab',
		layout:'border',
		border: false,
		items:[{
			region:'east',
			width:300, 
			items:[
		<?php echo $treeWCprojects->getName()   ?>,
		{
			padding:'20',
			border:false,
			layout:'fit',
			items:	{
				xtype:'button',
				text:'<?php echo $languagePack['statistic_analyze'];?>',
				width:260,
				handler:function(){
				<?php 
					echo "var p = $treeWCprojects.getChecked('id');
						
						$treeWCprojects.RMconn.request({params:{
							action:'analyze',
							groups:p.join()
						},
						success:function(r){
							var rp = Ext.decode(r.responseText);
							$treeQuestion.getLoader().baseParams.RMtable = 	rp.table;
							$treeQuestion.getRootNode().reload();
						}});
					";
				?>
				}
			}
		}	
		 ]},
		statisticsForm
		,<?php echo $treeQuestion   ?> ]
		
				
	});
	mainTabPanel.add(statisticsPanel).show();
