<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");


$dbConfigCapi = configLoader::loadConfig('capi');
	
$sql = "SELECT  translate_key, translate_text 
				FROM  ".DB_WARPIT_MAIN."._languagePack 
					WHERE id_programme = 2 AND id_language = ".$dbConfigCapi['default-language']." AND sub_page = 'capi'";
	//echo $sql;
$res = $db->SQLexecute($sql);
while ($zad = $db->fetchAssoc($res)) $languagePack[$zad['translate_key']] = $zad['translate_text'];
	

if ($_POST['action'] == "change-project") {
	
	$_SESSION['capi']['id_project'] = $_POST['id_project'];
	
	echo "store = Ext.getCmp('CapiAppointmentsGrid').getStore();";
	echo "store.removeAll();";
	echo "store.reload();";

} elseif ($_POST['action'] == "interrupt") {
	
	
	$sql = "UPDATE ".DB_WARPIT_WEBCATI_BASE."._Capi_interviewer_visits SET status_obiska = 9 WHERE id = ".$_POST['id_visit'];
	$db->SQLexecute($sql);
	
	echo "Ext.getCmp('CapiAppointmentsGrid').getStore().reload();	";
    echo "Ext.getCmp('surv-panel-".$_SESSION['capi']['id_project']."').destroy(); ";


} elseif($_POST['action'] == "start-new" || $_POST['action'] == "start-test" ||  $_POST['action'] == "continue-with-interview") {
	
	if (!$_SESSION['capi']['id_project']) {
		echo "	Ext.Msg.show({
					title:'".$languagePack['project-status']."',
					msg: '".$languagePack['no-project-is-selected']."',
					buttons: Ext.Msg.OK,
					animEl: 'elId',
					icon: Ext.MessageBox.QUESTION
				});";
		exit;
	} else {
		$date = date("Y-m-d H:i:s");
		$sql = 	" SELECT proj_name,loginSID,naziv_proj FROM ".DB_WARPIT_WEBCATI_BASE."._www_ids as a 
				INNER JOIN ".DB_WARPIT_WEBCATI_BASE."._Projects as b ON a.id_project = b.id
				WHERE 	b.active = 1 
						AND b.start_date < '".$date."' 
						AND b.end_date > '".$date."' 
						AND b.id = ".$_SESSION['capi']['id_project']." 
						AND a.id_user = ".$_SESSION['UserInfo']['id']." 
						AND a.user_type=".$_SESSION['UserInfo']['user_type'];
		$res = $db->SQLexecute($sql);
		$zad = $db->fetchAssoc($res);
		if ($zad['loginSID']) {
			if (!$zad['naziv_proj']) $projectName = $zad['proj_name']; else $projectName =  $zad['naziv_proj'];
			
			if ($_POST['action'] == "continue-with-interview") {
				
				 $sql = "UPDATE ".DB_WARPIT_WEBCATI_BASE."._Capi_interviewer_visits SET status_obiska = 2 WHERE id = ".$_POST['id_record'];
				 $db->SQLexecute($sql);

				 $sql = "SELECT quest_id FROM ".DB_WARPIT_WEBCATI_BASE."._Capi_interviewer_visits WHERE id = ".$_POST['id_record'];
				 $res =  $db->SQLexecute($sql);
				 $zad = $db->fetchAssoc($res);
				 $quest = $zad['quest_id'];
				 
				 
				 $sql = "INSERT INTO ".DB_WARPIT_WEBCATI_BASE."._Capi_interviewer_visits 
							SET
								obisk_st = 1,
								id_project = '".$_SESSION['capi']['id_project']."',
								user_id = '".$_SESSION['UserInfo']['id']."',
								user_type = '".$_SESSION['UserInfo']['user_type']."',
								cas_obiska = NOW(),
								quest_id = ".$quest.";";
				 
				 $db->SQLexecute($sql);
				 $lastID = $db->GetLastInsertId();
				 
				 $sql = 	" SELECT loginSID FROM ".DB_WARPIT_WEBCATI_BASE."._www_ids 
								WHERE 
										id_project 	= ".$_SESSION['capi']['id_project']."
									AND id_user 	= ".$_SESSION['UserInfo']['id']."
									AND user_type 	= 4
									And id_site		= ".$quest;
				$res = $db->SQLexecute($sql);
				$zad = $db->fetchAssoc($res);
				$link = "../survey/index.php?loginSID=".$zad['loginSID']."&capi=1";
				
				echo "Ext.getCmp('CapiAppointmentsGrid').getStore().reload();";
			
			} elseif ($_POST['action'] == "start-new") {

				// now I have checked if interviewer has permission for interviewing on this project
				// next step is to prepare record in capi interviewer visits and to create new login SID for this interviewer
				
				
				// -------------- ADDING NEW VISIT INTO VISIT TABLE ----------------------------------
				$sql = "INSERT INTO ".DB_WARPIT_WEBCATI_BASE."._Capi_interviewer_visits 
							SET
								obisk_st = 1,
								id_project = ".$_SESSION['capi']['id_project'].",
								user_id  = ".$_SESSION['UserInfo']['id'].",
								user_type = '".$_SESSION['UserInfo']['user_type']."',
								cas_obiska = NOW() ";
				$db->SQLexecute($sql);
				$lastID = $db->GetLastInsertId();
				
				$sql = "UPDATE ".DB_WARPIT_WEBCATI_BASE."._Capi_interviewer_visits SET quest_id = ".$lastID." WHERE id = ".$lastID;
				$db->SQLexecute($sql);
				
				// -------------- CREATING NEW loginSID ----------------------------------
				
					include(PATH_TO_ROOT."webcati/_ajax/generateWwwIds/ajax.generate-capi-sid.php");
					
				// -------------- GET new loginSID 	
				
				$sql = 	" SELECT loginSID FROM ".DB_WARPIT_WEBCATI_BASE."._www_ids 
								WHERE 
										id_project 	= ".$_SESSION['capi']['id_project']."
									AND id_user 	= ".$_SESSION['UserInfo']['id']."
									AND user_type 	= 4
									And id_site		= ".$lastID;
				$res = $db->SQLexecute($sql);
				$zad = $db->fetchAssoc($res);
				$link = "../survey/index.php?loginSID=".$zad['loginSID']."&capi=1&id_site=".$lastID;
				
			
			} else {
				
				$sql = 	" SELECT loginSID FROM ".DB_WARPIT_WEBCATI_BASE."._www_ids 
								WHERE 
										id_project 	= ".$_SESSION['capi']['id_project']."
									AND id_user 	= 0
									AND user_type 	= 3 ";
									
				$res = $db->SQLexecute($sql);
				$zad = $db->fetchAssoc($res);
				$link = "../survey/index.php?loginSID=".$zad['loginSID']."&test=1&capi=1";
				
			}
	
			echo "mainTabPanel.add( 
					new Ext.Panel({
						title: 'Interview: ".$projectName."', 
						closable: true, 
						id: 'surv-panel-".$_SESSION['capi']['id_project']."', ";
			
			if ($_POST['action'] == "start-new" || $_POST['action'] == "continue-with-interview") :
				echo 	"   tbar: [{	        		
					        			 text: '<span style=\'margin-left:10px\'><b>Interrupt Interview</b></span>',
					        			 iconCls: 'buttonInterrupt',
					        			 id: 'surveyButtonInterrupt',
							           	 handler: function() {
							           	 
							           	 	Ext.Msg.show({
													   title:'".$languagePack['interrupt-interview']."',
													   msg: '".$languagePack['interrupt-are-sure']."',
													   buttons: Ext.Msg.YESNO,
													   fn: function(selBtn) { 
													   		if (selBtn == 'yes') {
													   			CallScript(\"_ajax/ajax.project-actions.php\",{ action: 'interrupt', id_visit : '".$lastID."' }) 			
													   		}
													   },
													   animEl: 'elId',
													   icon: Ext.MessageBox.QUESTION
													});		           	 						            	
							            }
					        		
	        					},
	        					{	        		
					        			 text: '<span style=\'margin-left:10px\'><b>".$languagePack['make-appointment']."</b></span>',
					        			 iconCls: 'buttonAppointment',
					        			 id: 'surveyButtonAppointment',
							           	 handler: function() {
							           	 
							           	 	Ext.Msg.show({
													   title:'".$languagePack['appointment']."',
													   msg: '".$languagePack['sure-make-appointment']."',
													   buttons: Ext.Msg.YESNO,
													   fn: function(selBtn) { 
													   		if (selBtn == 'yes') {													   			
													   			CallScript(\"_forms/window.capi.appointment.php\",{ id_visit : '".$lastID."' })  			
													   		}
													   },
													   animEl: 'elId',
													   icon: Ext.MessageBox.QUESTION
													});		           	 						            	
							            }
					        		
	        					}
	        					], ";
	        endif;
	        			
	        	echo "	items: new Ext.ux.IFrameComponent({ 							
								width: '100%',
								height: '100%',
								id: 'survey-".$_SESSION['capi']['id_project']."', 
								url: '".$link."'
							})
	        	})).show();";
		}
	}
	
} elseif ($_POST['action'] == "finishQuestionnaire") {
	//echo "alert('test12345');";
	echo "Ext.getCmp('surveyButtonAppointment').hide();";
	echo "Ext.getCmp('surveyButtonInterrupt').hide();";
}

?>