<?
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');

$old_uids_json = file_get_contents("./uids_codes.json");
$old_uids = json_decode($old_uids_json, true);

$old = 0;
$new = 0;
$total = 0;

$car_search_res = CIBlockElement::GetList( [ "ID" => "ASC" ], [ "IBLOCK_ID" => IBLOCK_ID_CATALOG_CARS_NEW, ], false, [], [] );

$redirects = "";

while($car_search_ob = $car_search_res->GetNextElement())
{
	$car_existed = $car_search_ob->GetFields();
	$car_existed['PROPERTIES'] = $car_search_ob->GetProperties();
	$uid = $car_existed['PROPERTIES']['UID']['VALUE'];

	if(isset($old_uids[ $uid ]))
	{
		$old++;
		$redirects .= "RewriteRule ^car/". $old_uids[ $uid ] ."/$ https://www.evoleasing.ru/car/". $car_existed['CODE'] ."/ [R=301,NE,NC,L]\n";
	}
	else
	{
		$new++;
	}

	$total++;
}

print "old "+$old."\n";
print "new "+$new."\n";
print "total "+$total."\n";

file_put_contents("./car_redirects.txt", $redirects);

die();

?>