<?php

	include_once("_dirinfo.php");
	$sessionHandling = true;
	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");



	$db->ChangeDatabase(DB_WARPIT_MAIN);
	$grid = new MakeGrid($_POST['tabId']);
	$grid->setMainTable('pregledPoseta',DB_WARPIT_WEBCATI_BASE);

	$grid->addColumn('projId');
	$grid->addColumn('id_sample');
	$grid->addColumn('id_sampleTable');
	$grid->addColumn('strucId');
	$grid->addColumn('id','Zap broj');
	$grid->addColumn('projekat','projekat');
	$grid->addColumn('podprojekat','Podprojekat');
	$grid->addColumn('uniqueAddress_1','Address')->width(250);
	
	
	// $statCol = $grid->addColumn('status','Status')->type('combo')->comboStore('pregledStatusComboStore','pregledStatusComboStoreData')->comboValue('id')->comboDisplay('name');
	$statCol = $grid->addColumn('status','Status')->renderer('function (a) { 
      				if (!a) return "";
      				st = pregledStatusComboStore;
					index = st.find(\'id\',a);					
	   				return st.getAt(index).get(\'name\');
				} ');
	
	
	if( $_POST['visitsFolder'] || $_POST['visitsFilter'] ) {
		//if there's no data in this row we ingore the click
		$statCol->addIconAction('icon_zoom_document',"CallScript('_forms/forms.report.php',{id_project:record.data.projId,id_record:record.data.strucId});");
	}
	
	$grid->addColumn('visitDateTime','Date/time')->width(130)->editor("new Ext.form.TextField()");
	
	
	$grid->setTitle($_POST['title'])
		->setCheckBoxSelectionModel()
		->addConfig('closable','false')
		->setRemoteSort()
		->setReqUrl('_ajax/ajax.groups.php')
		->setHeight(200)
		->addConfig('closable','true')
		->setFiltering()
		 ->addTbarItem("{iconCls:'x-tbar-loading',handler:function(){ $grid.getStore().reload(); }}");
		 
	if($_POST['tabId'] == 'pregledPosetaTabId') {
	
		$grid->addTbarItem("{
			  	text:'Save',
			  	handler: function(){
			  	
			  		Ext.Msg.prompt('Name','Folder name',function(btn,text){
			  			if(btn=='ok')
			  			{
					  		var recs = $grid.selModel.getSelections();
					  		var combId = new Array();
					  		var uqA = new Array();
					  		for(var i=0;i<recs.length;i++ )
					  		{
					  			// check if records are done and/or confirmed
					  			if ( recs[i].data.strucId > 0) {
					  				// project ID + structure ID
									combId.push( recs[i].data.projId+'_'+recs[i].data.strucId );
									uqA.push( recs[i].data.uniqueAddress_1 );
					  			}		  			
					  		}
					  		$grid.RMconn.request({params:{
					  			combId:combId.join(),
					  			uniqId:uqA.join('<,>'),	
					  			action:'saveVisits',
					  			folder:text
					  		},
					  		success:function(){
					  			Ext.getCmp('visitsUniqePanel').show();
					  			Ext.getCmp('MysteryVisits').getRootNode().reload();
					  		}
					  		});
					  	}
			  		})
			  	}
			  }");
		
		$grid->addTbarItem("{
			  	text:'Move',
			  	handler: function(){
			  			  
			  		tree = Ext.getCmp('MysteryVisits');
			  		SelectedNode = tree.getSelectionModel().getSelectedNode();	 
			  		
			  		if (SelectedNode == null ||  SelectedNode.attributes.id_project > 0 ) {
			  		
			  			alert('Please select visit Folder');
			  			return;
			  		}
			  		
					var recs = $grid.selModel.getSelections();
					var combId = new Array();
					var uqA = new Array();
					for(var i=0;i<recs.length;i++ ) {
						
						// check if records are done and/or confirmed
						if ( recs[i].data.strucId > 0) {
							// project ID + structure ID
							combId.push( recs[i].data.projId + '_' + recs[i].data.strucId);
							uqA.push(recs[i].data.uniqueAddress_1);
						}
					}
					$grid.RMconn.request({
						params:{
								combId:combId.join(),
								uniqId:uqA.join('<,>'),							
								action:'moveVisits',
								node: SelectedNode.id,
								 
								
						},
						success:function(){
								Ext.getCmp('visitsUniqePanel').show();
								Ext.getCmp('MysteryVisits').getLoader().load(SelectedNode,function(){ SelectedNode.expand(); });
						}
					});  		
			  	}
			  }");
		
		
		$grid->addTbarItem("{
			  	text:'Make interview',
			  	handler: function(){
	
			  		var recs = $grid.selModel.getSelections();
					if (recs.length != 1) {
						alert('Please select only one sample point');
						return;
					}
					
					if ( recs[0].data.strucId > 0) {
							alert('This sample point is finished');
							return;
					}
					
					mainTabPanel.add(new Ext.ux.IFrameComponent({ 
					             						closable: true, 
					             						title: 'Make Interview: ' + recs[0].data.uniqueAddress_1, 				             						
					             						id: recs[0].data.id, 
					             						url: '_ajax/ajax.make-interview.php?id_project=' +  recs[0].data.projId + '&id_sample=' + recs[0].data.id_sample 
					             					})
					 ).show(); 
					
			  	}
			  }");
	
	}
	
	if($_POST['visitsFolder']) {
		
		$grid->baseParams= "visitsFolder:'".$_POST['visitsFolder']."'";
		
	} elseif ($_POST['visitsFilter']) {
		$grid->baseParams= "projectIds:'".$_POST['projectId']."', visitsFilter: '".$_POST['visitsFilter']."'";
		
	} else { 
		$grid->baseParams= "projectIds:'".$_POST['projectIds']."'";
	}
	
	// we add extra variables in afteredit event in grid which are send with ajax after some field is edited. In our case date is editable.
	$grid->ExtraAfterEditParameters[] = " project_name: e.record.get('podprojekat') ";
	$grid->ExtraAfterEditParameters[] = " id_record: e.record.get('strucId') ";

	echo $grid->CreateGrid();
	
?>
	mainTabPanel.add(<?php echo $grid;?>).show();

