<?php

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



if ($_POST['action'] == 'update') {
	
	$sql = "UPDATE   ".DB_WARPIT_WEBCATI_BASE."._Capi_interviewer_visits   
				SET 
					".$_POST['field']." = '".$_POST['value']."' 
				WHERE  id = ".$_POST['id'];
	$db->SQLexecute($sql);
	echo "{ success : true, sql: '".$sql."'}";
	
}  else {
	if (!$_SESSION['capi']['id_project']) {
		echo "{
				success: true,
			    results: 0 			          	    
			  }";
		
		exit;
	}
	
	
	
	
	$sql = "SELECT * FROM  ".DB_WARPIT_WEBCATI_BASE."._Projects WHERE id = ".$_SESSION['capi']['id_project'];
	$res = $db->SQLexecute($sql);
	$projectINFO = $db->fetchAssoc($res);
	
	$sql = " SELECT a.id_sampleTable, name FROM ".DB_WARPIT_WEBCATI_BASE.".".$projectINFO['proj_name']."___sample as a
				LEFT JOIN  ".DB_WARPIT_WEBCATI_BASE."._CapiTables as b ON a.id_sampleTable = b.id
			 WHERE a.user_id = ".$_SESSION['UserInfo']['id']." AND  a.user_type = ".$_SESSION['UserInfo']['user_type']."
				GROUP BY a.id_sampleTable ";
	
	//$db->LogMe($sql);
	 //echo "/*".$sql."*/"; 
	$res = $db->SQLexecute($sql);
	
	
	if ( intval($_POST['StatusConditionGroup']) > 0 ) {
		$StatusCondition = " c.status_obiska IN ( SELECT status_id FROM ".DB_WARPIT_WEBCATI_BASE."._Capi_interviewer_visits_status WHERE parent_id = ".intval($_POST['StatusConditionGroup'])." ) ";
	} else $StatusCondition = "( c.status_obiska IS NULL  OR c.status_obiska = 0  OR c.status_obiska IN ( SELECT status_id FROM  ".DB_WARPIT_WEBCATI_BASE."._Capi_interviewer_visits_status WHERE default_view = 1 ) )";
	//$StatusCondition = " ( c.status_obiska IS NULL OR  c.status_obiska IN () ";
	
	//$StatusCondition = " 1 = 1";
	while ($zad = $db->fetchAssoc($res)) {
		
		$sql = "  
							SELECT 
									a.id as IDsample,
									b.id as IDsampleMain,
									a.id_sampleTable as IDsampleTable,
									b.*,
									c.cas_obiska as visit_time,
									c.ponovni_dogovor as appointment_date,
									(SELECT count(*) as n FROM `".DB_WARPIT_WEBCATI_BASE."`.`_Capi_interviewer_visits` WHERE sample_point = c.sample_point ) as number_of_contacts,
									c.priimek_gosp as contact_person,
									c.komentar as comment,
									c.status_obiska as interview_status,
									d.status_group_name as interview_status_label,
									'".$projectINFO['proj_name']."' as projName
							FROM `".DB_WARPIT_WEBCATI_BASE."`.`".$projectINFO['proj_name']."___sample` as a

								LEFT JOIN `".DB_WARPIT_WEBCATI_BASE."`.`capi___".$zad['name']."` as b 
									ON a.id_sample = b.id AND a.id_sampleTable = ".$zad['id_sampleTable']."
								LEFT JOIN `".DB_WARPIT_WEBCATI_BASE."`.`_Capi_interviewer_visits` as c 
									ON a.id = c.sample_point AND c.id IN (SELECT  MAX(`id`) as id FROM `".DB_WARPIT_WEBCATI_BASE."`.`_Capi_interviewer_visits` WHERE id_project = ".$_SESSION['capi']['id_project']." GROUP BY sample_point) 
								LEFT JOIN 	`".DB_WARPIT_WEBCATI_BASE."`.`_Capi_interviewer_visits_status` as d ON c.status_obiska = d.status_id AND d.parent_id > 0
							WHERE 
											a.user_id = ".$_SESSION['UserInfo']['id']." 
									AND  	a.user_type = ".$_SESSION['UserInfo']['user_type']."
									AND 	".$StatusCondition."
									
						 ";
		 echo "/*".$sql."*/"; 
		$resSample = $db->SQLexecute($sql);
		while ($zadSample = $db->fetchAssoc($resSample)) {
			if ($zadSample['appointment_date'] != '0000-00-00 00:00:00' && $zadSample['appointment_date'] != NULL ) {
				$t = explode(" ",$zadSample['appointment_date']);
				$tmp  = explode("-",$t[0]);
				$zadSample['appointment_date'] = $tmp[2].".".$tmp[1].".".$tmp[0]." ".$t[1];
			} else {
				$zadSample['appointment_date'] = "";
			}
			
			
			if ($zadSample['visit_time'] != '0000-00-00 00:00:00' && $zadSample['visit_time'] != NULL ) {
				$t = explode(" ",$zadSample['visit_time']);
				$tmp  = explode("-",$t[0]);
				$zadSample['visit_time'] = $tmp[2].".".$tmp[1].".".$tmp[0]." ".$t[1];
			}
			$data[] = $zadSample;
		}
		
	}
	
	// echo "/*".$sql."*/"; 
?>
	
	{
		success: true,
	    results: <?php echo sizeof($data);?>, 
	    rows: <?php echo json_encode($data); ?>        	    
	}
<?php 
}
?>