<?php
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");
include_once (PATH_TO_ROOT.'CRM/class/class.logger.php');
include_once(PATH_TO_ROOT."CRM/class/class.checkString.php");

$Logger = new LoggerX($db);

if($_POST['action'] == "getExisting"){
	
	//ini_set("display_errors", 1);
	$name = StringChecker::Check($_POST['Name']);
	$surname = StringChecker::Check($_POST['Surname']);
	$city = StringChecker::Check($_POST['City']);
	$address = StringChecker::Check($_POST['Address']);
	$start = StringChecker::Check($_POST['start']);
	$limit = StringChecker::Check($_POST['limit']);
	
	if((isset($name) && $name != null) || (isset($surname) && $surname != null))
		$filter[] = " (name LIKE '%$name%$surname%' OR name LIKE '%$surname%$name%')";
	if(isset($city) && $city != null)
		$filter[] = " town LIKE '%$city%'";
	if(isset($address) && $address != null)
		$filter[] = " address LIKE '$address%'";
	
	if(!empty($filter)){
		$filter = "WHERE ".implode(" AND ", $filter);
	}
	
	$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".tel___medical $filter LIMIT $start, $limit";
	$row = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".tel___medical $filter";
//	echo "/*$sql*/";
	$res = $db->SQLexecute($sql);
	while($dat = $db->fetchAssoc($res)){
		$data[] = $dat;
	}
	
	$results['success'] = true;
	$results['results'] = $db->NumRows($db->SQLexecute($row));
	$results['rows'] = $data != null ? $data : "";
	
	echo json_encode($results);
	
} elseif ($_POST['action'] == "addNewUser"){
	
	$projectId = StringChecker::Check($_POST['projectId']);
	$cati_location = StringChecker::Check($_POST['cati_location']);
	
	$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = $projectId";
	$res = $db->SQLexecute($sql);
	$dat = $db->fetchAssoc($res);
	$ProjectName = $dat['proj_name'];
	$id_sub_tel = StringChecker::Check($_POST['id_tel']);
	$id_tel = 0;
	$id = StringChecker::Check($_POST['id']);
	$name = StringChecker::Check($_POST['name']);
	$surname = StringChecker::Check($_POST['surname']);
	$address = StringChecker::Check($_POST['address']);
	$zip = StringChecker::Check($_POST['zip']);
	$town = StringChecker::Check($_POST['town']);
	$email = StringChecker::Check($_POST['email']);
	$dan = StringChecker::Check($_POST['dan']);
	$ura = StringChecker::Check($_POST['ura']);

	$sql = "SELECT DISTINCT(presentation_location) FROM ".WARPIT_CRM."._PresentatorPermissions WHERE cati_location = ".$cati_location." AND id_project=".$projectId;

	$res = $db->SQLexecute($sql);
	$data = $db->fetchAssoc($res);
	$tmp = explode(",", $data['presentation_location']);
	$hotel = $tmp[0];
	
	$results['success'] = true;
	
	$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".tel___medical WHERE id = ".$id;
	$rez = $db->SQLexecute($sql);
	$sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic 
				WHERE 
						(
							(	firstname 		LIKE 	'%".$name."%' 
							AND surname 		LIKE 	'%".$surname."%' 
							) 
						OR 
							id_tel = ".$id."
						)
						AND dan 			= 		'".$dan."' 
						AND ura 			= 		'".$ura."' 
						AND cati_location 	= 		'".$cati_location."' 
						AND id_project 		= 		".$projectId;
	$res = $db->SQLexecute($sql);
	if($db->NumRows($res) == 0){
		$zad = $db->fetchAssoc($rez);
		$phone = "0".$zad['oms'].$zad['telephone'];
		$sql = "INSERT INTO ".WARPIT_CRM."._visitStatistic 
					(	id_project, id_tel,	id_sub_tel, 
						firstname, surname, post_code, 
						city, address, email, 
						dan, ura, dogodek, 
						cas_prihoda, cati_location, phone1
					) 
					VALUES ('".$projectId."', '".$zad['id']."', '".$id_sub_tel."', 
							'".$name."', '".$surname."', '".$zip."', 
							'".$town."', '".$address."', '".$email."', '".$dan."', 
							'".$ura."', '".$hotel."', NOW(), 
							'".$cati_location."', '".$phone."')";
		
		$Logger->Log('dodajanje_uporabnika', 'multiple-filed-insert', 'id_project, id_tel, id_sub_tel, firstname, surname, post_code, city, address, email, dan, ura, dogodek, cas_prihoda, cati_location', 
					  WARPIT_CRM."._visitStatistic", " ", addslashes($sql));
		$db->SQLexecute($sql);
		$sql = "SELECT MAX(id) as mId FROM ".WARPIT_CRM."._visitStatistic";
		$res = $db->SQLexecute($sql);
		$zad = $db->fetchAssoc($res);
		$id = $zad['mId'];
		$idField = "id_tel";
		$sql = "UPDATE ".WARPIT_CRM."._visitStatistic SET $idField = ".($id*-1)." WHERE id = ".$id;
		$results['response'] = 0;
	}else{
		
		$results['response'] = 1;
	}	
	echo json_encode($results);
}
?>