<?
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;

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

		$brands = [];
		$brands_res = CIBlockElement::GetList([ "NAME" => "ASC" ], [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_BRANDS, ], false, []);
		while ($brands_ob_element = $brands_res->GetNextElement())
		{
			$brands_ar_res = $brands_ob_element->GetFields();
			$brands_ar_res['PROPERTIES'] = $brands_ob_element->GetProperties();
			$brands[$brands_ar_res['CODE']] = [
				"NAME" => $brands_ar_res['NAME'], 
				"CODE" => $brands_ar_res['CODE'], 
				"UID" => $brands_ar_res['PROPERTIES']['UID']['VALUE'], 
			];
		}

		//print "<!-- BRANDS >>>>>";
		//print_r($brands);
		//print "-->";

		$arResult['TITLE'] = $arParams['RECOMMENDED'] ? "Каталог автомобилей ".$brands[$arParams['BRAND']]['NAME'] : "";

		$models_filter = [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_MODELS, "PROPERTY_BRAND_UID" => $brands[$arParams['BRAND']]['UID'] ];
		if($arParams['REGIONS'])
		{
			//$models_filter["PROPERTY_PROMO_VALUE"] = "Да";
		}

		$models_res = CIBlockElement::GetList([ "NAME" => "ASC" ], $models_filter, false, []);
		while ($models_ob_element = $models_res->GetNextElement())
		{
			$models_ar_res = $models_ob_element->GetFields();
			$models_ar_res['PROPERTIES'] = $models_ob_element->GetProperties();

			if(!empty($models_ar_res['PREVIEW_PICTURE']))
			{
				$models_ar_res['IMAGE_PREPARED'] = true;
				$models_ar_res['PREVIEW_PICTURE'] = CFile::GetPath($models_ar_res['PREVIEW_PICTURE']);
			}
			else
			{
				$cars_res = CIBlockElement::GetList([ "NAME" => "ASC" ], [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_CATALOG_CARS_NEW, "PROPERTY_MODEL" => $models_ar_res['ID'] ], false, [ "nPageSize" => 1 ]);
				while ($cars_ob_element = $cars_res->GetNextElement())
				{
					$cars_ar_res = $cars_ob_element->GetFields();
					$cars_ar_res['PROPERTIES'] = $cars_ob_element->GetProperties();

					$models_ar_res['IMAGE_PREPARED'] = false;
					$models_ar_res['PREVIEW_PICTURE'] = $cars_ar_res['PROPERTIES']['IMAGE_URL']['VALUE'];
				}
			}

			$models_ar_res['URL'] = $arParams['PATH'].$brands[$arParams['BRAND']]['CODE']."/".$models_ar_res['CODE']."/";

			if($arParams['REGIONS'])
			{
				if($models_ar_res['PROPERTIES']['REGIONS']['VALUE'] !== "Да")
				{
					$models_ar_res['URL'] = "/catalog/".$brands[$arParams['BRAND']]['CODE']."/".$models_ar_res['CODE']."/";
				}
			}

			$arResult['MODELS'][] = $models_ar_res;
		}

		$this->IncludeComponentTemplate();
	}
//}