<?
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'))
	{
		$breadcrumbs = [];
		$canonical = [];

		$arResult = [
			'SELECTED' => [], 
			'TITLES' => [], 
			'ITEMS' => [], 
			'FILTER_ARRAY' => [], 
		];

		$query = null;
		$query_arr = [];

		$pages_url = $arParams['PATH'];

		//'=PROPERTY_BODY' => $bodies_ar_res['NAME']
		$sort = Array("SORT" => "ASC", "PROPERTY_PRICE" => "DESC");
		$filter = [ "ACTIVE" => "Y", "IBLOCK_ID" => IBLOCK_ID_CATALOG_CARS_NEW ];
		$options = [ "nPageSize" => !empty($arParams['COUNT_PER_PAGE']) ? $arParams['COUNT_PER_PAGE'] : 15 ];

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

		$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']);

			$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, ".", " ");

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

		$arResult['SECTION_CODE'] = !empty($arParams['section_code']) ? $arParams['section_code'] : $pages_url;
		$arResult['QUERY'] = $query;

		if(count($arResult['ITEMS']) > 0 && $arParams["PAGINATION"] == "Y")
		{
			$arResult["NAV_STRING"] = $res->GetPageNavStringEx(
				$navComponentObject, 
				"", 
				"catalog", 
				"N", 
				null, 
				[
					"section_code" => $arResult['SECTION_CODE'], 
					"query" => $arResult['QUERY'], 
				]
			);
		}

		$arResult['PAGE_CURRENT'] = $res->NavPageNomer;
		$arResult['PAGE_COUNT'] = $res->NavPageCount;

		if(isset($_REQUEST['ajax']))
		{
			$GLOBALS['APPLICATION']->RestartBuffer();

			$this->IncludeComponentTemplate();
			$items = ob_get_contents();
			ob_end_clean();

			header('Content-Type: application/json; charset=utf-8');

			$response = [ 
				'ITEMS' => json_encode(str_replace(["\t","\r","\n"], ["","",""], $items), JSON_UNESCAPED_SLASHES), 
				'PAGE_CURRENT' => $arResult['PAGE_CURRENT'], 
				'PAGE_COUNT' => $arResult['PAGE_COUNT'], 
				'PAGINATION' => json_encode(str_replace(["\t","\r","\n"], ["","",""], $arResult['NAV_STRING']), JSON_UNESCAPED_SLASHES), 
			];

			print json_encode($response, JSON_UNESCAPED_SLASHES);

			die();
		}
		else
		{
			$this->IncludeComponentTemplate();
		}
	}
}