<?PHP
$_OPTIMIZATION["title"] = $lang['bonus_daily']['title'];
$usid = (int)$_SESSION["user_id"];
$uname = $_SESSION["user"];
# Bonus settings
$bonus_min = 10;
$bonus_max = 100;
define('SITE_KEY', '6LdFxb4iAAAAAKZ1giB1id0GRlsGdCS8eA3_oy34');
define('SECRET_KEY', '6LdFxb4iAAAAAMx5bkp8vpafYNffSm8tRtHW22nk');
if($_POST){
    function getCaptcha($SecretKey){
        $Response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".SECRET_KEY."&response={$SecretKey}");
        $Return = json_decode($Response);
        return $Return;
    }
    $Return = getCaptcha($_POST['g-recaptcha-response']);
	
}
?>

<section class="main races">
    <div class="wrapper-constrained">
        <div class="content-wrapper flex">
            <div class="perfpage">
                
                <div class="pinfo">
                    <div class="picon"></div>
                    <div class="pcont">
                        You can receive bonus from  10  to  100  gold/silver every 12 hours!                    </div>
                </div>
				<div class="pc-message elven" id="msgw1">								
	        <div class="msg-type sm-success"></div>
	        <div class="content">Simple Text Message</div> 
	        <div class="decor middle"></div>
            <div class="decor left"></div>
            <div class="decor right"></div>
	    </div>                   
                
                <div class="container elven">
                    <div class="decor big"></div>
                    <h3><span>12 Hours bonus</span></h3>
                    <div class="container-inner bonus-container">
                        <div class="get-bonus-area">

						<?PHP
                $ddel = time() + 60*60*12;
                $dadd = time();
                $db->Query("SELECT COUNT(*) FROM db_bonus_list WHERE user_id = '$usid' AND date_del > '$dadd'");
				
                $hide_form = false;
                if($db->FetchRow() == 0){
					$db->Query("SELECT referer_id FROM db_users_a  WHERE id = '$usid' LIMIT 1");
$user_data = $db->FetchArray();

$refid = $user_data['referer_id'];
                    # Выдача бонуса
                    if(isset($_POST["bonus"]) && $Return->success == true && $Return->score > 0.5 ){
                        $sum = mt_rand($bonus_min*10,mt_rand($bonus_min*10,$bonus_max*10))/10;
						$ale=random_int(0,1);
						if ($ale==0){
							$metal ='silver' ;
							$money_b ='money_b';
							
						}else {
							$metal ='gold' ;
							$money_b ='money_b1';
						}
                        # Credit bonus
                        $db->Query("UPDATE db_users_b SET $money_b = $money_b + '$sum' WHERE id = '$usid'");
						
						$to_referer = ($sum * 0.5);
						$to_refererbch= $to_referer/1000000 ;
						$db->Query("UPDATE db_users_b SET $money_b = $money_b + '$to_referer' , from_referals = from_referals + '$to_refererbch' WHERE id = '$refid'");
                        # Insert bonus on history
                        $db->Query("INSERT INTO db_bonus_list (user, user_id, sum, metal,date_add, date_del) VALUES ('$uname','$usid','$sum','$metal','$dadd','$ddel')");
                        # Cleaning of obsolete records
                        $db->Query("DELETE FROM db_bonus_list WHERE date_del < '$dadd'");
                        $mess= 'You have earned '.$sum.' '.$metal;
						echo "<script type='text/javascript'>$(document).ready(function(){show_message('{$mess}',1,6000); }); </script>";
                        $hide_form = true;
                    }	if(!$hide_form){
                ?>
                            <div class="bonus-button-area">
                                                                    <p class="mt20">Come every 12 Hours and get bonus!</p>
																	<script src='https://www.google.com/recaptcha/api.js?render=<?php echo SITE_KEY; ?>'></script>
																	<form action="" method="post">
																	<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" /><br >
                                    <button type="submit" name="bonus" class="btn-get-bonus big-button">Get Bonus</button>
									</form>
									<script>
    grecaptcha.ready(function() {
    grecaptcha.execute('<?php echo SITE_KEY; ?>', {action: 'homepage'})
    .then(function(token) {
        //console.log(token);
        document.getElementById('g-recaptcha-response').value=token;
    });
    });
    </script>
									
                                                                </div>
																
																<div class="ent-right"></div>
																
				<?php } 
				}else
					{ 
					$db->Query("SELECT date_del FROM db_bonus_list WHERE user_id = '$usid' ORDER BY id ASC LIMIT 1");
                    $time = $db->FetchRow();
                    $start = new DateTime(date('Y-m-d H:i:s'));
                    $end = new DateTime(date('Y-m-d H:i:s',$time));
                    $diff  = $start->diff($end); ?>
					 <div class="bonus-button-area">
					<div class="wait-bonus">
                                <img src="/images/elvenstar_clock.png" width="140"><br>
                Receiving bonus time left is :<br><span id="bonus-time"><?php echo ($diff->format('%h:%I:%S')) ?></span>
					</div> </div> <div class="ent-right"></div> <?php } ?>

<!--                            <div class="bag-left"></div>-->
                           
                        </div>
						 <h3 class="mt50">Recent bonuses:</h3>
						 


                        <div class="bonus-list-area">
                            <div class="content" id="bonus-content">
							<table class="table table-striped">
                    <thead class="text-center">
                       
                        <th><?= $lang['common']['username'] ?></th>
                        <th>Bonus amount</th>
                        <th><?= $lang['common']['date'] ?></th>
                    </thead>
                    <?PHP
                    $db->Query("SELECT * FROM db_bonus_list ORDER BY id DESC LIMIT 20");
                    if($db->NumRows() > 0){
                        while($bon = $db->FetchArray()){
                            ?>
                            <tr class="text-center">
                                
                                <td><i class="fa fa-user"></i> <?php echo $bon["user"]; ?></td>
                                <td><?php echo $bon["sum"]; ?> <?php if ($bon["metal"]=='gold') echo ('<span class="ic-gold2"></span>'); else echo ('<span class="ic-gold1"></span>' ); ?></td>
                                <td><i class="fa fa-calendar"></i> <?php echo date("h:m:s | d.m.Y",$bon["date_add"]); ?></td>
                            </tr>
                            <?PHP
                        }
                    }else echo '<tr><td class="text-center" colspan="4">'. $lang['bonus_daily']['no_rec'] .'</td></tr>';
                ?>
                </table>
                                    </div>
                        </div>
                    </div>
                    <div class="decor middle"></div>
                    <div class="decor left"></div>
                    <div class="decor right"></div>
                </div>
            </div>
        </div>
    </div>


</section>