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

require_once($_SERVER["DOCUMENT_ROOT"].'/bitrix/php_interface/phpqrcode/qrlib.php');


$object = json_decode(file_get_contents("https://evoleasing.ru/api/vizitka/?guid=".$arParams['guid']));

	//define("LOG_FILENAME", $_SERVER["DOCUMENT_ROOT"]."/vizitka/tmp/log2.txt");
	//AddMessage2Log("object => ".print_r($object, true)."\narParams => ".print_r($arParams, true), "my_module_id");
		
	//$ourData = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/vizitka/data.json");
	//$object = json_decode($ourData);
		
	//Очищаем временную директорию от файлоы старше 2 дней
	$tmpfolders = $_SERVER["DOCUMENT_ROOT"].'/vizitka/tmp/';
	$fileSystemIterator = new FilesystemIterator($tmpfolders);
	$now = time();
	foreach ($fileSystemIterator as $file) {
		if ($now - $file->getCTime() >= 60 * 60 * 24 * 2) // 2 days 
			unlink($tmpfolders.$file->getFilename());
	}

	//Разбираем данные из json
	//if ($arParams["guid"] & $object->firstName) {
	if ($object->firstName) {
		$arParams["name"] = $object->firstName;
		$arParams["family"] = $object->secondName;
		$arParams["post"] = $object->post;
		$arParams["dept"] = $object->department;
		$arParams["company"] = "Эволюция Автолизинга";
		$arParams["mob"] = $object->mobilePhone;
		$arParams["mobcl"] = preg_replace('/[^0-9]/', '', $arParams["mob"]);
		$arParams["tel"] = $object->workPhone;
		$arParams["telcl"] = preg_replace('/[^0-9]/', '', $arParams["tel"]);
		$arParams["teladd"] = $object->workPhoneCode;
		$arParams["mail"] = $object->email;
		$arParams["address"] = $object->address;
		$arParams["whatsapp"] = $object->whatsAppOn;
		$arParams["telegram"] = $object->telegram;
		$arParams["www"] = "https://www.evoleasing.ru";
		$arParams["image"] = $object->image;
	
//VCF для QR кода
$qrtext	= 'BEGIN:VCARD
VERSION:4.0
N;CHARSET=UTF-8:'.$arParams["family"].';'.$arParams["name"].';;;
FN;CHARSET=UTF-8:'.$arParams["name"].' '.$arParams["family"].'
TITLE;CHARSET=UTF-8:'.$arParams["post"].'
EMAIL:'.$arParams["mail"].'
ORG;CHARSET=UTF-8:'.$arParams["company"].';'.$arParams["dept"].'
TEL;type=CELL:'.$arParams["mob"].'
URL;type=PREF:'.$arParams["www"].'
LANG:ru-RU
END:VCARD';
	
$qrimg = QRcode::png($qrtext, $_SERVER["DOCUMENT_ROOT"].'/vizitka/tmp/'.$arParams["guid"].'.png', 'L', 3, 2);
//$im = imagecreatefrompng($_SERVER["DOCUMENT_ROOT"].'/vizitka/tmp/'.$arParams["guid"].'.png');
	
//Формируем vcf файл визитки для телефонов
$arParams["vcarddata"] = 'BEGIN:VCARD
VERSION:4.0
N;CHARSET=UTF-8:'.$arParams["family"].';'.$arParams["name"].';;;
FN;CHARSET=UTF-8:'.$arParams["name"].' '.$arParams["family"].'
TITLE;CHARSET=UTF-8:'.$arParams["post"].'
EMAIL:'.$arParams["mail"].'
ORG;CHARSET=UTF-8:'.$arParams["company"].';'.$arParams["dept"].'
TEL;type=CELL:'.$arParams["mob"].'
URL;type=PREF:'.$arParams["www"].'
PHOTO;ENCODING=BASE64;TYPE=JPEG:'.$arParams["image"].'
LANG:ru-RU
END:VCARD';
$arParams["vcarddata"] = mb_convert_encoding($arParams["vcarddata"], "UTF-8");
	
	$arParams["vcffile"] = './tmp/'.$arParams["guid"].'.vcf';
	$fp = fopen($arParams["vcffile"], "w");
	fwrite($fp, $arParams["vcarddata"]);
	fclose($fp);
	
//Формируем vcf файл визитки для компьютера
$arParams["vcarddatawin"] = 'BEGIN:VCARD
VERSION:4.0
N;CHARSET=windows-1252:'.$arParams["family"].';'.$arParams["name"].';;;
FN;CHARSET=windows-1252:'.$arParams["name"].' '.$arParams["family"].'
TITLE;CHARSET=windows-1252:'.$arParams["post"].'
EMAIL:'.$arParams["mail"].'
ORG;CHARSET=windows-1252:'.$arParams["company"].';'.$arParams["dept"].'
TEL;type=CELL:'.$arParams["mob"].'
URL;type=PREF:'.$arParams["www"].'
PHOTO;ENCODING=BASE64;TYPE=JPEG:'.$arParams["image"] .'
LANG:ru-RU
END:VCARD';
	
$arParams["vcarddatawin"] = iconv("utf-8", "windows-1251", $arParams["vcarddatawin"]);
	
	$arParams["vcffilewin"] = './tmp/'.$arParams["guid"].'-win.vcf';
	$fp = fopen($arParams["vcffilewin"], "w");
	fwrite($fp, $arParams["vcarddatawin"]);
	fclose($fp);
	
}
$this->IncludeComponentTemplate();