<?php

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


$db->ChangeDatabase(DB_WARPIT_MAIN);

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

			

	  $treeWCprojects->addtBarItem("{iconCls:'icon-add-folder',tooltip:'Add',handler: function(){ 
			
	 		 Ext.Msg.prompt('Add ','Name: ',function(btn,text){
				if(btn=='ok')	{
					//alert('test');
					 $treeName.RMconn.request({ 
						params: { action:'AddVisitFolder', folderName: text },
					 	success:function(resp,opt){ 
							if(Ext.decode(resp.responseText).success)
							{		
								n = $treeWCprojects.getRootNode();						
								$treeName.getLoader().load(n,function(){ n.expand();});
							}
					    } 
					}); 
				
				} else return false;
									 
			});
	  		
	  
	  }}");
	  
	  
	  $treeWCprojects->addtBarItem("{iconCls:'icon-delete-folder',tooltip:'Delete',handler: function(){
	  		n = $treeWCprojects.getSelectionModel().getSelectedNode(); 
	  		Ext.Msg.confirm('Delete ','Are you sure?',function(btn){
				if(btn=='yes')	{
					
					 $treeName.RMconn.request({ 
						params: {action:'deleteVisits',id:n.id },
					 	success:function(resp,opt){ 
							if(Ext.decode(resp.responseText).success)
							{								
								n.remove();
							}
					    } 
					}); 
				
				} else return false;
					 
			});
		  }
	  }");
	 
	 //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',"'center'")
	  ->baseParams = "attr:'id_project,id_structure'";
	 ;
//	 $treeWCprojects->addListener('click',"function(n){
//	 	if(!n.attributes.leaf)
//		{
//			CallScript('_forms/forms.pregledPoseta.php',{visitFolder:n.attributes.id });
//		}
//	 }");

	$treeWCprojects->activateDD('mystVisitView');
	echo $treeWCprojects->CreateTree();
	
	
	echo "$treeWCprojects.on('afterrender',function(){
		$treeWCprojects.dropZone.onNodeOver = function(n, dd, e, data){		
		var n = this.getTargetFromEvent(e);		
		if (n.node.attributes.leaf == false) return 'x-tree-drop-ok-append';		
		return this.dropNotAllowed;
    }
    $treeWCprojects.dropZone.notifyDrop = function(dd, e, data){
    	var n = this.getTargetFromEvent(e);
    	if(!n) return false;
    	if (n.node.attributes.leaf == true) return false;		
		
		
		var combId = new Array();
		var uqA = new Array();
	    flag = 0;
		for(var i=0;i<data.selections.length;i++ ) {
			obj = data.selections[i].data;
			
			//console.log(data.selections[i].data.id);
					
			if (obj.status != 1) flag = 1;
			if ( obj.strucId > 0 && obj.status == 1) {
					combId.push( obj.projId + '_' + obj.strucId);
					uqA.push(obj.uniqueAddress_1);
			}
		}
		
		/*if (flag) {						
			 	alert('test');
		}*/
		
		
		new RMajax().request({
			url:'_ajax/ajax.groups.php',
			params:{
								combId	:	combId.join(),
								uniqId	:	uqA.join('<,>'),							
								action	:	'moveVisits',
								node	: 	n.node.attributes.id,
								RMcomp	: 	'grid',
								RMtable	: 	'pregledPoseta'
								
			},
			success:function(){
				Ext.getCmp('visitsUniqePanel').show();
				Ext.getCmp('MysteryVisits').getLoader().load(n.node,function(){ n.node.expand(); });
			}
		}); 
		
			
		
    	return true;
    }
    
	});";
	
//	echo $treeProjName.".getRootNode().expand();";
		
// we add JS code to define a new panel which has two trees combined in one tab
//this tab we dan add to the western tabpanel with id = 'west-panel'	
?>
	var visitsUniqePanel = new Ext.Panel({
		id:'visitsUniqePanel',
		title:'<?php echo $languagePack['visits']; ?>',
		defaults:{split:true,border:false},
		iconCls:'iconVisits',
		layout:'border',
		border: false,
		items:[<?php echo $treeWCprojects->getName(); ?>  ]
		
				
	});

