<?
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'))
	{
		$breadcrumbs = [];
		$canonical = [];

		$arResult = [
			'SELECTED' => [], 
			'TITLES' => [], 
			'ITEMS' => [], 
			'FILTER_ARRAY' => [], 
		];

		$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" => IBLOCK_ID_CATALOG_CARS_NEW);
		$options = Array("nPageSize" => !empty($arParams['COUNT_PER_PAGE']) ? $arParams['COUNT_PER_PAGE'] : 15);

		$pages_url = $arParams['PATH'];

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

		$arResult['SHOW_BRANDS_LIST'] = true;
		$arResult['SHOW_MODELS_LIST'] = false;

		if(!empty($arResult['TITLES']['BRAND']))
		{
			if(!empty($arResult['TITLES']['MODEL']))
			{
				$APPLICATION->SetPageProperty("title", $arResult['TITLES']['BRAND']." ".$arResult['TITLES']['MODEL']." в лизинг для юридических лиц и ИП в Москве - купить новый автомобиль ".$arResult['TITLES']['TRANSCRIPTION']." ".$arResult['TITLES']['MODEL']." в лизинг | Эволюция");
				$APPLICATION->SetPageProperty("description", "Новые автомобили ".$arResult['TITLES']['BRAND']." ".$arResult['TITLES']['MODEL']." в лизинг для юридических лиц и ИП в Москве и в других регионах России. Вы можете купить ".$arResult['TITLES']['BRAND']." ".$arResult['TITLES']['MODEL']." в лизинг от официальных дилеров, автосалонов на выгодных условиях со скидкой. Телефон горячей линии ".CENTRAL_PHONE_FORMATTED." - звонок по России бесплатный.");	
			}
			else
			{
				$APPLICATION->SetPageProperty("title", $arResult['TITLES']['BRAND']." в лизинг для юридических лиц и ИП в Москве - купить новый автомобиль ".$arResult['TITLES']['TRANSCRIPTION']." в лизинг | Эволюция");
				$APPLICATION->SetPageProperty("description", "Новые автомобили ".$arResult['TITLES']['BRAND']." в лизинг для юридических лиц и ИП в Москве и в других регионах России. Вы можете купить ".$arResult['TITLES']['BRAND']." в лизинг от официальных дилеров, автосалонов на выгодных условиях со скидкой. Телефон горячей линии ".CENTRAL_PHONE_FORMATTED." - звонок по России бесплатный.");	
			}
		}

		$arResult['SECTION_CODE'] = !empty($arParams['section_code']) ? $arParams['section_code'] : $pages_url;
		$arResult['QUERY'] = $query;

		$arResult['BRANDS_IDS'] = [];

		if(!is_null($arParams['REGION']))
		{
			//IBLOCK_ID_CITIES
			$res_city = CIBlockElement::GetList([], [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_CITIES, "CODE" => $arParams['REGION'] ], false, []);

			while ($city_element = $res_city->GetNextElement())
			{
				$ar_city = $city_element->GetFields();
				$ar_city['PROPERTIES'] = $city_element->GetProperties();

				$ar_city['PROPERTIES']['OFFICES']['RELATED'] = get_related_array(IBLOCK_ID_CONTACTS, $ar_city['PROPERTIES']['OFFICES']['VALUE']);

				if(isset($ar_city['PROPERTIES']['OFFICES']['RELATED'][0]))
				{
					$ar_city['LEADER_CRM_ID'] = $ar_city['PROPERTIES']['LEADER_CRM_ID']['VALUE'];
				}

				$arResult['CITY'] = $ar_city;

				array_unshift($breadcrumbs, [ 
					"NAME" => $ar_city['NAME'], 
					"URL" => "https://".SITE_SERVER_NAME."/regions/".$ar_city['CODE']."/", 
				]);

				array_unshift($canonical, $ar_city['CODE']);

				$engines_element_res = CIBlockElement::GetList(["NAME" => "ASC"], ["IBLOCK_ID" => IBLOCK_ID_ENGINE_FUELS, "CODE" => $arParams['ENGINE_FUEL_SLUG']], false, []);
				while ($engines_element_ob_element = $engines_element_res->GetNextElement())
				{
					$engines_ar_res = $engines_element_ob_element->GetFields();
					$engines_ar_res['PROPERTIES'] = $engines_element_ob_element->GetProperties();
					$arResult['ENGINE_FUEL'] = $engines_ar_res['NAME'];
					$arResult['SELECTED']['ENGINE_FUEL'] = $engines_ar_res['NAME'];

					array_push($breadcrumbs, [ 
						"NAME" => mb_ucfirst($engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE']), 
						"URL" => "https://".SITE_SERVER_NAME."/regions/dvigatel-".$arParams['CODE']."/", 
					]);

					$APPLICATION->AddHeadString('<link rel="canonical" href="https://'.SITE_SERVER_NAME.'/regions/'.$arParams['REGION'].'/dvigatel-'.$arParams['ENGINE_FUEL_SLUG'].'/"/>');
					$APPLICATION->SetPageProperty("title", mb_ucfirst($engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE'])." в ".$ar_city['PROPERTIES']['CASE']['VALUE']." в лизинг для юридических лиц и ИП | Эволюция");
					$APPLICATION->SetPageProperty("description", "Новые ".$engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE']." в ".$ar_city['PROPERTIES']['CASE']['VALUE']." в лизинг для юридических лиц и ИП. Вы можете купить ".$engines_ar_res['PROPERTIES']['SINGULAR_NAME']['VALUE']." в ".$ar_city['PROPERTIES']['CASE']['VALUE']." в лизинг от официальных дилеров, автосалонов на выгодных условиях со скидкой. Телефон горячей линии 8 (800) 333-75-75 - звонок по России бесплатный.");
					$APPLICATION->SetPageProperty("keywords", mb_ucfirst($engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE'])." в лизинг в ".$ar_city['PROPERTIES']['CASE']['VALUE'].", купить ".$engines_ar_res['PROPERTIES']['SINGULAR_NAME']['VALUE']." в ".$ar_city['PROPERTIES']['CASE']['VALUE']);

					if($engines_ar_res['PROPERTIES']['META_SEO_INDEXED_REGIONS']['VALUE_XML_ID'] !== "YES")
					{
						$APPLICATION->SetPageProperty("robots", "noindex");
					}

					$arResult['TITLE'] = mb_ucfirst($engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE'])." в ".$ar_city['PROPERTIES']['CASE']['VALUE']." в лизинг";
					$arResult['SUBTITLE'] = " для юридических лиц и ИП";

					$arResult['CARS_CATALOG_TITLE'] = "Каталог ".$engines_ar_res["PROPERTIES"]["GENITIVE_NAME"]["VALUE"];

					$catalog_engines_res = CIBlockElement::GetPropertyValues(IBLOCK_ID_CATALOG_CARS_NEW, [ 'ACTIVE' => 'Y', '=PROPERTY_ENGINE_FUEL' => $engines_ar_res['NAME'] ], false, [ 'ID' => [ PROPERTY_ID_CATALOG_CARS_NEW_BRAND ] ]);
					while ($catalog_engines_row = $catalog_engines_res->Fetch())
					{
						array_push($arResult['BRANDS_IDS'], $catalog_engines_row[ PROPERTY_ID_CATALOG_CARS_NEW_BRAND ]);
					}
					$arResult['BRANDS_IDS'] = array_unique($arResult['BRANDS_IDS']);
				}
			}

			if(!isset($ar_city))
			{
				define('ERROR_404', 'Y');
			}
		}
		else
		{
			$engines_element_res = CIBlockElement::GetList(["NAME" => "ASC"], ["IBLOCK_ID" => IBLOCK_ID_ENGINE_FUELS, "CODE" => $arParams['ENGINE_FUEL_SLUG']], false, []);
			while ($engines_element_ob_element = $engines_element_res->GetNextElement())
			{
				$engines_ar_res = $engines_element_ob_element->GetFields();
				$engines_ar_res['PROPERTIES'] = $engines_element_ob_element->GetProperties();
				$arResult['ENGINE_FUEL'] = $engines_ar_res['NAME'];
				$arResult['SELECTED']['ENGINE_FUEL'] = $engines_ar_res['NAME'];

				$APPLICATION->AddHeadString('<link rel="canonical" href="https://'.SITE_SERVER_NAME.'/catalog/dvigatel-'.$arParams['ENGINE_FUEL_SLUG'].'/"/>');
				$APPLICATION->SetPageProperty("title", mb_ucfirst($engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE'])." в лизинг для юридических лиц и ИП в Москве - купить новый ".$engines_ar_res['PROPERTIES']['SINGULAR_NAME']['VALUE']." в лизинг | Эволюция");
				$APPLICATION->SetPageProperty("description", "Новые ".$engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE']." в лизинг для юридических лиц и ИП в Москве и в других регионах России. Вы можете купить ".$engines_ar_res['PROPERTIES']['SINGULAR_NAME']['VALUE']." в лизинг от официальных дилеров, автосалонов на выгодных условиях со скидкой. Телефон горячей линии 8 (800) 333-75-75 - звонок по России бесплатный.");
				$APPLICATION->SetPageProperty("keywords", mb_ucfirst($engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE'])." в лизинг, купить ".$engines_ar_res['PROPERTIES']['SINGULAR_NAME']['VALUE']." в лизинг");
				$APPLICATION->AddChainItem(mb_ucfirst($engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE']), "https://".SITE_SERVER_NAME."/catalog/dvigatel-".$arParams['ENGINE_FUEL_SLUG']."/");

				$arResult['TITLE'] = mb_ucfirst($engines_ar_res['PROPERTIES']['PLURAL_NAME']['VALUE'])." в лизинг";
				$arResult['SUBTITLE'] = " для юридических лиц и ИП";

				$arResult['CARS_CATALOG_TITLE'] = "Каталог ".$engines_ar_res["PROPERTIES"]["GENITIVE_NAME"]["VALUE"];

				$catalog_engines_res = CIBlockElement::GetPropertyValues(IBLOCK_ID_CATALOG_CARS_NEW, [ 'ACTIVE' => 'Y', '=PROPERTY_ENGINE_FUEL' => $engines_ar_res['NAME'] ], false, [ 'ID' => [ PROPERTY_ID_CATALOG_CARS_NEW_BRAND ] ]);
				while ($catalog_engines_row = $catalog_engines_res->Fetch())
				{
					array_push($arResult['BRANDS_IDS'], $catalog_engines_row[ PROPERTY_ID_CATALOG_CARS_NEW_BRAND ]);
				}

				$arResult['BRANDS_IDS'] = array_unique($arResult['BRANDS_IDS']);
			}

			if(!isset($engines_ar_res))
			{
				define('ERROR_404', 'Y');
			}
		}

		if($arResult['MODEL_DISABLED'])
		{
			$arResult['MODEL'] = null;
			$arResult['SELECTED']['MODEL'] = null;
			$arResult['SELECTED']['MODEL_ID'] = null;
			$arResult['SELECTED']['MODEL_UID'] = null;
			$arResult['SHOW_SPECIAL_OFFERS'] = false;
		}

		if($arResult['BRAND_DISABLED'])
		{
			$arResult['BRAND'] = null;
			$arResult['SELECTED']['BRAND'] = null;
			$arResult['SELECTED']['BRAND_ID'] = null;
			$arResult['SELECTED']['BRAND_UID'] = null;
			$arResult['SHOW_SPECIAL_OFFERS'] = false;
		}

		$arResult['SHOW_SPECIAL_OFFERS'] = true;
		$arResult['SHOW_PROMO_BRANDS'] = false;

		foreach($breadcrumbs AS $breadcrumb)
		{
			$APPLICATION->AddChainItem($breadcrumb['NAME'], $breadcrumb['URL']);
		}

		$arResult['SHOW_PROMO_MODELS'] = true;

		$this->IncludeComponentTemplate();
	}
}