<?
//$_SERVER["DOCUMENT_ROOT"] = __DIR__;
define('STOP_STATISTICS', true);
define('NO_KEEP_STATISTIC', 'Y');
define('NO_AGENT_STATISTIC', 'Y');
define('DisableEventsCheck', true);
define('BX_SECURITY_SHOW_MESSAGE', true);
define('NOT_CHECK_PERMISSIONS', true);

$_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__, 1);
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php'); 
CModule::IncludeModule('iblock');

$arrContextOptions = [
	"ssl" => [
		"verify_peer"=>false,
		"verify_peer_name"=>false,
	],
];

$bitrix_cars = [];
$api_cars = [];

$all_cars_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => 1, ], false, false, [] );
while($all_cars_search_ob = $all_cars_search_res->GetNextElement())
{
	$car_search = $all_cars_search_ob->GetFields();
	$car_search['PROPERTIES'] = $all_cars_search_ob->GetProperties();

	$bitrix_cars[ $car_search['PROPERTIES']['UID']['VALUE'] ] = $car_search['CODE'];
}

$api_modifications_url = API_HOST."/site/Vehicle/GetModifications";
$content = file_get_contents($api_modifications_url, false, stream_context_create($arrContextOptions));
print $api_modifications_url."\n\n";

file_put_contents("./api_modifications.json", $content);

$json = json_decode($content, true);

foreach($json AS $car)
{
	$name = "".$car['brand']['name']." ".$car['model']['name']." ".$car['modification']."";
	$title = $name." ".$car['modificationYear']."";
	$code = CUtil::translit( str_replace(["&quot;", " ", "+"], ['"', "-", "-plus-"], $title), "ru", [ "max_len" => 255, "change_case" => "L", "replace_space" => "-", "replace_other" => "-", "safe_chars" => "-" ] );

	$api_cars[ $car['uid'] ] = $code;
}

print "bitrix cars: ".count($bitrix_cars)."\n";
print "ilsa cars: ".count($api_cars)."\n";

foreach($api_cars AS $uid => $code)
{
	if(isset($bitrix_cars[ $uid ]))
	{
		if($code != $bitrix_cars[ $uid ])
		{
			//print $uid."\n".$code."\n".$bitrix_cars[ $uid ]."\n\n";
		}
	}
	else
	{
		print $uid." - ".$code."\n";
	}
}

?>