<?
if( isset( $_POST['search'] ) AND $_POST['query'] != '' ) {
	$dom = new DOMDocument;
	$dom->load($baseC);
	$xpath = new DOMXPath($dom);
	$query	= mb_strtolower(rawurldecode($_POST['query']), 'UTF-8');
	$clients	= $xpath->query('/clients/client[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]');
	$countClient 	= count($xmlclient->xpath('/clients/client[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]'));
} elseif( isset( $_POST['ClientFilter'] ) ) {
	/* Список - после фильтрации */
	if( isset( $_POST["radioType"] ) ) {
		$filterType = '@type="'.$_POST["radioType"].'"';
		echo '<script>$(function() {$("form#filter-list input[value*=\''.$_POST["radioType"].'\']").prop("checked", true); $(".filter-buttons").fadeIn("500");});</script>';
	} else {
		$filterType = '';
	}
	if( isset( $_POST["radioPriority"] ) ) {
		$filterPriority = '@priority="'.$_POST["radioPriority"].'"';
		echo '<script>$(function() {$("form#filter-list input[value*=\''.$_POST["radioPriority"].'\']").prop("checked", true); $(".filter-buttons").fadeIn("500");});</script>';
	} else {
		$filterPriority = '';
	}
	if( isset( $_POST["radioType"] ) && isset( $_POST["radioPriority"] )) {
		$filterSeparator = ' and ';
	}
	
	$dom = new DOMDocument;
	$dom->load($baseC);
	$xpath = new DOMXPath($dom);
	$clients = $xpath->query('/clients/client['.$filterType.$filterSeparator.$filterPriority.']');
	$countClient 	= count($xmlclient->xpath('/clients/client['.$filterType.$filterSeparator.$filterPriority.']'));
} else {
	/* Список - стандартный вид */
	$dom = new DOMDocument;
	$dom->load($baseC);
	$xpath = new DOMXPath($dom);
	$clients = $xpath->query('/clients/client');
	$countClient 	= count($xmlclient->xpath('/clients/client'));
}

if (file_exists($baseD)) {
	$xmldeal = simplexml_load_file($baseD);
}
if( isset($_GET['edit']) and $_GET['edit'] != '' or isset($_GET['view']) and $_GET['view'] != '' ) {
	$dom = new DOMDocument;
	$dom->load($baseC);
	$xpath = new DOMXPath($dom);
	if(isset($_GET['edit']) AND $_GET['edit'] != '') {
		$clients = $xpath->query('/clients/client[@id="'.$_GET['edit'].'"]');
	}
	if(isset($_GET['view']) AND $_GET['view'] != '') {
		$clients = $xpath->query('/clients/client[@id="'.$_GET['view'].'"]');
	}
	foreach ($clients as $client) {
		$ClientId 		= $client->getAttribute('id');
		$ClientType 	= $client->getAttribute('type');
		$ClientPriority = $client->getAttribute('priority');
		$ClientPhoto	= $client->getElementsByTagName("clientphoto")->item(0)->nodeValue;
		$ClientName 	= $client->getElementsByTagName("clientname")->item(0)->nodeValue;
		$ClientAgent 	= $client->getElementsByTagName("clientagent")->item(0)->nodeValue;
		$ClientPhoneW 	= $client->getElementsByTagName("clientphonew")->item(0)->nodeValue;
		$ClientPhone 	= $client->getElementsByTagName("clientphone")->item(0)->nodeValue;
		$ClientSite 	= $client->getElementsByTagName("clientsite")->item(0)->nodeValue;
		$ClientEmail 	= $client->getElementsByTagName("clientemail")->item(0)->nodeValue;
		$ClientStart 	= $client->getElementsByTagName("clientstart")->item(0)->nodeValue;
		$ClientAdress 	= $client->getElementsByTagName("clientadress")->item(0)->nodeValue;
		$ClientFined 	= $client->getElementsByTagName("clientfined")->item(0)->nodeValue;
		$Сinitials 		= mb_substr($ClientName, 0, 1,'utf-8');
	}
	if( isset($_GET['view']) and $_GET['view'] != '' ) {
		if (file_exists($baseD)) {
			$countClient1	= count($xmldeal->xpath('/deals/deal[@type="deal-a" and @client="'.$ClientId.'"]'));
			$countClient2	= count($xmldeal->xpath('/deals/deal[@type="deal-c" and @client="'.$ClientId.'"]'));
			$countClient3	= count($xmldeal->xpath('/deals/deal[@type="offer" and @client="'.$ClientId.'"]'));
			
			$doc = new DOMDocument;
			$doc->load(ROOT . 'crmdata/database/deals.xml');
			$xpath = new DOMXPath($doc);
			
			$clientDeals = $xpath->query('/deals/deal[@type="deal-a" and @client="'.$ClientId.'" or @type="deal-c" and @client="'.$ClientId.'"]');
			$countDealPrice = '0';
			for ($i = 0; $i < $clientDeals->length; $i++) {
				$countDealPrice = $countDealPrice + $xpath->query('/deals/deal[@type="deal-a" and @client="'.$ClientId.'" or @type="deal-c" and @client="'.$ClientId.'"]')->item($i)->getElementsByTagName("price")->item(0)->nodeValue;
			}
			
			$clientOffers = $xpath->query('/deals/deal[@type="offer" and @client="'.$ClientId.'"]');
			$countOfferPrice = '0';
			for ($i = 0; $i < $clientOffers->length; $i++) {
				$countOfferPrice = $countOfferPrice + $xpath->query('/deals/deal[@type="offer" and @client="'.$ClientId.'"]')->item($i)->getElementsByTagName("price")->item(0)->nodeValue;
			}
			
			$clientPaid = $xpath->query('/deals/deal[@type="deal-a" and @client="'.$ClientId.'" or @type="deal-c" and @client="'.$ClientId.'"]');
			$countPaidPrice = '0';
			for ($i = 0; $i < $clientPaid->length; $i++) {
				$countPaidPrice = $countPaidPrice + $xpath->query('/deals/deal[@type="deal-a" and @client="'.$ClientId.'" or @type="deal-c" and @client="'.$ClientId.'"]')->item($i)->getElementsByTagName("paid")->item(0)->nodeValue;
			}
		}
	}
}
?>