<?php 
	include("_dirinfo.php");
	include (PATH_TO_ROOT."init.php");
	include_once (PATH_TO_ROOT."/TGI/init.php");
	include_once (PATH_TO_ROOT."TGI/_class/class.tgi.php");
	include_once(PATH_TO_ROOT."/TGI/_class/class.permissions.php");
	
	$db->ChangeDatabase(DB_WARPIT_TGI);
	
	$tgi = new tgi($db);

	$idUser = $_SESSION ['UserInfo']['id'];
	$typeUser = $_SESSION ['UserInfo']['user_type'];
	$idTGIproject = $_POST['idTGIproject'];
	
	$tgi->activeProject = $idTGIproject;
	$WarpitProjects = $tgi->getWarpitProjects();
	
	$db->LogWrite("ID user:".$idUser." typeUser:".$typeUser." idTGIproject:".$idTGIproject,true,"needed variables inside TGI");
	
	$tgiPermissions = new tgi_permissions($db);
	$tgiUserPermission = $tgiPermissions->getPermissions($idUser,$typeUser,$idTGIproject);
	
	$vrtTabItems = array();
	// if user has right to execute the questionnaire we list all available subprojects
	if ($tgiUserPermission['project'][$idTGIproject]) {
		$WarpitProjectsLinks = $tgi->getActiveQuestionnaireLinks($WarpitProjects,$idUser,$typeUser,$idTGIproject);
		$id_structure = $tgi->getStructureID($idUser,$typeUser);
		
		// get translator between warpit project and id 
		$ID_translator = $tgi->id2TGI_TGI2id($idTGIproject);
		// for each warpit project I get % of finished questionnaire
		$done_pct = $tgi->getUserDone($idTGIproject,$idUser,$typeUser);
		
		// I got system for packets of warpit project to enable
		$tgi->getQuestOrder($idTGIproject);
		
		foreach($tgi->enablePackets AS $keyPack => $itemPack ) {
			$db->LogWrite($keyPack." => ".$itemPack,true,"packet order inside verticalPanel");
			$warpit_project = $ID_translator['pcounter'][$keyPack];
			
			// if I can enable some questionnaire, I check which ones, because some can be already 100%
			if ($done_pct[$warpit_project] == 100) {
				// go through projects which can be set
				foreach($itemPack AS $item4enable) {
					$warpitProject2enable = $ID_translator['pcounter'][$item4enable];
					if ($done_pct[$warpitProject2enable] < 100) {
						$canEnable[$warpitProject2enable] = true;
					}
				}
			}
			
		}
	
		
		if ($WarpitProjectsLinks) {
			$cnt=0;
			foreach ($WarpitProjects['name'] AS $key_wp => $item_wp) {
				// enable the first questionnaire, if it is not already done
				if ($cnt == 0 AND $done_pct[$key_wp] < 100) $canEnable[$key_wp] = true;
				
				$disableTab = true;
				// check if tab can be shown...
				if ($canEnable[$key_wp]) $disableTab = false;
				
				$Qlink = $dbConfigPaths['warpit_www']."survey/index.php?TGIclient=1&loginSID=".$WarpitProjectsLinks[$WarpitProjects['ids'][$key_wp]]."&structurePreDef=".$id_structure;
				$db->LogWrite($Qlink,true,"questionnaire link $key_wp");
				
				
				$singleTab = "{
		    	title: '".$WarpitProjects['C_name'][$key_wp]."<br><div id=pct_".$key_wp.">".$done_pct[$key_wp]."%</div>'
		       ,iconCls: 'iconSklop'";
			   if ($disableTab == true) $singleTab .= ",disabled:true";
		       $singleTab .= ",listeners:{activate:function() {
		       			closeQuestTab();	 
			       		setTimeout(function() {
			       			Ext.getCmp('mainTabPanel').add(new Ext.ux.IFrameComponent({ 
				       	 		closable: false, 
				       	 		iconCls: 'iconInterviewSmall',
				        		title: 'Anketa',
				        		id: 'WarpitProject', 
								url: '".$Qlink."' 
				        	})
				       		).show(); 
				       	}, 500);
					}}
		        }";
				$vrtTabItems[] = $singleTab;
				
				$cnt++;
			}
		}
	}
	
?>

 
<?php if ($typeUser == 1) { ?>							
	 sklopiPanel.removeAll();
	 sklopiPanel.add(
		new Ext.ux.tot2ivn.VrTabPanel({
	    plain:true,
	    title: 'Sklopi',
	    defaults:{autoScroll: true, border: false},
	    items:[<?php echo implode(",",$vrtTabItems)?>]
	   	})
	).show();
	 sklopiPanel.doLayout();

<?php } else { ?>
 	westPanel.removeAll();
	 westPanel.add(
		new Ext.ux.tot2ivn.VrTabPanel({
	    plain:true,
	   
	    defaults:{autoScroll: true, border: false},
	    items:[<?php echo implode(",",$vrtTabItems)?>]
	   	})
	).show();
	 westPanel.doLayout();

<?php } ?>