<?php if(!defined('FastCore')){exit('Oops!');}

$opt['title'] = 'Make a deposit';

# Способ платежа
$py = $pg->segment[2] ?? NULL;

if ($user['ban'] == 1){
    exit('Your account has been blocked for violating the rules');
}

# Выбран способ оплаты
if ($py) {

$selectPS = mb_strtoupper($py);
$systemPay = $db->query('SELECT * FROM db_paysystem WHERE name = ? LIMIT 1',$selectPS)->fetchArray();
$minDep = $systemPay['mindep'];
$currencyPS = $systemPay['currency'];
$psTitle = $systemPay['title'];

# Заносим в БД
$db->query("INSERT INTO db_insert (uid, login, sum, sum_x, sys, `add`, role, type, status) VALUES ('$uid','$login','$minDep','$minDep','$currencyPS', '".time()."','1','2','0')");
$order_idd = $db->LastInsert();

$params = [
	"amount" => $minDep,
	"system" => $systemPay['name'],
	"currency" =>  $currencyPS,
	"order_id" => $order_idd,
	"comment" => "Insert by id-" .$order_idd,
];

$calculate = $systemPay['pairs'] * $minDep;

$test = false; // Режим тестирования

$paykassa = new PaykassaSCI($config->pkm_id, $config->pkm_pass,$test);


$res = $paykassa->createAddress(
	$params["system"],
	$params["currency"],
	$params["order_id"],
	$params["comment"]
);

if ($res['error']) {
	echo $res['message'];
	// ошибка когда что-то не так
} else {
	if (false === $test) {
		echo '<h3 class="text-center">MAKE A DEPOSIT METHOD - <span style="color: #f76c82;">'.$psTitle.'</span></h3><div class="row m-2">';
		$invoice_id = $res['data']['invoice'];

		$address = $res["data"]["wallet"];
		$tag = $res["data"]["tag"];
		$tag_name = $res["data"]["tag_name"];
		$is_tag = $res["data"]["is_tag"];

		$system = $res["data"]["system"];
		$currency = $res["data"]["currency"];

		$display = sprintf("%s", $address);
		if ($is_tag) {
			$display = sprintf("address %s %s: %s", $address, mb_convert_case($tag_name, MB_CASE_TITLE), $tag);
		}

		if (null === $params["amount"]) {
			echo sprintf(
				'<b>Send a money to the %s (%s) address</b>
				<div class="input-group input-group-lg mb-2">
					<input type="text" class="form-control notranslate" placeholder="wallet" value="%s" name="wall" id="purse" onclick="copier();">
					<span class="input-group-text copy" onclick="copier();"><i class="fa fa-copy"></i></span>
				</div>
				',
				$system,
				$currency,
				htmlspecialchars($display,ENT_QUOTES, "UTF-8")
			);
			
		} else {
			echo sprintf(
				'<div class="col-lg-8"><div class="card mt-3">
<div class="p-3">
<b>Send a money to the %s address:</b>
				
				<div class="input-group input-group-lg mb-2">
					<input type="text" class="form-control notranslate" placeholder="wallet" value="%s" name="wall" id="purse" onclick="copier();">
					<span class="input-group-text copy" onclick="copier();"><i class="fa fa-copy"></i></span>
				</div>
<hr>
<p>Minimal deposit: <b>%s %s</b> </p>
<p>Minimum in site currency: <b>%s TRX</b> </p>

</div>
</div></div>',
				$system,
				htmlspecialchars($display, ENT_QUOTES, "UTF-8"),
				$params["amount"],
				$currency,
				$calculate
			);
		}


		//Creating QR
		$qr_request = $paykassa->getQrLink($res['data'], $params["amount"]);
		if (!$qr_request["error"]) {
			echo sprintf(
				'<div class="col-lg-4"><br><br>QR Code:<br><img alt="" src="http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=%s"></div>',
				$qr_request["data"]["link"]
			);
		}
		echo '</div>';
	} else {
		echo sprintf('Test link: <a target="_blank" href="%s">Open link</a>', $res["data"]["url"]);
	}
}
?>
<style>
.copy {
  text-transform: uppercase;
  cursor: pointer;
}
.copy:hover {
	background-color: #00a3a3;
}
</style>
<script>
function copier() {
    $("#purse").select();
    document.execCommand('copy');
	document.getElementById('purse').style.backgroundColor = '#2e272c';
	document.getElementById('purse').style.color = '#f34';
}
</script>
<?php 
} else {
?>

<style>
	a.card {
		text-decoration: none !important;
		border-radius: 1em;
		border: 2px solid #dcdcf7 !important;
	}
	a.card h4 {
		padding: 10px 0;
		color: #445;padding-bottom: 5px;
		font-size: 20px;
	}
	a.card:hover {
	color: #f76c82 !important;
		border: 2px solid #f76c82 !important;
	background: #fff6f9;
	}
	a.card .btn-light {
	color: #f76c82 !important;
	background: #fde6e9;
	font-size: 14px;font-weight: 500;padding: 1px 8px;border-radius: 12px;
	border: 1px solid #fdd2d9 !important;
	}
</style>
<h3 class="text-center text-uppercase">Method top-up advertising</h3>
<div class="row m-1">
<?php

$pslist = $db->query('SELECT * FROM db_paysystem')->fetchAll();
foreach ($pslist as $psl) {
$psPage = mb_strtolower($psl['name']);
$psVal = mb_strtolower($psl['currency']);

 ?>
	<div class="col-xl-3 col-lg-4 col-md-6 p-1">
	<a href="/user/insert_surf/<?=$psPage;?>" class="card mb-1 px-2 p-1">
	<div>
	<table>
	<tr>
		<td><img src="/img/pay/icon/<?=$psVal;?>.png" style="width: 50px;"></td>
		<td class="ps-3"><div><h4 class="notranslate mb-0"><?=$psl['title'];?></h4> <span class="btn btn-sm btn-light mb-2"><?=$psl['currency'];?></span></div></td>
	</tr>
	</table></div>
</a>
	</div>
	<?php
}
 ?>

</div>

<?php 
} 
?>