<?
if(isset($_POST["checkYear"])){ $sel_year = date("Y",strtotime($_POST['selectYear'])); } else { $sel_year = date("Y");}
if(isset($_POST["checkYear"]) and $_POST['selectYear'] != date("Y")){ $count_month = '12'; } else { $count_month = date("n");}

$dom 	= new DOMDocument;
$dom	->load($baseD);
$xpath	= new DOMXPath($dom);
	
if(isset($_POST['search'])) {
	$query	= mb_strtolower(rawurldecode($_POST['query']), 'UTF-8');
	$deals	= $xpath->query('/deals/deal[@type="deal-a" and contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]');
	$countDeal 	= $deals->length;
	$offers	= $xpath->query('/deals/deal[@type="offer" and contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]');
	$countOffer = $offers->length;
} elseif(isset($_POST['DealFilter'])) {

	if(isset($_POST["checkYear"])) {
		$f_year = ' and datey="'.$sel_year.'"';
		echo '
		<script>
			$(document).ready(function() {
				$("form#filter-list input[name=checkYear]").prop("checked", true);
				$("form#filter-list select[name=selectYear]").prop("disabled",false);
				$("form#filter-list option[value=\''.$_POST['selectYear'].'\']").prop("selected", true);
			});
		</script>
		';
	}

	if(isset($_POST["checkClient"])) {
		$f_client = ' and @client="'.$_POST["selectClient"].'"';
	echo '
	<script>
		$(document).ready(function() {
			$("form#filter-list input[name=checkClient]").prop("checked", true);
			$("form#filter-list select[name=selectClient]").prop("disabled",false);
			$("form#filter-list option[value=\''.$_POST["selectClient"].'\']").prop("selected", true);
		});
	</script>
	';
	} else {
		$f_client = ' and @client';
	}

	if(isset($_POST["checkPrice"])){
		if(isset($_POST["inputPriceS"]) AND $_POST["inputPriceS"] != '') {
			$f_priceS = ' and price>="'.$_POST["inputPriceS"].'"';
		}
		if(isset($_POST["inputPriceE"]) AND $_POST["inputPriceE"] != '') {
			$f_priceE = ' and price<"'.$_POST["inputPriceE"].'"';
		}
		$f_price = $f_priceS.$f_priceE;
	echo '
	<script>
		$(document).ready(function() {
			$("form#filter-list input[name=checkPrice]").prop("checked", true);
			$("form#filter-list input[name=inputPriceS]").prop("disabled",false).val("'.$_POST["inputPriceS"].'");
			$("form#filter-list input[name=inputPriceE]").prop("disabled",false).val("'.$_POST["inputPriceE"].'");
		});
	</script>
	';
	} else {
		$f_price = ' and price';
	}

	$deals 		= $xpath->query('/deals/deal[@type="deal-a"'.$f_year.$f_client.$f_price.' or @type="deal-c"'.$f_year.$f_client.$f_price.']');
	$countDeal 	= $deals->length;
	$offers 	= $xpath->query('/deals/deal[@type="offer"'.$f_year.$f_client.$f_price.']');
	$countOffer = $offers->length;
} else {
	$f_year = ' and datey="'.$sel_year.'"';
	$deals 		= $xpath->query('/deals/deal[@type="deal-a"]');
	$countDeal 	= $deals->length;
	$offers 	= $xpath->query('/deals/deal[@type="offer"'.$f_year.']');
	$countOffer = $offers->length;
}

if(isset($_GET['edit']) AND $_GET['edit'] != '' OR isset($_GET['view']) AND $_GET['view'] != ''){
	if(isset($_GET['edit']) AND $_GET['edit'] != '') {
		$deals = $xpath->query('/deals/deal[@id="'.$_GET['edit'].'"]');
		$dealDir = ROOT . 'crmdata/upload/deals_'.$_GET['edit'];
	}
	if(isset($_GET['view']) AND $_GET['view'] != '') {
		$deals = $xpath->query('/deals/deal[@id="'.$_GET['view'].'"]');
		$dealDir = ROOT . 'crmdata/upload/deals_'.$_GET['view'];
	}
	$dealFile = $dealDir . '/page_description.xml';
	foreach ($deals as $deal) {
		$DealId 	= $deal->getAttribute('id');
		$Type 		= $deal->getAttribute('type');
		$Client 	= $deal->getAttribute('client');
		$Subject 	= $deal->getElementsByTagName("subject")->item(0)->nodeValue;
		$DealNumber = $deal->getElementsByTagName("num")->item(0)->nodeValue;
		$Price 		= $deal->getElementsByTagName("price")->item(0)->nodeValue;
		$Costs 		= $deal->getElementsByTagName("costs")->item(0)->nodeValue;
		$Paid 		= $deal->getElementsByTagName("paid")->item(0)->nodeValue;
		$CommentSm 	= $deal->getElementsByTagName("comment")->item(0)->nodeValue;
		$DateStart 	= $deal->getElementsByTagName("dates")->item(0)->nodeValue;
		$DateEnd 	= $deal->getElementsByTagName("datee")->item(0)->nodeValue;
		$UIP 		= $deal->getElementsByTagName("uip")->item(0)->nodeValue;
	}
	if(file_exists($dealFile)){
		$doz 	= new DOMDocument;
		$doz	->load($dealFile);
		$xpathz = new DOMXPath($doz);
		$Comment = $xpathz->query('/item')->item(0)->getElementsByTagName("text")->item(0)->nodeValue;
	} else {
		$Comment = $CommentSm;
	}
}
?>