<?
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(CModule::IncludeModule('iblock'))
{
	$arResult = [
		'ITEMS' => [], 
		'LINK' => "/catalog/", 
	];

	$query = null;
	$query_arr = [];

	$sort = Array( "RAND" => "ASC" );
	$filter = Array( "ACTIVE" => "Y", "IBLOCK_ID" => 1, "!ID" => $arParams['EXCEPT'] );
	$options = Array( "nPageSize" => 6 );

	if(!empty($arParams['BRAND_ID']))
	{
		$filter['PROPERTY_BRAND'] = $arParams['BRAND_ID'];
		$arResult['LINK'] .= $arParams['BRAND_CODE']."/";
	}

	if(!empty($arParams['MODEL_ID']))
	{
		$filter['PROPERTY_MODEL'] = $arParams['MODEL_ID'];
		$arResult['LINK'] .= $arParams['MODEL_CODE']."/";
	}

	$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(IBLOCK_ID_BRANDS, $ar_res['PROPERTIES']['BRAND']['VALUE']);
		$ar_res['PROPERTIES']['MODEL']['RELATED'] = get_related(IBLOCK_ID_MODELS, $ar_res['PROPERTIES']['MODEL']['VALUE']);
		$ar_res['PROPERTIES']['MODIFICATION']['RELATED'] = get_related(IBLOCK_ID_MODIFICATIONS, $ar_res['PROPERTIES']['MODIFICATION']['VALUE']);

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

	$this->IncludeComponentTemplate();
}