<?php
$path = explode('/' , $_SERVER['PHP_SELF']);

require_once($_SERVER['DOCUMENT_ROOT'].'/'.$path[1].'/functions/crm_settings.php');
if(isset($_POST['query']) AND $_POST['query'] != ''){
	if($_POST['module'] == 'tasks' or $_POST['module'] == 'tasks-archive'){
		$DB 		= $baseT;
		$DB_item 	= 'task';
		$DB_items 	= 'tasks';
	} elseif($_POST['module'] == 'clients'){
		$DB 		= $baseC;
		$DB_item 	= 'client';
		$DB_items 	= 'clients';
	} elseif($_POST['module'] == 'deals' or $_POST['module'] == 'deals-archive'){
		$DB 		= $baseD;
		$DB_item 	= 'deal';
		$DB_items 	= 'deals';
	} elseif($_POST['module'] == 'users'){
		$DB 		= $baseU;
		$DB_item 	= 'user';
		$DB_items 	= 'users';
	} elseif($_POST['module'] == 'events'){
		$DB 		= $baseE;
		$DB_item 	= 'event';
		$DB_items 	= 'events';
	} elseif($_POST['module'] == 'payments'){
		$DB 		= $baseP;
		$DB_item 	= 'payment';
		$DB_items 	= 'payments';
	} elseif($_POST['module'] == 'letters'){
		$DB 		= $baseLT;
		$DB_item 	= 'letter';
		$DB_items 	= 'letters';
	} elseif($_POST['module'] == 'requests'){
		$DB 		= $baseR;
		$DB_item 	= 'request';
		$DB_items 	= 'requests';
	} elseif($_POST['module'] == 'vacations'){
		$DB 		= $baseV;
		$DB_item 	= 'vacation';
		$DB_items 	= 'vacations';
	} else {
		$DB 		= $_SERVER['DOCUMENT_ROOT'].'/'.$path[1].'/crmdata/database/notebooks/notebook-' . $_POST['login'] . '.xml';
		$DB_item 	= 'note';
		$DB_items 	= 'notes';
	}
	$dom 	= 	new DOMDocument; 
	$dom	->	load($DB);
	$xpath 	= 	new DOMXPath($dom);
	
	$query 	= 	$xpath->query('/'.$DB_items.'/'.$DB_item.'[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.mb_strtolower($_POST['query'], 'UTF-8').'")]');

	if($query->length < 1){
		echo '<span class="search_item "><a href="#">Ничего не найдено</a></span>';
	} else {
		
		if($_POST['module'] == 'tasks' or $_POST['module'] == 'tasks-archive'){
			$i = 0; $start = 0; $end = 10;
			foreach ($query as $item) if($start++ <= $end) {
				$ID 	= $item->getAttribute('id');
				$NAME 	= $item->getElementsByTagName("taskname")->item(0)->nodeValue;
				echo '<span class="search_item "><a href="/'.$path[1].'/tasks/?view='.$ID.'" target="_blank">'.$NAME.'</a></span>';
			}
		} elseif($_POST['module'] == 'clients'){
			$i = 0; $start = 0; $end = 10;
			foreach ($query as $item) if($start++ <= $end) {
				$ID 	= $item->getAttribute('id');
				$NAME 	= $item->getElementsByTagName("clientname")->item(0)->nodeValue;
				echo '<span class="search_item "><a href="/'.$path[1].'/clients/?view='.$ID.'" target="_blank">'.$NAME.'</a></span>';
			}
		} elseif($_POST['module'] == 'deals' or $_POST['module'] == 'deals-archive'){
			$i = 0; $start = 0; $end = 10;
			foreach ($query as $item) if($start++ <= $end) {
				$ID 		= $item->getAttribute('id');
				$NUM 		= $item->getElementsByTagName("num")->item(0)->nodeValue;
				$CLIENT 	= $item->getAttribute('client');
				if(is_object($item->getElementsByTagName("subject")->item(0))){
					$SUBJECT 	= $item->getElementsByTagName("subject")->item(0)->nodeValue;
				} else {
					$SUBJECT 	= 'Метка сделки не указана';
				}
				echo '<span class="search_item "><a href="/'.$path[1].'/deals/?view='.$ID.'" target="_blank">'.$NUM.' - '.get_client_name($CLIENT).' - "'.$SUBJECT.'"</a></span>';
			}
		} elseif($_POST['module'] == 'users'){
			$i = 0; $start = 0; $end = 10;
			foreach ($query as $item) if($start++ <= $end) {
				$ID 		= $item->getAttribute('id');
				$NAME 		= $item->getElementsByTagName("username")->item(0)->nodeValue;
				$SURNAME 	= $item->getElementsByTagName("usersurname")->item(0)->nodeValue;
				
				if(is_object($item->getElementsByTagName("userposition")->item(0))){
					$POSITION 	= ' - ' . $item->getElementsByTagName("userposition")->item(0)->nodeValue;
				} else {
					$POSITION 	= '';
				}
				echo '<span class="search_item "><a href="/'.$path[1].'/users/?view='.$ID.'" target="_blank">'.$SURNAME.' '.$NAME.$POSITION.'</a></span>';
			}
		} elseif($_POST['module'] == 'vacations'){
			$i = 0; $start = 0; $end = 10;
			foreach ($query as $item) if($start++ <= $end) {
				$ID 		= $item->getAttribute('id');
				$UNAME 		= $item->getAttribute('user');
				echo '<span class="search_item "><a href="/'.$path[1].'/vacations/?id='.$ID.'" target="_blank">Заявление №'.$ID.' - '.get_user_name_login($UNAME, '').'</a></span>';
			}
		} elseif($_POST['module'] == 'events'){
			$i = 0; $start = 0; $end = 10;
			foreach ($query as $item) if($start++ <= $end) {
				$ID 	= $item->getAttribute('id');
				$NAME 	= $item->getElementsByTagName("name")->item(0)->nodeValue;
				echo '<span class="search_item "><a href="/'.$path[1].'/events/?view='.$ID.'" target="_blank">'.$NAME.'</a></span>';
			}
		} elseif($_POST['module'] == 'payments'){
			$i = 0; $start = 0; $end = 10;
			foreach ($query as $item) if($start++ <= $end) {
				$DID 	= $item->getAttribute('did');
				$NAME 	= $item->getElementsByTagName("deal")->item(0)->nodeValue;
				$SUBJ	= get_deal_info($DID, 'subject');
				$CLIENT	= get_client_name(get_deal_info($DID, 'cid'));
				echo '<span class="search_item "><a href="/'.$path[1].'/deals/?view='.$DID.'" target="_blank">'.$NAME.' - '.$CLIENT.' - "'.$SUBJ.'"</a></span>';
			}
		} elseif($_POST['module'] == 'letters'){
			$i = 0; $start = 0; $end = 10;
			foreach ($query as $item) if($start++ <= $end) {
				$ID 		= $item->getAttribute('id');
				$NAME 		= $item->getElementsByTagName("fromname")->item(0)->nodeValue;
				$SUBJECT	= $item->getElementsByTagName("subject")->item(0)->nodeValue;
				echo '<span class="search_item "><a href="/'.$path[1].'/letters/?view='.$ID.'" target="_blank">От: '.$NAME.' - "'.$SUBJECT.'"</a></span>';
			}
		} else {
			$i = 0; $start = 0; $end = 10;
			foreach ($query as $item) if($start++ <= $end) {
				$ID 		= $item->getAttribute('id');
				$NAME 		= $item->getElementsByTagName("notename")->item(0)->nodeValue;
				echo '<span class="search_item "><a href="/'.$path[1].'/notes/?view='.$ID.'" target="_blank">'.$NAME.'</a></span>';
			}
		}
	}

} else {
	echo '<span class="search_item "><a href="#">Ничего не найдено</a></span>';
}