<?
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("IBLOCK_ID" => IBLOCK_ID_CATALOG_CARS_USED, "CODE" => $arParams['OFFER']);
		$options = Array("nPageSize" => 1);

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

			$arParams['car_price'] = ($ar_res['PROPERTIES']['PRICE_WITH_DISCOUNT']['VALUE'] > 0 ? $ar_res['PROPERTIES']['PRICE_WITH_DISCOUNT']['VALUE'] : $ar_res['PROPERTIES']['PRICE']['VALUE']);

			$ar_res['CALCULATION'] = (array) json_decode(file_get_contents("https://evoleasing.ru/api/calculation/"
			."?car_price=".$arParams['car_price']
			."&initial_payment=".$arParams['initial_payment']
			."&lease_period=".$arParams['lease_period']
			."&redemption_payment=".$arParams['redemption_payment'].""));

			$arResult = $ar_res;

			$arResult['PHOTOS_1080'] = [];
			$arResult['PHOTOS_480'] = [];
			$arResult['PHOTOS_120'] = [];

			foreach($ar_res['PROPERTIES']['PHOTOS_1080']['VALUE'] AS $photo_id)
			{
				array_push($arResult['PHOTOS_1080'], CFile::GetPath($photo_id));
			}

			foreach($ar_res['PROPERTIES']['PHOTOS_480']['VALUE'] AS $photo_id)
			{
				array_push($arResult['PHOTOS_480'], CFile::GetPath($photo_id));
			}

			foreach($ar_res['PROPERTIES']['PHOTOS_120']['VALUE'] AS $photo_id)
			{
				array_push($arResult['PHOTOS_120'], CFile::GetPath($photo_id));
			}

			//$APPLICATION->AddHeadString('<link rel="canonical" href="'.$ar_res['CANONICAL_PAGE_URL'].'"/>');
			$APPLICATION->AddHeadString('<link rel="canonical" href="https://'.SITE_SERVER_NAME.'/izt/'.$ar_res['CODE'].'/"/>');
		}

		if(empty($ar_res['NAME'])) { define('ERROR_404', 'Y'); }
		else
		{
			$APPLICATION->AddChainItem($ar_res['PROPERTIES']['BRAND']['RELATED']['NAME'], "/used/".$ar_res['PROPERTIES']['BRAND']['RELATED']['CODE']."/");
			$APPLICATION->AddChainItem($ar_res['PROPERTIES']['MODEL']['RELATED']['NAME'], "/used/".$ar_res['PROPERTIES']['BRAND']['RELATED']['CODE']."/".$ar_res['PROPERTIES']['MODEL']['RELATED']['CODE']."/");
			$APPLICATION->AddChainItem($ar_res["NAME"]);

			$ipropValues = new \Bitrix\Iblock\InheritedProperty\ElementValues($ar_res['IBLOCK_ID'], $ar_res["ID"]);
			$meta = $ipropValues->getValues();
			$APPLICATION->SetPageProperty("title", $meta['ELEMENT_META_TITLE']);
			$APPLICATION->SetPageProperty("keywords", $meta['ELEMENT_META_KEYWORDS']);
			$APPLICATION->SetPageProperty("description", $meta['ELEMENT_META_DESCRIPTION']);

			$APPLICATION->SetPageProperty("robots", "noindex, nofollow");
		}

		$this->IncludeComponentTemplate();
	}
}