<?php
/*
 * Naijadev Clone 3.1.0 (http://naijadev.ml or http://naijadev.in)
 *
 * For copyright and license information, please see the LICENSE.md
 * Installing the system or redistributions of files must retain the above copyright notice.
 *
 * @link        http://facebook.com/tunzxywalex Tunz Project
 * @email		Tunzxywalex@gmail.com
 * @Whatsapp	+2349076642400
 * @copyright   Copyright (C) Naijadev Community 2018
 * @license     Premium Version Licence
 */
define('_IN_JOHNCMS', 1);
 
//require('bootstrap.php');
 
/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();
 
/** @var PDO $db */
$db = $container->get(PDO::class);
 
/** @var Johncms\Api\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);
 
/** @var Johncms\Api\ToolsInterface $tools */
$tools = $container->get(Johncms\Api\ToolsInterface::class);
 
/** @var Johncms\Api\EnvironmentInterface $env */
$env = $container->get(Johncms\Api\EnvironmentInterface::class);
 
/** @var Johncms\Api\BbcodeInterface $bbcode */
$bbcode = $container->get(Johncms\Api\BbcodeInterface::class);
 
/** @var Johncms\Api\ConfigInterface $config */
$config = $container->get(Johncms\Api\ConfigInterface::class);
 
 
if (isset($systemUser->id)) {
    $days = 10;      # Amount of days
    $bonus = 10000;  # Amount of main bonus
    $restart = 1;    # Binding by days: 1 - on, 0 - off.
    function restart($time, $id) {
        global $db;
        $sql = "UPDATE users SET balans_time = $time, balans_day = 0 WHERE id = $id";
        $db->exec($sql);
        header('Location:'. strip_tags($_SERVER['PHP_SELF']));
        exit;
    }
 
 
    $time = time();
    $lbl = array(0);
    for ($i = 1; $i <= $days; $i++) {
        $lbl[] = $time - 3600 * 24 * $i;
    }
 
    $sql = "SELECT balans_day, balans_time FROM users WHERE id = ". $systemUser->id;
    $res = $db->query($sql);
    $row = $res->fetch();
    $day = (int)$row['balans_day'];
    $tm = (int)$row['balans_time'];
    !($tm == 0 || $lbl[$days] > $tm) ?: restart($time, $systemUser->id);
 
    for ($i = 1; $i <= $days; $i++) {
        if ($tm >= $lbl[$i] && $day == 0) {
            $today = $i; 
            break;
        } elseif ($tm < $lbl[$i] && $tm >= $lbl[$i + 1] && $day <= $i) {
            $today = $i + 1;
        }
    }
 
    if (!empty($today)) {
        !($restart == 1 && $today - $day > 1) ?: restart($time, $systemUser->id);
        $today == $days ? $balls = $bonus : $balls = mt_rand($today * $today, $today * $days) * 20;
        $sql = "UPDATE users SET balans = balans + $balls, balans_day = $today WHERE id = ". $systemUser->id;
        $db->exec($sql);
        $out = '<p><table id=down><tr><td class="small w grad"><b>Reward System!</b><br>';
        for ($i = 1; $i <= $days; $i++) {
        }
        $out .= ' Congratulations !<br> You have been credited  '. ($balls == $bonus ? 
            '<span style="color:red"><b>'. $balls .'</b></span>' : $balls) .' points, for logging in today! 
<br>Come often to earn more credits. <br>you can also visit our <a href="/shop">Shop Store!"</a></table></div>';
        echo $out;
    }
}