<?
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 = [];
		$sort = Array("SORT" => "ASC", "NAME" => "ASC");
		$filter = Array("ACTIVE" => "Y", "IBLOCK_ID" => 4);
		$options = Array("nPageSize" => 1000);

		$res = CIBlockElement::GetList($sort, $filter, false, $options);
		$arResult = [
			'BRANDS' => [], 
			'SELECTED' => $_REQUEST['brand'], 
		];

		while ($obElement = $res->GetNextElement())
		{
			$ar_res = $obElement->GetFields();
			$ar_res['PROPERTIES'] = $obElement->GetProperties();
			if($ar_res['PROPERTIES']['BRAND']['VALUE'])
			{
				$ar_res['PROPERTIES']['BRAND']['RELATED'] = get_related(8, $ar_res['PROPERTIES']['BRAND']['VALUE']);
				$arResult['BRANDS'][$ar_res['PROPERTIES']['BRAND']['RELATED']['NAME']] = $ar_res['PROPERTIES']['BRAND']['RELATED']['CODE'];
			}
			ksort($arResult['BRANDS']);
		}

		$this->IncludeComponentTemplate();
	}
}