<?
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 = [
			'MODELS' => [], 
		];

		$models_ids = [];
		$iterator = CIBlockElement::GetPropertyValues( IBLOCK_ID_CATALOG_CARS_NEW, $arParams['PROPERTIES_FILTER_ARRAY'], true, [ 'ID' => [ PROPERTY_ID_CATALOG_CARS_NEW_MODEL ] ] );
		while ($row = $iterator->Fetch())
		{
			array_push($models_ids, $row[ PROPERTY_ID_CATALOG_CARS_NEW_MODEL ]);
		}

		$models_ids = array_values(array_unique($models_ids));

		$models_sort = [ "SORT" => "ASC" ];
		$models_filter = [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_MODELS, "ID" => $models_ids, ];
		$models_filter = array_merge($models_filter, $arParams['PROPERTIES_FILTER_ARRAY_MODELS']);

		$models_res = CIBlockElement::GetList($models_sort, $models_filter, false, []);
		while ($models_element = $models_res->GetNextElement())
		{
			$models_ar_res = $models_element->GetFields();
			$models_ar_res['PROPERTIES'] = $models_element->GetProperties();
			$models_ar_res['ITEMS'] = [];

			$brands_res = CIBlockElement::GetList([ "SORT" => "ASC" ], [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_BRANDS, "PROPERTY_UID" => $models_ar_res['PROPERTIES']['BRAND_UID']['VALUE'], ], false, []);
			while ($brands_element = $brands_res->GetNextElement())
			{
				$brands_ar_res = $brands_element->GetFields();
				$models_ar_res['BRAND_NAME'] = $brands_ar_res['NAME'];
				$models_ar_res['BRAND_CODE'] = $brands_ar_res['CODE'];
			}

			$cars_sort = [ "NAME" => "ASC" ];
			$cars_filter = [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_CATALOG_CARS_NEW, "PROPERTY_MODEL" => $models_ar_res['ID'], ];
			$cars_filter = array_merge($cars_filter, $arParams['PROPERTIES_FILTER_ARRAY']);

			$position_res = CIBlockElement::GetList($cars_sort, $cars_filter, false, [ "nPageSize" => 3 ]);

			while ($position_element = $position_res->GetNextElement())
			{
				$ar_res = $position_element->GetFields();
				$ar_res['PROPERTIES'] = $position_element->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']);

				$ar_res['CALCULATION'] = [];
				$ar_res['CALCULATION']['MONTHLY'] = number_format(($ar_res['PROPERTIES']['PRICE']['VALUE'] / CALCULATION_COEFFICIENT), 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, ".", " ");

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

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

		/*
		$models_sort = [ "SORT" => "ASC" ];
		$models_filter = [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_MODELS, ];
		$models_filter = array_merge($models_filter, $arParams['FILTER']);

		$models_res = CIBlockElement::GetList($sort, $filter, false, []);
		while ($models_element = $models_res->GetNextElement())
		{
			$models_ar_res = $models_element->GetFields();
			$models_ar_res['PROPERTIES'] = $models_element->GetProperties();
			$models_ar_res['ITEMS'] = [];

			if(!isset($arParams['BRAND_CODE']))
			{
				$brands_res = CIBlockElement::GetList([ "SORT" => "ASC" ], [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_BRANDS, "CODE" => $models_ar_res['PROPERTIES']['BRAND_UID']['VALUE'], ], false, [ "nPageSize" => 1 ]);
				while ($brands_element = $brands_res->GetNextElement())
				{
					$brands_ar_res = $brands_element->GetFields();
					$arParams['BRAND_CODE'] = $brands_ar_res['CODE'];
				}
			}

			$cars_sort = [ "NAME" => "ASC" ];
			$cars_filter = [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_CATALOG_CARS_NEW, "PROPERTY_MODEL" => $models_ar_res['ID'], ];

			$position_res = CIBlockElement::GetList($cars_sort, $cars_filter, false, [ "nPageSize" => 3 ]);

			while ($position_element = $position_res->GetNextElement())
			{
				$ar_res = $position_element->GetFields();
				$ar_res['PROPERTIES'] = $position_element->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']);

				$ar_res['CALCULATION'] = [];
				$ar_res['CALCULATION']['MONTHLY'] = number_format(($ar_res['PROPERTIES']['PRICE']['VALUE'] / CALCULATION_COEFFICIENT), 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, ".", " ");

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

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

		$this->IncludeComponentTemplate();
	}
}