<?
if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();
/** @var CBitrixComponent $this */
/** @var array $arParams */
/** @var array $arResult */
/** @var string $componentPath */
/** @var string $componentName */
/** @var string $componentTemplate */
/** @global CDatabase $DB */
/** @global CUser $USER */
/** @global CMain $APPLICATION */

use Bitrix\Main\Context,
	Bitrix\Main\Type\DateTime,
	Bitrix\Main\Loader,
	Bitrix\Iblock;

global $NavNum;
$NavNum = 0;

if($this->StartResultCache(60*60*24, md5(var_export($_POST, true))))
{
	if(CModule::IncludeModule('iblock'))
	{
		$arResult = [
			'SELECTED' => [], 
			'ITEMS' => []
		];

		$query = null;
		$query_arr = [];

		$sort = Array("SORT" => "ASC", "NAME" => "ASC");
		if(isset($arParams['SORT']))
		{
			if($arParams['SORT'] === "name")
			{
				$sort = Array("SORT" => "ASC", "NAME" => "ASC");
				$arResult['SCROLL'] = true;
			}
			if($arParams['SORT'] === "low")
			{
				$sort = Array("SORT" => "ASC", "PROPERTY_PRICE" => "ASC");
				$query_arr['SORT'] = $arParams['SORT'];
				$arResult['SCROLL'] = true;
			}
			if($arParams['SORT'] === "high")
			{
				$sort = Array("SORT" => "ASC", "PROPERTY_PRICE" => "DESC");
				$query_arr['SORT'] = $arParams['SORT'];
				$arResult['SCROLL'] = true;
			}

			$arResult['SELECTED']['SORT'] = $arParams['SORT'];
		}
		$filter = Array("ACTIVE" => "Y", "IBLOCK_ID" => 1);
		$options = Array("nPageSize" => 6);

		$pages_url = $arParams['PATH'];

		$arResult['EXTENDED'] = false;
		$arResult['FILTER_SMALL'] = $arParams['FILTER_SMALL'] ? true : false;

		if(!empty($arParams['BRAND']))
		{
			$brands_res = CIBlockElement::GetList(["NAME" => "ASC"], ["ACTIVE" => "Y", "IBLOCK_ID" => 8, "CODE" => $arParams['BRAND']], false, []);
			while ($brands_ob_element = $brands_res->GetNextElement())
			{
				$brands_ar_res = $brands_ob_element->GetFields();
				$filter['PROPERTY_BRAND'] = $brands_ar_res['ID'];
				$pages_url = $pages_url.$brands_ar_res['CODE']."/";
				$arResult['SELECTED']['BRAND'] = $brands_ar_res['CODE'];
			}

			$arResult['EXTENDED'] = true;
			$arResult['FILTER_SMALL'] = false;
			$arResult['SCROLL'] = true;
		}

		if(!empty($arParams['MODEL']))
		{
			$models_res = CIBlockElement::GetList(["NAME" => "ASC"], ["ACTIVE" => "Y", "IBLOCK_ID" => 9, "CODE" => $arParams['MODEL']], false, []);
			while ($models_ob_element = $models_res->GetNextElement())
			{
				$models_ar_res = $models_ob_element->GetFields();
				$filter['PROPERTY_MODEL'] = $models_ar_res['ID'];
				$pages_url = $pages_url.$models_ar_res['CODE']."/";
				$arResult['SELECTED']['MODEL'] = $models_ar_res['CODE'];
			}

			$arResult['EXTENDED'] = true;
			$arResult['SCROLL'] = true;
		}

		if(!empty($arParams['MODIFICATION']))
		{
			$arResult['EXTENDED'] = true;
			$arResult['SELECTED']['MODIFICATION'] = $arParams['MODIFICATION'];

			$filter['PROPERTY_MODIFICATION'] = $arParams['MODIFICATION'];
			$query_arr['MODIFICATION'] = $arParams['MODIFICATION'];
			$arResult['SCROLL'] = true;
		}

		if(!empty($arParams['DRIVE']))
		{
			$arResult['EXTENDED'] = true;
			$arResult['SELECTED']['DRIVE'] = $arParams['DRIVE'];
			$filter['=PROPERTY_DRIVE'] = $arParams['DRIVE'];

			$query_arr['DRIVE'] = $arParams['DRIVE'];
			$arResult['SCROLL'] = true;
		}

		if(!empty($arParams['BODY']))
		{
			$arResult['EXTENDED'] = true;
			$arResult['SELECTED']['BODY'] = $arParams['BODY'];
			$filter['=PROPERTY_BODY'] = $arParams['BODY'];

			$query_arr['BODY'] = $arParams['BODY'];
			$arResult['SCROLL'] = true;
		}

		if(!empty($arParams['GEAR']))
		{
			$arResult['EXTENDED'] = true;
			$arResult['SELECTED']['GEAR'] = $arParams['GEAR'];
			$filter['=PROPERTY_GEAR'] = $arParams['GEAR'];

			$query_arr['GEAR'] = $arParams['GEAR'];
			$arResult['SCROLL'] = true;
		}

		if(!empty($arParams['ENGINE_FUEL']))
		{
			$filter['=PROPERTY_ENGINE_FUEL'] = $arParams['ENGINE_FUEL'];
			$arResult['EXTENDED'] = true;
			$arResult['SELECTED']['ENGINE_FUEL'] = $arParams['ENGINE_FUEL'];

			$query_arr['ENGINE_FUEL'] = $arParams['ENGINE_FUEL'];
			$arResult['SCROLL'] = true;
		}

		if(!empty($arParams['ENGINE_POWER_FROM']) || !empty($arParams['ENGINE_POWER_TO']))
		{
			$filter['><PROPERTY_ENGINE_POWER'] = [ !empty($arParams['ENGINE_POWER_FROM']) ? $arParams['ENGINE_POWER_FROM'] : 0, !empty($arParams['ENGINE_POWER_TO']) ? $arParams['ENGINE_POWER_TO'] : 1000 ];
			$arResult['EXTENDED'] = true;
			if(!empty($arParams['ENGINE_POWER_FROM']))
			{
				$arResult['SELECTED']['ENGINE_POWER_FROM'] = $arParams['ENGINE_POWER_FROM'];
				$query_arr['ENGINE_POWER_FROM'] = $arParams['ENGINE_POWER_FROM'];
			}
			if(!empty($arParams['ENGINE_POWER_TO']))
			{
				$arResult['SELECTED']['ENGINE_POWER_TO'] = $arParams['ENGINE_POWER_TO'];
				$query_arr['ENGINE_POWER_TO'] = $arParams['ENGINE_POWER_TO'];
			}
			$arResult['SCROLL'] = true;
		}

		if(!empty($arParams['ENGINE_VOLUME_FROM']) || !empty($arParams['ENGINE_VOLUME_TO']))
		{
			$filter['><PROPERTY_ENGINE_VOLUME'] = [ !empty($arParams['ENGINE_VOLUME_FROM']) ? $arParams['ENGINE_VOLUME_FROM'] : 0, !empty($arParams['ENGINE_VOLUME_TO']) ? $arParams['ENGINE_VOLUME_TO'] : 10000 ];
			$arResult['EXTENDED'] = true;
			if(!empty($arParams['ENGINE_VOLUME_FROM']))
			{
				$arResult['SELECTED']['ENGINE_VOLUME_FROM'] = $arParams['ENGINE_VOLUME_FROM'];
				$query_arr['ENGINE_VOLUME_FROM'] = $arParams['ENGINE_VOLUME_FROM'];
			}
			if(!empty($arParams['ENGINE_VOLUME_TO']))
			{
				$arResult['SELECTED']['ENGINE_VOLUME_TO'] = $arParams['ENGINE_VOLUME_TO'];
				$query_arr['ENGINE_VOLUME_TO'] = $arParams['ENGINE_VOLUME_TO'];
			}
			$arResult['SCROLL'] = true;
		}

		//$pages_url = $pages_url."/";

		if(count($query_arr) > 0)
		{
			$query = http_build_query($query_arr);
		}
		//if(!empty($_SERVER['REDIRECT_QUERY_STRING']))
		//{
		//	$query = $_SERVER['REDIRECT_QUERY_STRING'];
		//}

		/*
		if(isset($arParams["sort"]))
		{
			$sort = $arParams["sort"];
		}

		if(isset($arParams["options"]))
		{
			$options = $arParams["options"];
		}
		else
		{
			//$options = false;
		}

		if(isset($arParams["filter"]))
		{
			$filter = array_merge($filter, $arParams["filter"]);
		}

		if(!empty($_REQUEST['filter']['city_id']))
		{
			$filter['PROPERTY_CITY'] = $_REQUEST['filter']['city_id'];
		}

		//if(!empty($_REQUEST['filter']['subway_id']))
		//{
			//$filter['PROPERTY_SUBWAY'] = $_REQUEST['filter']['subway_id'];
		//}

		if(is_array($_REQUEST['subways']))
		{
			$subs = Array(); foreach($_REQUEST['subways'] AS $s) { array_push($subs, $s); }
			$filter['PROPERTY_SUBWAY'] = $subs;
		}
		*/

		if($arParams['leasing_program'] && $arParams['leasing_program'] !== "")
		{
			$filter['PROPERTY_LEASING_PROGRAMS'] = $arParams["leasing_program"];
		}

		$res = CIBlockElement::GetList($sort, $filter, false, $options);

		while ($obElement = $res->GetNextElement())
		{
			$ar_res = $obElement->GetFields();
			$ar_res['PROPERTIES'] = $obElement->GetProperties();
			$ar_res['PROPERTIES']['BRAND']['RELATED'] = get_related(8, $ar_res['PROPERTIES']['BRAND']['VALUE']);
			$ar_res['PROPERTIES']['MODEL']['RELATED'] = get_related(9, $ar_res['PROPERTIES']['MODEL']['VALUE']);
			$ar_res['PROPERTIES']['MODIFICATION']['RELATED'] = get_related(10, $ar_res['PROPERTIES']['MODIFICATION']['VALUE']);

			$ar_res['CALCULATION'] = [];
			$ar_res['CALCULATION']['MONTHLY'] = number_format((($ar_res['PROPERTIES']['PRICE']['VALUE'] / 100) * 10), 0, ".", " ");
			$ar_res['CALCULATION']['SAVINGS_TAX'] = number_format((($ar_res['PROPERTIES']['PRICE']['VALUE'] / 100) * 4), 0, ".", " ");
			$ar_res['CALCULATION']['SAVINGS_VAT'] = number_format((($ar_res['PROPERTIES']['PRICE']['VALUE'] / 100) * 4), 0, ".", " ");

			//print "<!-- ".$ar_res['ID']."-->\n";
			//print "<!-- ";
			//print_r($ar_res['PROPERTIES']);
			//print "-->\n\n\n";

			/*
			if(isset($ar_res['PREVIEW_PICTURE']))
			{
				$ar_res['PREVIEW_PICTURE_SRC'] = get_image_path($ar_res['PREVIEW_PICTURE']);
			}

			if(!empty($ar_res['PROPERTIES']['PHOTO_ICON']['VALUE']))
			{
				$ar_res['PROPERTIES']['PHOTO_ICON']['SRC'] = get_image_path($ar_res['PROPERTIES']['PHOTO_ICON']['VALUE']);
			}

			if(!empty($ar_res['PROPERTIES']['CITY']['VALUE']))
			{
				$ar_res['PROPERTIES']['CITY']['RELATED'] = get_related_array(10, $ar_res['PROPERTIES']['CITY']['VALUE']);
			}

			if(!empty($ar_res['PROPERTIES']['SUBWAY']['VALUE']))
			{
				$ar_res['PROPERTIES']['SUBWAY']['RELATED'] = get_related_array(12, $ar_res['PROPERTIES']['SUBWAY']['VALUE']);
			}
			*/

			$arResult['ITEMS'][] = $ar_res;
		}

		if($_REQUEST['PAGEN_1'] > 1)
		{
			$arResult['SCROLL'] = true;
		}

		if($arParams["PAGINATION"] == "Y")
		{
			$arResult["NAV_STRING"] = $res->GetPageNavStringEx(
				$navComponentObject, 
				"", 
				"catalog", 
				"N", 
				null, 
				[
					 "section_code" => !empty($arParams['section_code']) ? $arParams['section_code'] : $pages_url, 
					 "query" => $query, 
				]
			);
		}

		$this->IncludeComponentTemplate();
	}
}