<?
if(isset($_POST["checkYear"])){$sel_year = date("Y",strtotime($_POST['selectYear'])); } else {$sel_year = '';}
if(isset($_POST["checkYear"]) and $_POST['selectYear'] != date("Y")){ $count_month = '12'; } else { $count_month = date("n");}
if(isset($_POST["selectDealType"])){ $sel_type = $_POST["selectDealType"];} else { $sel_type = '';}
	
$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[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]');
	$countDeal 	= count($xmldeal->xpath('/deals/deal[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]'));
} elseif( isset( $_POST['DealFilter'] ) ) {
	
	if( isset( $_POST["checkDealType"] ) ) {
		$f_type = '@type="'.$sel_type.'"';
		echo '
		<script>
			$(function() {
				$("form#filter-list input[name=checkDealType]").prop("checked", true);
				$("form#filter-list select[name=selectDealType]").prop("disabled",false);
				$("form#filter-list option[value=\''.$sel_type.'\']").prop("selected", true);
			});
		</script>
		';
	} else {
		$f_type = '@type';
	}
	
	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=\''.$sel_year.'\']").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['.$f_type.$f_year.$f_client.$f_price.']');
	$countDeal 	= $deals->length;
} elseif( isset( $_GET['client'] ) or isset( $_GET['type'] ) ) {
	if( isset( $_GET['type'] ) ) {
		$f_type 	= '@type="'.$_GET['type'].'"';
	} else {
		$f_type 	= '@type!=""';
	}
	if( isset( $_GET['client'] ) ) {
		$f_client	= ' and @client="'.$_GET['client'].'"';
	} else {
		$f_client 	= '';
	}
	$dom = new DOMDocument;
	$dom->load($baseD);
	$xpath = new DOMXPath($dom);
	$deals = $xpath->query('/deals/deal['.$f_type.$f_client.']');
	$countDeal 	= $deals->length;
} else {
	$deals 		= $xpath->query('/deals/deal');
	$countDeal 	= $deals->length;
}
?>