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


$timeTarif = time();

# Ищем покупки
$update_speed = $db->query("SELECT * FROM db_store WHERE uid = '$uid' AND status = 1 ORDER BY end DESC")->fetchAll();
foreach($update_speed as $us) {

	# Если срок прошел
	if ($us['end'] < $timeTarif) {

		# Убавляем скорость
		$speed_down = $us['speed'];
		$hash_down = $us['hashpower'];
		$db->query("UPDATE db_users SET speed = speed - '$speed_down', `bank` = `bank` - '$hash_down' WHERE id = '$uid'");

		# Меняем статус на 2
		$pers_id = $us['id'];
		$db->query("UPDATE db_store SET status = '2' WHERE id = '$pers_id'");
	}
}

# Персонажи
$pers = $db->query('SELECT * FROM db_tarif')->fetchAll();

# Покупка персонажа
if(isset($_POST['buy']) && $id = intval($_POST['buy'])) {
	foreach ($pers as $buy) {
		if($buy['id'] === $id) {

			# Проверка баланса
			if($buy['price'] <= $user['money_p']) {

			# Характеристика
			$title = $buy['title'];
			$price = $buy['price'];
			$speed = round($buy['price']*$buy['speed'],4);
			$bank = round($buy['price']*$buy['profit_speed'],4);
			$bankup = round($speed/100,2);
			$time = time();

	$sumPay22 = $price; // трачу
	$sumHold = $user['hold']; // холд

	if($sumPay22 < $sumHold){
	    if ($sumHold > 0) {
	    	$holdm = $sumPay22;  
	    }
	    else {
	        $holdm = $sumHold;
	    }
	} else {
		$holdm = $sumHold;
	}

			# Обновляем пользователя
		//	$db->query('UPDATE db_users SET money_p = money_p - ?, speed = speed + ?, bank = bank + ?, bankin = bankin + ?, hold = hold - ?, `last` = ? WHERE id = ?
		//	', $price, $speed, $bank, $price, $holdm, $time, $uid);
		
		//	$db->query("INSERT INTO db_store (uid, tarif, title, speed, level, `add`, `end`, `last` ) VALUES ('$uid', '$id', '$title', '$speed', '1', '$time', '$end', '$time')");
			echo '<div class="alert alert-success text-center">You bought '.$title.' <b style="color: #f5086b;">+Daily profit '.$bankup.' {!VAL!}</b></div>';

			} else echo '<div class="alert alert-danger text-center">Not enough funds to pay!</div>';	
		}
	}
}
?>
<div class="row mt-5" id="tarif">

<?php
foreach($pers as $b){
$month= round(($b['speed']*100)/100,4);
$profitPrc= round(($b['profit_speed']*100),4);
if ($b['id'] == 1) {
	$duration = '8';
}
if ($b['id'] == 2) {
	$duration = '6';
}
if ($b['id'] == 3) {
	$duration = '4';
}
if ($b['id'] == 4) {
	$duration = '3';
}
if ($b['id'] == 5) {
	$duration = '2';
}
?>


<div class="col-xl-4 col-lg-6 p-2 p-lg-3 p-xl-4">

<div class="planbox zoomIn wow mb-5" style="visibility: visible; animation-name: zoomIn;">
          <div class="plantop">
<div>
	<img src="/img/items/<?=$b['id'];?>.png">
</div>
			<div class="planpercentage"><span>+</span><?=($month*$b['period']); ?><span>%</span></div>
			<div class="plandays"><?=$b['title'];?></div>
          </div>
          <div class="planbottom">
            <div class="investrange">Daily profit: <span><br>+<?=($month); ?><span>%</span></span></div>
          </div>
          <div class="planbottom">
            <div class="investrange">Investment Range: <span><br><?=$b['price']; ?> - <?=$b['price']*10; ?> {!VAL!}</span></div>
          </div>
          <div class="planbottom">
            <div class="investrange">Duration: <span><br><?=$b['period']; ?> days</span></div>
          </div>
        </div>

</div>
<?php
	}
?> 

</div>
