<?php

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_tree.php");
include_once(PATH_TO_ROOT."_support/class.UserSession.php");

$db->ChangeDatabase(DB_WARPIT_MAIN);
$UserSession = new UserSession($db);


// here we define first tree with webcati options
	$treeWC = new MakeTree($_POST['treeName']);
	$treeWC->SetTable($_POST[tree],DB_WARPIT_WEBCATI_BASE);
	$treeWC->SetRootVisible("false")
		->addConfig('region',"'north'")
		// ->setTitle("Webcati"); // this is not used because we combine in one tab two trees and title is set in combined panel
		->setLinesVisible(false)
		->setBoldRoots()
		->setFontMedium()
		->setTextPadding('1px')
		->setHeight(300)
	;
	
// here we define projects tree	
	$treeWCprojects = new MakeTree('WCprojects_webrecode');
	$treeName = $treeWCprojects->getName();
	$treeWCprojects->SetTable('_Projects',DB_WARPIT_WEBCATI_BASE);
	$treeWCprojects->SetRootVisible('true','Projects')
	//  ->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('webcati/_forms/_ajax/ajax.groups.php')
	 /* ->setContDelete('Delete')
	  ->setContAdd('Add child')
	  ->setContAddMain('Add sibling')*/
	  ->addContItem('Add project'," function(item) {
	  Ext.Msg.prompt('Add Project','Project name:',function(btn,text){ if(btn == 'ok' ){
		var n = item.parentMenu.contextNode;
		var par,pos,ord;
		if(n.attributes.isProject==true)
		{par = n.parentNode;  } else {par = n;}
         $treeName.RMconn.request({params:{action:'insertProject',parId:par.id,nodeText:text,nodeId:n.id} 
		,success:function(resp){
		if(Ext.decode(resp.responseText).success){
			//var newNode = new Ext.tree.TreeNode({id: Ext.decode(resp.responseText).insertId,q_pos:pos,position:ord, text: 'Novi', leaf: true});
			//par.appendChild(newNode);
			//par.reload();
			var p = par.getPath()+'/'+Ext.decode(resp.responseText).insertId;
			$treeName.getLoader().load($treeName.getRootNode(),function(){ $treeName.selectPath(p); } );
			
		} else{ }},
			failure:function(){alert('failed');}	}); }}); }",'false','icon-add-record' )
	->addContItem('Add Group',"function(item){
		Ext.Msg.prompt('Add Group','Group name:',function(btn,text){ if(btn == 'ok' ){
		var n = item.parentMenu.contextNode; 
		var par;
		//the group gets added after the clicked group (if a q is clicked its parent is used)
		
		if(n.attributes.isProject == 1)
			{par = n.parentNode; } 
		else{ par = n;   }
		$treeName.RMconn.request({params:{
		action:'insertGroup',nodeText:text,
		nodeId:par.id
		,isTargetQ:par.attributes.isQuestion },success:function(resp){
		if(Ext.decode(resp.responseText).success){ $treeName.getLoader().load($treeName.getRootNode(),function(){ $treeName.getRootNode().expand();});  }
		 }  }); }  } );   }",'false','icon-add-folder')
	 ->addContItem('Delete project',"function(item){
	 	var n = item.parentMenu.contextNode;
	 	Ext.Msg.show({ title:'Remove project',buttons: Ext.MessageBox.YESNO,msg:'Are you sure?',
		fn: function(btn){if(btn == 'yes'){
		 $treeName.RMconn.request({ params: {action:'deleteProject',id:n.id,name:n.attributes.text},
		 success:function(resp,opt){ 
				if(Ext.decode(resp.responseText).success)
				{
					n.remove();
				}
		    } });     
		 }} });
	 }","node.attributes.isProject!=true",'icon-delete-record')
	  ->addContItem('Delete group',"function(item){
	 	var n = item.parentMenu.contextNode;
	 	Ext.Msg.show({ title:'Remove group',buttons: Ext.MessageBox.YESNO,msg:'Are you sure?',
		fn: function(btn){if(btn == 'yes'){
		 $treeName.RMconn.request({ params: {action:'delete',nodeId:n.id,name:n.attributes.text},
		 success:function(resp,opt){ 
				if(Ext.decode(resp.responseText).success)
				{
					n.remove();
				}
		    } });     
		 }} });
	 }","node.attributes.isProject==true || node.hasChildNodes()",'icon-delete-folder')
	  ->activateDD('wcqbGroup')
	  ->setEditable() 
	  ->setSearchField('','top',100)
	 // ->addtBarItem("{text:'Expand All',handler:function(){WCprojects.expandAll();}} ")
	  ->addConfig('region',"'center'")
	 ;
	 
	echo $treeWC->CreateTree();
	// webcati_tree as first parameter in addTab function (extended addTab function from ExtJS), is meant for title of the tab
	// in this case where click on each node transfer different text, this text is used for title
	// and first argument is not used at all 
	$treeProjName = $treeWCprojects->getName();
	echo $_POST[treeName].".on('click',function(node) {
	var url;
	
	
	switch(node.attributes.id)
	{	//build questionnaire
		case '1' : 
				var nodeProj = $treeProjName.getSelectionModel().getSelectedNode();
				var checkedProj = $treeProjName.getChecked();
				//alert('project_id:'+nodeProj.attributes.id_project);
				if(nodeProj && nodeProj.isLeaf() )
				{
					// nodeProj.attributes.id_project
					addTabAuto(nodeProj.text+'_panel','webcati/_forms/forms.webrecode.php',{id_project:nodeProj.attributes.id_project, proj_name:nodeProj.text});  
				}
				else {alert('select project!'); }
				
				
				break;
		case '8' : 
				var nodeProj = $treeProjName.getSelectionModel().getSelectedNode();
				
				//alert('project_id:'+nodeProj.attributes.id_project);
				if(nodeProj && nodeProj.isLeaf() )
				{
					// nodeProj.attributes.id_project
					new RMajax().request({
						url: 'webrecode/_ajax/ajax.autoRecode.php',
						params:{
							id_project: nodeProj.id
						}
					
					});  
				}
				else {alert('select project!'); }
				
				
				break;
		case '10' : 
				var nodeProj = $treeProjName.getSelectionModel().getSelectedNode();
				
				//alert('project_id:'+nodeProj.attributes.id_project);
				if(nodeProj && nodeProj.isLeaf() )
				{
					// nodeProj.attributes.id_project
					Ext.Msg.confirm('Delete recode for '+nodeProj.text,'Are you sure?',function(btn){
					if(btn == 'yes')
						new RMajax().request({
							url: 'webrecode/_ajax/ajax.autoRecode.php',
							params:{
								id_project: nodeProj.id,
								resetRecode:true
							}
						
						});
						  });
						      
				}
				else {alert('select project!'); }
				
				
				break;
		case '2' : 
				var nodeProj = $treeProjName.getSelectionModel().getSelectedNode();
				var checkedProj = $treeProjName.getChecked();
				if(nodeProj && nodeProj.isLeaf() )
				{
					mainTabPanel.add( 
						new Ext.Panel({
							title: 'Export - categories', 
							closable: true, 
							id: 'Recode_tab_docs_categories',
						    items: new Ext.ux.IFrameComponent({ 							
										width: '100%',
										height: '100%',
										id: 'Export_docs_categories', 
										url: 'webcati/_forms/forms.webrecode.export.php?id_project='+nodeProj.attributes.id_project+'&proj_name='+nodeProj.text,
									})
						})
					).show();

				}
				else {alert('select project!'); }
						
				
				break;
		case '4' : 
				var nodeProj = $treeProjName.getSelectionModel().getSelectedNode();
				var checkedProj = $treeProjName.getChecked();
				if(nodeProj && nodeProj.isLeaf() )
				{
					mainTabPanel.add( 
						new Ext.Panel({
							title: 'Export - Labels by category', 
							closable: true, 
							id: 'Recode_tab_docs_catlab',
						    items: new Ext.ux.IFrameComponent({ 							
										width: '100%',
										height: '100%',
										id: 'Export_docs_catlab', 
										url: 'webcati/_forms/forms.webrecode.export.php?documents_mode=1&id_project='+nodeProj.attributes.id_project+'&proj_name='+nodeProj.text,
									})
						})
					).show();

				}
				else {alert('select project!'); }
								
				
				break;
		case '12' : 
				var nodeProj = $treeProjName.getSelectionModel().getSelectedNode();
				var checkedProj = $treeProjName.getChecked();
				if(nodeProj && nodeProj.isLeaf() )
				{
					mainTabPanel.add( 
						new Ext.Panel({
							title: 'Documents - categories', 
							closable: true, 
							id: 'Recode_tab_docs_categories',
						    items: new Ext.ux.IFrameComponent({ 							
										width: '100%',
										height: '100%',
										id: 'Recode_docs_categories', 
										url: 'webcati/_forms/forms.webrecode.documents.php?id_project='+nodeProj.attributes.id_project+'&proj_name='+nodeProj.text,
									})
						})
					).show();

				}
				else {alert('select project!'); }
						
				
				break;
		case '13' : 
				var nodeProj = $treeProjName.getSelectionModel().getSelectedNode();
				var checkedProj = $treeProjName.getChecked();
				if(nodeProj && nodeProj.isLeaf() )
				{
					mainTabPanel.add( 
						new Ext.Panel({
							title: 'Documents - Labels by category', 
							closable: true, 
							id: 'Recode_tab_docs_catlab',
						    items: new Ext.ux.IFrameComponent({ 							
										width: '100%',
										height: '100%',
										id: 'Recode_docs_catlab', 
										url: 'webcati/_forms/forms.webrecode.documents.php?documents_mode=1&id_project='+nodeProj.attributes.id_project+'&proj_name='+nodeProj.text,
									})
						})
					).show();

				}
				else {alert('select project!'); }
								
				
				break;
		case '3' : 
				var nodeProj = $treeProjName.getSelectionModel().getSelectedNode();
				var checkedProj = $treeProjName.getChecked();
				if(nodeProj && nodeProj.isLeaf() )
				{
					addTabAuto('stats_'+nodeProj.text+'_0','webcati/_forms/forms.webrecode.statistics.php',{statistics_mode:0, id_project:nodeProj.attributes.id_project, proj_name:nodeProj.text});
				}
				else {alert('select project!'); }
				
				
				
				break;
		case '5' : 
				var nodeProj = $treeProjName.getSelectionModel().getSelectedNode();
				var checkedProj = $treeProjName.getChecked();
				if(nodeProj && nodeProj.isLeaf() )
				{
					addTabAuto('stats_'+nodeProj.text+'_1','webcati/_forms/forms.webrecode.statistics.php',{statistics_mode:1, id_project:nodeProj.attributes.id_project, proj_name:nodeProj.text});
				}
				else {alert('select project!'); }
				
				break;
		default:
			if(!node.isLeaf()) node.toggle();
			break;
	}
				

	
	
	
	 
});";

	echo $treeWCprojects->CreateTree();
	echo $treeProjName.".getRootNode().expand();";
		
// we add JS code for define a new panel which is two trees combined in one tab	
?>
	var webrecodeUniquePanel = new Ext.Panel({
		id:'webrecodeUniquePanel',
		title:'Webrecode',
		defaults:{split:true,border:false},
		iconCls:'icon-webrecode-tab',
		layout:'border',
		border: false,
		closable:true,
		items:[<?php echo $treeWC->getName().','.$treeWCprojects->getName();   ?>  ]
		
				
	});
	Ext.getCmp('west-panel').add(webrecodeUniquePanel).show();
