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

		$query = null;
		$query_arr = [];

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

		$filter['PROPERTY_SPECIAL_OFFER_VALUE'] = "Да";

		if(!empty($arParams['BRAND_ID']))
		{
			$filter['PROPERTY_BRAND'] = $arParams['BRAND_ID'];
		}

		if(isset($arParams['FILTER_ARRAY']))
		{
			$filter = array_merge($filter, $arParams['FILTER_ARRAY']);
		}

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

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

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

		$this->IncludeComponentTemplate();
	}
}