<?php 

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


$fieldTree = new MakeTree($tabId.'_Fieldtree');

$fieldTree->SetTable('_InterviewerStructure',DB_WARPIT_USERSPOOL);
$fieldTree->SetRootVisible('true','Pool Fields')
	->setWidth('250')
;
$fieldTree
	->addConfig('flex',1)
//	->setReqUrl('webuserspool/_ajax/redirectionManager/ajax.redirectionManager.php')
	;
	$fieldTree->addConfig('collapseMode', "'mini'")
	//button for createing a sample
	->addTbarItem("{ text: 'Load', handler: function(){
		var grTree = Ext.getCmp( 'PoolMenu_1');
		if(grTree)
		{	
			var n = grTree.getSelectionModel().getSelectedNode();
			if(n && n.isLeaf()  ){

				var fields = $fieldTree.getChecked('id');
				new RMajax().request({
					url: 'webuserspool/_ajax/sampling/ajax.userSampling.php',
					params: {
						action: 'createSamplingData',
						fields: fields.join(),
						id_group: n.id
					},
					success: function(){
						var cmp = Ext.getCmp('sampleUserDefault_{$_SESSION['UserInfo']['id']}');
						if(cmp)
							Ext.getCmp('userSamplerTabPanel').remove(cmp);
						CallScript('webuserspool/_forms/sampling/forms.dataSample.php',{uniqueTabId : 'sampleUserDefault_{$_SESSION['UserInfo']['id']}' });
					}
				});
				return;
			}
		}
		
		Ext.Msg.alert('Warning!','Please select a group!');
		
	} }")
	->baseParams = "checked:'leaf',nameField:'field_name'";
;	 


$tree = new MakeTree($tabId.'_Templatetree');

$tree->SetTable('_SamplingTemplates',DB_WARPIT_USERSPOOL);
$tree->SetRootVisible('true','Templates')
	->setWidth('250')
;
$tree
	->addConfig('region',"'west'")
	->activateDD()
	->setReqUrl('webuserspool/_ajax/sampling/ajax.sampleTemplate.php')
	;
	$tree//->setContAddItem('Add template','false','icon-add-record',null,2)
	->setContAddItem('Add template','true','',null,0)
	->setContAddGroup('Add folder','false','icon-add-folder',2)
	->setContRename()
	->setContDelete('Delete','node.hasChildNodes()','icon-delete-record',2)
	->addListener('dblclick',"function(node){
		new RMajax().request({
					url: 'webuserspool/_ajax/sampling/ajax.userSampling.php',
					params: {
						action: 'updateSampling',
						id_template: node.id
					},
					success: function(){
						var tabId = 'sampleUserTemplate_'+node.id;
						var c = Ext.getCmp(tabId);
						if(c)
							c.show();
						else
							CallScript('webuserspool/_forms/sampling/forms.dataSample.php',{uniqueTabId : tabId, id_template: node.id,templateName:node.text });
					}
				});
	}")
	->flex(1)
;	 



echo $tree->CreateTree();


echo $fieldTree->CreateTree();

echo "
	
	var $tabId = new Ext.Panel({
		id:'$tabId',
		title:'CAWI Sampling',
		iconCls:'RMicon_make_sample',
		defaults:{split:true,border:false},
	
		layout:'border',
		border: false,
		closable:true,
		items:[ {region:'west',collapseMode:'mini',xtype:'panel',width:250,layout:{
						    type: 'vbox',
						    align: 'stretch'
							},items: [$fieldTree,$tree] } , { xtype:'tabpanel',id:'userSamplerTabPanel',region:'center'  }   ]
	});
mainTabPanel.add( $tabId ).show();  ";
//let's load last state if it exists!
if($db->TableExists('samplingDataUser___'.$_SESSION['UserInfo']['id'],DB_WARPIT_USERSPOOL))
	echo " 
	new RMajax().request({
					url: 'webuserspool/_ajax/sampling/ajax.userSampling.php',
					params: {
						action: 'updateSampling'
					},
					success: function(resp){
						if(Ext.decode(resp.responseText).success)
						CallScript('webuserspool/_forms/sampling/forms.dataSample.php',{uniqueTabId : 'sampleUserDefault_{$_SESSION['UserInfo']['id']}' });
					}
				});
	";

?>